From c1a99a03f822bd3a155dae162bb88fd8eef49cef Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 17:52:59 -0400 Subject: [PATCH 1/7] Add test case. --- org_mode_samples/footnote_reference/simple.org | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 org_mode_samples/footnote_reference/simple.org diff --git a/org_mode_samples/footnote_reference/simple.org b/org_mode_samples/footnote_reference/simple.org new file mode 100644 index 0000000..5360307 --- /dev/null +++ b/org_mode_samples/footnote_reference/simple.org @@ -0,0 +1,8 @@ +[fn:1] This is a regular footnote definition because it is on an unindented line while lacking a definition inside the brackets. + + + [fn:1] This is a footnote reference because the line is indented + +[fn:2:This is a footnote reference since it has the definition inside the brackets. This style is referred to as an "inline footnote".] + +[fn::This is a footnote reference since it has the definition inside the brackets. This style is referred to as an "anonymous footnote".] From 9c2eb3b122296b064ae9a2da9a2f80e6ea0d3948 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 18:56:46 -0400 Subject: [PATCH 2/7] Create structure for footnote references. --- src/compare/diff.rs | 25 +++++++++++++++++++++++++ src/parser/footnote_reference.rs | 13 +++++++++++++ src/parser/mod.rs | 2 ++ src/parser/object.rs | 15 +++++++++++++++ src/parser/object_parser.rs | 11 ++++++++++- src/parser/token.rs | 3 +++ 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/parser/footnote_reference.rs diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 61f0479..9a04cec 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -19,6 +19,7 @@ use crate::parser::ExportBlock; use crate::parser::ExportSnippet; use crate::parser::FixedWidthArea; use crate::parser::FootnoteDefinition; +use crate::parser::FootnoteReference; use crate::parser::GreaterBlock; use crate::parser::Heading; use crate::parser::HorizontalRule; @@ -160,6 +161,7 @@ fn compare_object<'s>( Object::Entity(obj) => compare_entity(source, emacs, obj), Object::LatexFragment(obj) => compare_latex_fragment(source, emacs, obj), Object::ExportSnippet(obj) => compare_export_snippet(source, emacs, obj), + Object::FootnoteReference(obj) => compare_footnote_reference(source, emacs, obj), } } @@ -1313,3 +1315,26 @@ fn compare_export_snippet<'s>( children: Vec::new(), }) } + +fn compare_footnote_reference<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s FootnoteReference<'s>, +) -> Result> { + let mut this_status = DiffStatus::Good; + let emacs_name = "footnote-reference"; + if assert_name(emacs, emacs_name).is_err() { + this_status = DiffStatus::Bad; + } + + if assert_bounds(source, emacs, rust).is_err() { + this_status = DiffStatus::Bad; + } + + Ok(DiffResult { + status: this_status, + name: emacs_name.to_owned(), + message: None, + children: Vec::new(), + }) +} diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs new file mode 100644 index 0000000..c43253b --- /dev/null +++ b/src/parser/footnote_reference.rs @@ -0,0 +1,13 @@ +use super::Context; +use crate::error::Res; +use crate::parser::FootnoteReference; +use crate::parser::util::not_yet_implemented; + +#[tracing::instrument(ret, level = "debug")] +pub fn footnote_reference<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, FootnoteReference<'s>> { + not_yet_implemented()?; + todo!() +} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 45d687b..6cf9bbc 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -12,6 +12,7 @@ mod exiting; mod export_snippet; mod fixed_width_area; mod footnote_definition; +mod footnote_reference; mod greater_block; mod greater_element; mod horizontal_rule; @@ -75,6 +76,7 @@ pub use object::Bold; pub use object::Code; pub use object::Entity; pub use object::ExportSnippet; +pub use object::FootnoteReference; pub use object::Italic; pub use object::LatexFragment; pub use object::Object; diff --git a/src/parser/object.rs b/src/parser/object.rs index 9bd56ef..481408f 100644 --- a/src/parser/object.rs +++ b/src/parser/object.rs @@ -18,6 +18,7 @@ pub enum Object<'s> { Entity(Entity<'s>), LatexFragment(LatexFragment<'s>), ExportSnippet(ExportSnippet<'s>), + FootnoteReference(FootnoteReference<'s>), } #[derive(Debug, PartialEq)] @@ -117,6 +118,13 @@ pub struct ExportSnippet<'s> { pub contents: Option<&'s str>, } +#[derive(Debug, PartialEq)] +pub struct FootnoteReference<'s> { + pub source: &'s str, + pub label: &'s str, + pub definition: Vec>, +} + impl<'s> Source<'s> for Object<'s> { fn get_source(&'s self) -> &'s str { match self { @@ -136,6 +144,7 @@ impl<'s> Source<'s> for Object<'s> { Object::Entity(obj) => obj.source, Object::LatexFragment(obj) => obj.source, Object::ExportSnippet(obj) => obj.source, + Object::FootnoteReference(obj) => obj.source, } } } @@ -229,3 +238,9 @@ impl<'s> Source<'s> for ExportSnippet<'s> { self.source } } + +impl<'s> Source<'s> for FootnoteReference<'s> { + fn get_source(&'s self) -> &'s str { + self.source + } +} diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index e84992b..8ee65d3 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -10,6 +10,7 @@ use crate::error::Res; use crate::parser::angle_link::angle_link; use crate::parser::entity::entity; use crate::parser::export_snippet::export_snippet; +use crate::parser::footnote_reference::footnote_reference; use crate::parser::latex_fragment::latex_fragment; use crate::parser::object::Object; use crate::parser::org_macro::org_macro; @@ -23,10 +24,14 @@ pub fn standard_set_object<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Object<'s>> { - // TODO: footnote references, citations (NOT citation references), inline babel calls, inline source blocks, line breaks, links, macros, targets and radio targets, statistics cookies, subscript and superscript, timestamps, and text markup. + // TODO: citations (NOT citation references), inline babel calls, inline source blocks, line breaks, links, macros, targets and radio targets, statistics cookies, subscript and superscript, timestamps, and text markup. not(|i| context.check_exit_matcher(i))(input)?; alt(( + map( + parser_with_context!(footnote_reference)(context), + Object::FootnoteReference, + ), map( parser_with_context!(export_snippet)(context), Object::ExportSnippet, @@ -79,6 +84,10 @@ pub fn any_object_except_plain_text<'r, 's>( ) -> Res<&'s str, Object<'s>> { // Used for exit matchers so this does not check exit matcher condition. alt(( + map( + parser_with_context!(footnote_reference)(context), + Object::FootnoteReference, + ), map( parser_with_context!(export_snippet)(context), Object::ExportSnippet, diff --git a/src/parser/token.rs b/src/parser/token.rs index c97cbe1..5bbf740 100644 --- a/src/parser/token.rs +++ b/src/parser/token.rs @@ -55,6 +55,9 @@ impl<'r, 's> Token<'r, 's> { Object::Entity(_) => Box::new(std::iter::empty()), Object::LatexFragment(_) => Box::new(std::iter::empty()), Object::ExportSnippet(_) => Box::new(std::iter::empty()), + Object::FootnoteReference(inner) => { + Box::new(inner.definition.iter().map(Token::Object)) + } }, Token::Element(elem) => match elem { Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)), From 5fb66a586d81215518eec95298d5664e87340431 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 20:52:09 -0400 Subject: [PATCH 3/7] Implement a function to detect the end of a footnote reference definition with balanced brackets. --- src/parser/footnote_definition.rs | 2 +- src/parser/footnote_reference.rs | 68 ++++++++++++++++++++++++++++++- src/parser/parser_context.rs | 18 ++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 833360e..61318dc 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -67,7 +67,7 @@ pub fn footnote_definition<'r, 's>( } #[tracing::instrument(ret, level = "debug")] -fn label<'s>(input: &'s str) -> Res<&'s str, &'s str> { +pub fn label<'s>(input: &'s str) -> Res<&'s str, &'s str> { alt(( digit1, take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c)), diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index c43253b..6d88df0 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -1,13 +1,79 @@ +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::bytes::complete::tag_no_case; + +use super::parser_context::ContextElement; use super::Context; +use crate::error::CustomError; +use crate::error::MyError; use crate::error::Res; -use crate::parser::FootnoteReference; +use crate::parser::parser_context::FootnoteReferenceDefinition; +use crate::parser::parser_with_context::parser_with_context; +use crate::parser::util::get_consumed; use crate::parser::util::not_yet_implemented; +use crate::parser::FootnoteReference; +use crate::parser::Object; #[tracing::instrument(ret, level = "debug")] 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) +} + +#[tracing::instrument(ret, level = "debug")] +pub fn anonymous_footnote<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, FootnoteReference<'s>> { + let (remaining, _) = tag_no_case("[fn::")(input)?; + not_yet_implemented()?; todo!() } + +#[tracing::instrument(ret, level = "debug")] +fn definition<'s>(input: &'s str) -> Res<&'s str, Vec>> { + Ok((input, vec![])) +} + +#[tracing::instrument(ret, level = "debug")] +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 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;}, + ']' 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")))); + } + tag("]")(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn get_bracket_depth<'r, 's>( + context: Context<'r, 's>, +) -> Option<&'r FootnoteReferenceDefinition<'s>> { + for node in context.iter() { + match node.get_data() { + ContextElement::FootnoteReferenceDefinition(depth) => return Some(depth), + _ => {} + } + } + None +} diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index c49e099..d2c105e 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -141,6 +141,18 @@ pub enum ContextElement<'r, 's> { /// org-mode document since text needs to be re-parsed to look for /// radio links matching the contents of radio targets. RadioTarget(Vec<&'r Vec>>), + + /// Stores the current bracket depth inside a footnote reference's definition. + /// + /// The definition inside a footnote reference must have balanced + /// brackets [] inside the definition, so this stores the amount + /// of opening brackets subtracted by the amount of closing + /// brackets within the definition. + /// + /// A reference to the position in the string is also included so + /// unbalanced brackets can be detected in the middle of an + /// object. + FootnoteReferenceDefinition(FootnoteReferenceDefinition<'s>), } pub struct ExitMatcherNode<'r> { @@ -148,6 +160,12 @@ pub struct ExitMatcherNode<'r> { pub class: ExitClass, } +#[derive(Debug)] +pub struct FootnoteReferenceDefinition<'s> { + pub position: &'s str, + pub depth: usize, +} + impl<'r> std::fmt::Debug for ExitMatcherNode<'r> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut formatter = f.debug_struct("ExitMatcherNode"); From 6822069c2f1fd65104c3534da01cd6c3a0ea0545 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 21:14:09 -0400 Subject: [PATCH 4/7] 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>, } From a36a820e84ddb92210b79aeca37043af70d97279 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 21:32:08 -0400 Subject: [PATCH 5/7] Implement parser for the simplest form of footnote reference. --- src/parser/footnote_reference.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index 34c8428..e7fe3ba 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -9,6 +9,7 @@ use super::Context; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; +use crate::parser::footnote_definition::label; use crate::parser::object_parser::standard_set_object; use crate::parser::parser_context::FootnoteReferenceDefinition; use crate::parser::parser_with_context::parser_with_context; @@ -27,7 +28,7 @@ pub fn footnote_reference<'r, 's>( } #[tracing::instrument(ret, level = "debug")] -pub fn anonymous_footnote<'r, 's>( +fn anonymous_footnote<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, FootnoteReference<'s>> { @@ -57,6 +58,34 @@ pub fn anonymous_footnote<'r, 's>( )) } +#[tracing::instrument(ret, level = "debug")] +fn inline_footnote<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, FootnoteReference<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn footnote_reference_only<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, FootnoteReference<'s>> { + let (remaining, _) = tag_no_case("[fn:")(input)?; + let (remaining, label_contents) = label(remaining)?; + let (remaining, _) = tag("]")(remaining)?; + let source = get_consumed(input, remaining); + Ok(( + remaining, + FootnoteReference { + source, + label: Some(label_contents), + definition: Vec::with_capacity(0), + }, + )) +} + #[tracing::instrument(ret, level = "debug")] fn definition<'s>(input: &'s str) -> Res<&'s str, Vec>> { Ok((input, vec![])) From b850f596408713537661a14e260c5446f23a7128 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 23:20:17 -0400 Subject: [PATCH 6/7] Implement parser for inline footnotes. --- src/parser/footnote_reference.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index e7fe3ba..e023164 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -15,7 +15,6 @@ 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; use crate::parser::Object; @@ -39,6 +38,7 @@ fn anonymous_footnote<'r, 's>( depth: 0, }, )); + // 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), @@ -63,8 +63,33 @@ fn inline_footnote<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, FootnoteReference<'s>> { - not_yet_implemented()?; - todo!() + 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, + }, + )); + // 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 == &"]", + )(remaining)?; + + let source = get_consumed(input, remaining); + Ok(( + remaining, + FootnoteReference { + source, + label: Some(label_contents), + definition: children, + }, + )) } #[tracing::instrument(ret, level = "debug")] From 7611deb1ff6f2180f5e6ae48b16e7ee4a5501024 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 23:40:26 -0400 Subject: [PATCH 7/7] Forgot to add the exit matcher nodes to the context and match the trailing ']'. --- src/parser/footnote_reference.rs | 52 +++++++++++++++++++++++--------- toy_language.txt | 4 +-- 2 files changed, 38 insertions(+), 18 deletions(-) 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".]