Separate babel call out to its own parser.

This commit is contained in:
Tom Alexander
2023-10-05 16:27:36 -04:00
parent f49a1853ad
commit 343af41f78
6 changed files with 63 additions and 27 deletions

59
src/parser/babel_call.rs Normal file
View File

@@ -0,0 +1,59 @@
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::anychar;
use nom::character::complete::space0;
use nom::combinator::consumed;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use nom::InputTake;
use super::keyword::affiliated_keyword;
use super::util::get_name;
use super::OrgSource;
use crate::context::RefContext;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::org_line_ending;
use crate::types::BabelCall;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn babel_call<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, BabelCall<'s>> {
let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?;
let (remaining, (consumed_input, (_, _, parsed_key, _))) =
consumed(tuple((space0, tag("#+"), tag_no_case("call"), tag(":"))))(input)?;
if let Ok((remaining, (_, line_break))) = tuple((space0, org_line_ending))(remaining) {
let source = get_consumed(input, remaining);
return Ok((
remaining,
BabelCall {
source: Into::<&str>::into(source),
name: get_name(&affiliated_keywords),
value: Into::<&str>::into(line_break.take(0)),
},
));
}
let (remaining, _ws) = space0(remaining)?;
let (remaining, parsed_value) =
recognize(many_till(anychar, peek(tuple((space0, org_line_ending)))))(remaining)?;
let (remaining, _ws) = tuple((space0, org_line_ending))(remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
BabelCall {
source: Into::<&str>::into(source),
name: get_name(&affiliated_keywords),
value: Into::<&str>::into(parsed_value),
},
))
}