Implement parser for inline source blocks.
This commit is contained in:
parent
e0d2bb8213
commit
e8979513aa
@ -80,7 +80,7 @@ fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s
|
||||
#[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.");
|
||||
.expect("This function should only be called from inside an 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() {
|
||||
@ -125,7 +125,7 @@ fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'
|
||||
#[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.");
|
||||
.expect("This function should only be called from inside an 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() {
|
||||
|
@ -1,6 +1,24 @@
|
||||
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::CustomError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::not_yet_implemented;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_context::InlineSourceBlockBracket;
|
||||
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::InlineSourceBlock;
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
@ -8,6 +26,151 @@ pub fn inline_source_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, InlineSourceBlock<'s>> {
|
||||
not_yet_implemented()?;
|
||||
todo!()
|
||||
let (remaining, _) = tag_no_case("src_")(input)?;
|
||||
let (remaining, _) = lang(context, remaining)?;
|
||||
let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?;
|
||||
let (remaining, _body) = body(context, remaining)?;
|
||||
let (remaining, _) = space0(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((remaining, InlineSourceBlock { source }))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn lang<'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: &lang_end,
|
||||
}));
|
||||
let (remaining, lang) = recognize(many_till(
|
||||
verify(anychar, |c| !(c.is_whitespace() || "[{".contains(*c))),
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(input)?;
|
||||
Ok((remaining, lang))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn lang_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::InlineSourceBlockBracket(
|
||||
InlineSourceBlockBracket {
|
||||
position: input,
|
||||
depth: 0,
|
||||
},
|
||||
))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &header_end,
|
||||
}));
|
||||
|
||||
let (remaining, header_contents) = recognize(many_till(
|
||||
anychar,
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(remaining)?;
|
||||
let (remaining, _) = tag("]")(remaining)?;
|
||||
Ok((remaining, header_contents))
|
||||
}
|
||||
|
||||
#[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 an inline source block 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 source block header bracket depth.")
|
||||
}
|
||||
']' if current_depth > 0 => {
|
||||
current_depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if current_depth == 0 {
|
||||
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input);
|
||||
if close_bracket.is_ok() {
|
||||
return close_bracket;
|
||||
}
|
||||
}
|
||||
|
||||
line_ending(input)
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn body<'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::InlineSourceBlockBracket(
|
||||
InlineSourceBlockBracket {
|
||||
position: input,
|
||||
depth: 0,
|
||||
},
|
||||
))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &body_end,
|
||||
}));
|
||||
|
||||
let (remaining, body_contents) = recognize(many_till(
|
||||
anychar,
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(remaining)?;
|
||||
let (remaining, _) = tag("}")(remaining)?;
|
||||
Ok((remaining, body_contents))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn body_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 an inline source block body.");
|
||||
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 source block body bracket depth.")
|
||||
}
|
||||
'}' if current_depth > 0 => {
|
||||
current_depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if current_depth == 0 {
|
||||
let close_bracket = tag::<&str, &str, CustomError<&str>>("}")(input);
|
||||
if close_bracket.is_ok() {
|
||||
return close_bracket;
|
||||
}
|
||||
}
|
||||
|
||||
line_ending(input)
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
pub fn get_bracket_depth<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
) -> Option<&'r InlineSourceBlockBracket<'s>> {
|
||||
for node in context.iter() {
|
||||
match node.get_data() {
|
||||
ContextElement::InlineSourceBlockBracket(depth) => return Some(depth),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
@ -181,6 +181,19 @@ pub enum ContextElement<'r, 's> {
|
||||
/// unbalanced brackets can be detected in the middle of an
|
||||
/// object.
|
||||
BabelHeaderBracket(BabelHeaderBracket<'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.
|
||||
InlineSourceBlockBracket(InlineSourceBlockBracket<'s>),
|
||||
}
|
||||
|
||||
pub struct ExitMatcherNode<'r> {
|
||||
@ -206,6 +219,12 @@ pub struct BabelHeaderBracket<'s> {
|
||||
pub depth: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InlineSourceBlockBracket<'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");
|
||||
|
Loading…
Reference in New Issue
Block a user