Merge branch 'inline_babel'
All checks were successful
semver Build semver has succeeded
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded

This commit is contained in:
Tom Alexander 2023-07-21 21:40:18 -04:00
commit b323a407c4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 240 additions and 3 deletions

View File

@ -0,0 +1,4 @@
call_foo(arguments)
call_bar[header](arguments)
call_baz(arguments)[header]
call_lorem[header](arguments)[another header]

View File

@ -25,6 +25,7 @@ use crate::parser::FootnoteReference;
use crate::parser::GreaterBlock; use crate::parser::GreaterBlock;
use crate::parser::Heading; use crate::parser::Heading;
use crate::parser::HorizontalRule; use crate::parser::HorizontalRule;
use crate::parser::InlineBabelCall;
use crate::parser::Italic; use crate::parser::Italic;
use crate::parser::Keyword; use crate::parser::Keyword;
use crate::parser::LatexEnvironment; use crate::parser::LatexEnvironment;
@ -166,6 +167,7 @@ fn compare_object<'s>(
Object::FootnoteReference(obj) => compare_footnote_reference(source, emacs, obj), Object::FootnoteReference(obj) => compare_footnote_reference(source, emacs, obj),
Object::Citation(obj) => compare_citation(source, emacs, obj), Object::Citation(obj) => compare_citation(source, emacs, obj),
Object::CitationReference(obj) => compare_citation_reference(source, emacs, obj), Object::CitationReference(obj) => compare_citation_reference(source, emacs, obj),
Object::InlineBabelCall(obj) => compare_inline_babel_call(source, emacs, obj),
} }
} }
@ -1388,3 +1390,26 @@ fn compare_citation_reference<'s>(
children: Vec::new(), children: Vec::new(),
}) })
} }
fn compare_inline_babel_call<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s InlineBabelCall<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "inline-babel-call";
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(),
})
}

View File

@ -0,0 +1,157 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::anychar;
use nom::character::complete::line_ending;
use nom::character::complete::one_of;
use nom::character::complete::space0;
use nom::combinator::opt;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many_till;
use super::Context;
use crate::error::Res;
use crate::parser::exiting::ExitClass;
use crate::parser::parser_context::BabelHeaderBracket;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ExitMatcherNode;
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::InlineBabelCall;
#[tracing::instrument(ret, level = "debug")]
pub fn inline_babel_call<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, InlineBabelCall<'s>> {
let (remaining, _) = tag_no_case("call_")(input)?;
let (remaining, _name) = name(context, remaining)?;
let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?;
let (remaining, _argument) = argument(context, remaining)?;
let (remaining, _header2) = opt(parser_with_context!(header)(context))(remaining)?;
let (remaining, _) = space0(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, InlineBabelCall { source }))
}
#[tracing::instrument(ret, level = "debug")]
fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &name_end,
}));
let (remaining, name) = recognize(many_till(
verify(anychar, |c| !(c.is_whitespace() || "[]()".contains(*c))),
parser_with_context!(exit_matcher_parser)(&parser_context),
))(input)?;
Ok((remaining, name))
}
#[tracing::instrument(ret, level = "debug")]
fn name_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
recognize(one_of("[("))(input)
}
#[tracing::instrument(ret, level = "debug")]
fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, _) = tag("[")(input)?;
let parser_context = context
.with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket {
position: input,
depth: 0,
}))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &header_end,
}));
let (remaining, name) = recognize(many_till(
anychar,
parser_with_context!(exit_matcher_parser)(&parser_context),
))(remaining)?;
let (remaining, _) = tag("]")(remaining)?;
Ok((remaining, name))
}
#[tracing::instrument(ret, level = "debug")]
fn header_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 inline babel call header.");
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 inline babel call header bracket depth.")
}
')' if current_depth > 0 => {
current_depth -= 1;
}
_ => {}
}
}
alt((tag("]"), line_ending))(input)
}
#[tracing::instrument(ret, level = "debug")]
fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, _) = tag("(")(input)?;
let parser_context = context
.with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket {
position: input,
depth: 0,
}))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &argument_end,
}));
let (remaining, name) = recognize(many_till(
anychar,
parser_with_context!(exit_matcher_parser)(&parser_context),
))(remaining)?;
let (remaining, _) = tag(")")(remaining)?;
Ok((remaining, name))
}
#[tracing::instrument(ret, level = "debug")]
fn argument_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 inline babel call argument.");
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 inline babel call argument bracket depth.")
}
']' if current_depth > 0 => {
current_depth -= 1;
}
_ => {}
}
}
alt((tag(")"), line_ending))(input)
}
#[tracing::instrument(ret, level = "debug")]
pub fn get_bracket_depth<'r, 's>(context: Context<'r, 's>) -> Option<&'r BabelHeaderBracket<'s>> {
for node in context.iter() {
match node.get_data() {
ContextElement::BabelHeaderBracket(depth) => return Some(depth),
_ => {}
}
}
None
}

