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::org_source::OrgSource; 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; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_babel_call<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, 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: source.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn name<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Gamma, 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)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn name_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { recognize(one_of("[("))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn header<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let (remaining, _) = tag("[")(input)?; let parser_context = context .with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket { position: remaining, depth: 0, })) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Gamma, 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)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn header_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .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 Into::<&str>::into(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) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn argument<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let (remaining, _) = tag("(")(input)?; let parser_context = context .with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket { position: remaining, depth: 0, })) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Gamma, 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)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn argument_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .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 Into::<&str>::into(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) } #[cfg_attr(feature = "tracing", 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 }