2023-07-21 22:51:19 -04:00
|
|
|
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;
|
2023-07-21 23:14:52 -04:00
|
|
|
use tracing::span;
|
2023-07-21 22:51:19 -04:00
|
|
|
|
2023-07-21 22:29:04 -04:00
|
|
|
use super::Context;
|
2023-07-21 22:51:19 -04:00
|
|
|
use crate::error::CustomError;
|
2023-07-21 22:29:04 -04:00
|
|
|
use crate::error::Res;
|
2023-07-21 22:51:19 -04:00
|
|
|
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;
|
2023-07-21 22:29:04 -04:00
|
|
|
use crate::parser::InlineSourceBlock;
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
pub fn inline_source_block<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, InlineSourceBlock<'s>> {
|
2023-07-21 22:51:19 -04:00
|
|
|
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 {
|
2023-07-21 23:14:52 -04:00
|
|
|
position: remaining,
|
2023-07-21 22:51:19 -04:00
|
|
|
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 {
|
2023-07-21 23:14:52 -04:00
|
|
|
position: remaining,
|
2023-07-21 22:51:19 -04:00
|
|
|
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)?;
|
2023-07-21 23:14:52 -04:00
|
|
|
let (remaining, _) = {
|
|
|
|
let span = span!(
|
|
|
|
tracing::Level::DEBUG,
|
|
|
|
"outside end body",
|
|
|
|
remaining = remaining
|
|
|
|
);
|
|
|
|
let _enter = span.enter();
|
|
|
|
tag("}")(remaining)?
|
|
|
|
};
|
2023-07-21 22:51:19 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2023-07-21 23:14:52 -04:00
|
|
|
{
|
|
|
|
let span = span!(
|
|
|
|
tracing::Level::DEBUG,
|
|
|
|
"inside end body",
|
|
|
|
remaining = input,
|
|
|
|
current_depth = current_depth
|
|
|
|
);
|
|
|
|
let _enter = span.enter();
|
|
|
|
|
|
|
|
if current_depth == 0 {
|
|
|
|
let close_bracket = tag::<&str, &str, CustomError<&str>>("}")(input);
|
|
|
|
if close_bracket.is_ok() {
|
|
|
|
return close_bracket;
|
|
|
|
}
|
2023-07-21 22:51:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-07-21 22:29:04 -04:00
|
|
|
}
|