From 343af41f78de01c53b653d43e09107c7b5306ceb Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 5 Oct 2023 16:27:36 -0400 Subject: [PATCH] Separate babel call out to its own parser. --- src/compare/diff.rs | 1 + src/parser/babel_call.rs | 59 ++++++++++++++++++++++++++++++++++++ src/parser/element_parser.rs | 4 +-- src/parser/keyword.rs | 24 --------------- src/parser/mod.rs | 1 + src/types/lesser_element.rs | 1 - 6 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 src/parser/babel_call.rs diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 2e6f304..7f76639 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -2416,6 +2416,7 @@ fn compare_babel_call<'b, 's>( )); } + // Compare value let value = unquote( get_property(emacs, ":value")? .ok_or("Emacs keywords should have a :value")? diff --git a/src/parser/babel_call.rs b/src/parser/babel_call.rs new file mode 100644 index 0000000..0961332 --- /dev/null +++ b/src/parser/babel_call.rs @@ -0,0 +1,59 @@ +use nom::bytes::complete::tag; +use nom::bytes::complete::tag_no_case; +use nom::character::complete::anychar; +use nom::character::complete::space0; +use nom::combinator::consumed; +use nom::combinator::peek; +use nom::combinator::recognize; +use nom::multi::many0; +use nom::multi::many_till; +use nom::sequence::tuple; +use nom::InputTake; + +use super::keyword::affiliated_keyword; +use super::util::get_name; +use super::OrgSource; +use crate::context::RefContext; +use crate::error::Res; +use crate::parser::util::get_consumed; +use crate::parser::util::org_line_ending; +use crate::types::BabelCall; + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +pub(crate) fn babel_call<'b, 'g, 'r, 's>( + _context: RefContext<'b, 'g, 'r, 's>, + input: OrgSource<'s>, +) -> Res, BabelCall<'s>> { + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; + + let (remaining, (consumed_input, (_, _, parsed_key, _))) = + consumed(tuple((space0, tag("#+"), tag_no_case("call"), tag(":"))))(input)?; + + if let Ok((remaining, (_, line_break))) = tuple((space0, org_line_ending))(remaining) { + let source = get_consumed(input, remaining); + return Ok(( + remaining, + BabelCall { + source: Into::<&str>::into(source), + name: get_name(&affiliated_keywords), + value: Into::<&str>::into(line_break.take(0)), + }, + )); + } + + let (remaining, _ws) = space0(remaining)?; + let (remaining, parsed_value) = + recognize(many_till(anychar, peek(tuple((space0, org_line_ending)))))(remaining)?; + let (remaining, _ws) = tuple((space0, org_line_ending))(remaining)?; + + let source = get_consumed(input, remaining); + + Ok(( + remaining, + BabelCall { + source: Into::<&str>::into(source), + name: get_name(&affiliated_keywords), + value: Into::<&str>::into(parsed_value), + }, + )) +} diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index 81dc435..b416831 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -6,6 +6,7 @@ use nom::sequence::tuple; #[cfg(feature = "tracing")] use tracing::span; +use super::babel_call::babel_call; use super::clock::clock; use super::comment::comment; use super::comment::detect_comment; @@ -20,7 +21,6 @@ use super::footnote_definition::footnote_definition; use super::greater_block::greater_block; use super::horizontal_rule::horizontal_rule; use super::keyword::affiliated_keyword; -use super::keyword::babel_call_keyword; use super::keyword::keyword; use super::latex_environment::latex_environment; use super::lesser_block::comment_block; @@ -76,7 +76,7 @@ fn _element<'b, 'g, 'r, 's>( let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context); let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context); let keyword_matcher = parser_with_context!(keyword)(context); - let babel_keyword_matcher = parser_with_context!(babel_call_keyword)(context); + let babel_keyword_matcher = parser_with_context!(babel_call)(context); let paragraph_matcher = parser_with_context!(paragraph)(context); let latex_environment_matcher = parser_with_context!(latex_environment)(context); diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index 34739bc..19aea56 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -26,7 +26,6 @@ use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::start_of_line; -use crate::types::BabelCall; use crate::types::Keyword; const ORG_ELEMENT_AFFILIATED_KEYWORDS: [&'static str; 13] = [ @@ -104,29 +103,6 @@ pub(crate) fn affiliated_keyword<'s>(input: OrgSource<'s>) -> Res, filtered_keyword(affiliated_key)(input) } -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub(crate) fn babel_call_keyword<'b, 'g, 'r, 's>( - _context: RefContext<'b, 'g, 'r, 's>, - input: OrgSource<'s>, -) -> Res, BabelCall<'s>> { - let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; - let (remaining, kw) = filtered_keyword(babel_call_key)(input)?; - Ok(( - remaining, - BabelCall { - source: kw.source, - name: get_name(&affiliated_keywords), - key: kw.key, - value: kw.value, - }, - )) -} - -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn babel_call_key<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { - tag_no_case("call")(input) -} - #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn table_formula_keyword<'b, 'g, 'r, 's>( _context: RefContext<'b, 'g, 'r, 's>, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b3e2710..02dabf6 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,4 +1,5 @@ mod angle_link; +mod babel_call; mod citation; mod citation_reference; mod clock; diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index 8797bbc..665da39 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -138,7 +138,6 @@ pub struct Keyword<'s> { pub struct BabelCall<'s> { pub source: &'s str, pub name: Option<&'s str>, - pub key: &'s str, pub value: &'s str, }