Implement the parser for inline babel calls.
This commit is contained in:
parent
2773b35438
commit
45b01012b3
@ -1,6 +1,24 @@
|
|||||||
|
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 super::Context;
|
||||||
use crate::error::Res;
|
use crate::error::Res;
|
||||||
use crate::parser::util::not_yet_implemented;
|
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;
|
use crate::parser::InlineBabelCall;
|
||||||
|
|
||||||
#[tracing::instrument(ret, level = "debug")]
|
#[tracing::instrument(ret, level = "debug")]
|
||||||
@ -8,6 +26,132 @@ pub fn inline_babel_call<'r, 's>(
|
|||||||
context: Context<'r, 's>,
|
context: Context<'r, 's>,
|
||||||
input: &'s str,
|
input: &'s str,
|
||||||
) -> Res<&'s str, InlineBabelCall<'s>> {
|
) -> Res<&'s str, InlineBabelCall<'s>> {
|
||||||
not_yet_implemented()?;
|
let (remaining, _) = tag_no_case("call_")(input)?;
|
||||||
todo!()
|
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
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
|
@ -60,7 +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()), // 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)),
|
||||||
|
Loading…
Reference in New Issue
Block a user