View File

@ -18,6 +18,7 @@ mod footnote_reference;
mod greater_block; mod greater_block;
mod greater_element; mod greater_element;
mod horizontal_rule; mod horizontal_rule;
mod inline_babel_call;
mod keyword; mod keyword;
mod latex_environment; mod latex_environment;
mod latex_fragment; mod latex_fragment;
@ -81,6 +82,7 @@ pub use object::Code;
pub use object::Entity; pub use object::Entity;
pub use object::ExportSnippet; pub use object::ExportSnippet;
pub use object::FootnoteReference; pub use object::FootnoteReference;
pub use object::InlineBabelCall;
pub use object::Italic; pub use object::Italic;
pub use object::LatexFragment; pub use object::LatexFragment;
pub use object::Object; pub use object::Object;

View File

@ -21,6 +21,7 @@ pub enum Object<'s> {
FootnoteReference(FootnoteReference<'s>), FootnoteReference(FootnoteReference<'s>),
Citation(Citation<'s>), Citation(Citation<'s>),
CitationReference(CitationReference<'s>), CitationReference(CitationReference<'s>),
InlineBabelCall(InlineBabelCall<'s>),
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -137,6 +138,11 @@ pub struct CitationReference<'s> {
pub source: &'s str, pub source: &'s str,
} }
#[derive(Debug, PartialEq)]
pub struct InlineBabelCall<'s> {
pub source: &'s str,
}
impl<'s> Source<'s> for Object<'s> { impl<'s> Source<'s> for Object<'s> {
fn get_source(&'s self) -> &'s str { fn get_source(&'s self) -> &'s str {
match self { match self {
@ -159,6 +165,7 @@ impl<'s> Source<'s> for Object<'s> {
Object::FootnoteReference(obj) => obj.source, Object::FootnoteReference(obj) => obj.source,
Object::Citation(obj) => obj.source, Object::Citation(obj) => obj.source,
Object::CitationReference(obj) => obj.source, Object::CitationReference(obj) => obj.source,
Object::InlineBabelCall(obj) => obj.source,
} }
} }
} }
@ -270,3 +277,9 @@ impl<'s> Source<'s> for CitationReference<'s> {
self.source self.source
} }
} }
impl<'s> Source<'s> for InlineBabelCall<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -12,6 +12,7 @@ use crate::parser::citation::citation;
use crate::parser::entity::entity; use crate::parser::entity::entity;
use crate::parser::export_snippet::export_snippet; use crate::parser::export_snippet::export_snippet;
use crate::parser::footnote_reference::footnote_reference; use crate::parser::footnote_reference::footnote_reference;
use crate::parser::inline_babel_call::inline_babel_call;
use crate::parser::latex_fragment::latex_fragment; use crate::parser::latex_fragment::latex_fragment;
use crate::parser::object::Object; use crate::parser::object::Object;
use crate::parser::org_macro::org_macro; use crate::parser::org_macro::org_macro;
@ -25,10 +26,14 @@ pub fn standard_set_object<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,
) -> Res<&'s str, Object<'s>> { ) -> Res<&'s str, Object<'s>> {
// 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. // TODO: inline source blocks, line breaks, targets (different from radio targets), statistics cookies, subscript and superscript, timestamps.
not(|i| context.check_exit_matcher(i))(input)?; not(|i| context.check_exit_matcher(i))(input)?;
alt(( alt((
map(
parser_with_context!(inline_babel_call)(context),
Object::InlineBabelCall,
),
map(parser_with_context!(citation)(context), Object::Citation), map(parser_with_context!(citation)(context), Object::Citation),
map( map(
parser_with_context!(footnote_reference)(context), parser_with_context!(footnote_reference)(context),
@ -86,6 +91,10 @@ pub fn any_object_except_plain_text<'r, 's>(
) -> Res<&'s str, Object<'s>> { ) -> Res<&'s str, Object<'s>> {
// Used for exit matchers so this does not check exit matcher condition. // Used for exit matchers so this does not check exit matcher condition.
alt(( alt((
map(
parser_with_context!(inline_babel_call)(context),
Object::InlineBabelCall,
),
map(parser_with_context!(citation)(context), Object::Citation), map(parser_with_context!(citation)(context), Object::Citation),
map( map(
parser_with_context!(footnote_reference)(context), parser_with_context!(footnote_reference)(context),
@ -121,6 +130,13 @@ pub fn regular_link_description_object_set<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,
) -> Res<&'s str, Object<'s>> { ) -> Res<&'s str, Object<'s>> {
// TODO: minimal set of objects as well as export snippets, inline babel calls, inline source blocks, macros, and statistics cookies. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] // TODO: minimal set of objects as well as export snippets, inline source blocks, and statistics cookies. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
alt((parser_with_context!(minimal_set_object)(context),))(input) alt((
map(
parser_with_context!(inline_babel_call)(context),
Object::InlineBabelCall,
),
map(parser_with_context!(org_macro)(context), Object::OrgMacro),
parser_with_context!(minimal_set_object)(context),
))(input)
} }

View File

@ -168,6 +168,19 @@ pub enum ContextElement<'r, 's> {
/// unbalanced brackets can be detected in the middle of an /// unbalanced brackets can be detected in the middle of an
/// object. /// object.
CitationBracket(CitationBracket<'s>), CitationBracket(CitationBracket<'s>),
/// Stores the current bracket or parenthesis depth inside an inline babel call.
///
/// Inside an inline babel call the headers must have balanced
/// parentheses () and the arguments must have balanced brackets
/// [], so this stores the amount of opening brackets subtracted
/// by the amount of closing brackets within the definition must
/// equal zero.
///
/// A reference to the position in the string is also included so
/// unbalanced brackets can be detected in the middle of an
/// object.
BabelHeaderBracket(BabelHeaderBracket<'s>),
} }
pub struct ExitMatcherNode<'r> { pub struct ExitMatcherNode<'r> {
@ -187,6 +200,12 @@ pub struct CitationBracket<'s> {
pub depth: usize, pub depth: usize,
} }
#[derive(Debug)]
pub struct BabelHeaderBracket<'s> {
pub position: &'s str,
pub depth: usize,
}
impl<'r> std::fmt::Debug for ExitMatcherNode<'r> { impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut formatter = f.debug_struct("ExitMatcherNode"); let mut formatter = f.debug_struct("ExitMatcherNode");

View File

@ -60,6 +60,7 @@ impl<'r, 's> Token<'r, 's> {
} }
Object::Citation(_) => Box::new(std::iter::empty()), // TODO: Iterate over children Object::Citation(_) => Box::new(std::iter::empty()), // TODO: Iterate over children
Object::CitationReference(_) => Box::new(std::iter::empty()), // TODO: Iterate over children Object::CitationReference(_) => Box::new(std::iter::empty()), // TODO: Iterate over children
Object::InlineBabelCall(_) => Box::new(std::iter::empty()),
}, },
Token::Element(elem) => match elem { Token::Element(elem) => match elem {
Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)), Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),