2023-07-19 04:37:51 +00:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::character::complete::anychar;
|
|
|
|
use nom::combinator::opt;
|
|
|
|
use nom::combinator::recognize;
|
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many1;
|
|
|
|
use nom::multi::many_till;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-08-23 04:30:26 +00:00
|
|
|
use super::org_source::OrgSource;
|
2023-08-31 19:44:44 +00:00
|
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
2023-09-03 16:07:51 +00:00
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::ContextElement;
|
|
|
|
use crate::context::ExitClass;
|
|
|
|
use crate::context::ExitMatcherNode;
|
|
|
|
use crate::context::RefContext;
|
2023-07-19 04:09:16 +00:00
|
|
|
use crate::error::Res;
|
2023-07-19 04:37:51 +00:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
|
|
|
use crate::parser::util::get_consumed;
|
2023-09-03 16:07:51 +00:00
|
|
|
use crate::types::ExportSnippet;
|
2023-07-19 04:09:16 +00:00
|
|
|
|
2023-10-09 22:00:48 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(context))
|
|
|
|
)]
|
2023-09-11 17:13:28 +00:00
|
|
|
pub(crate) fn export_snippet<'b, 'g, 'r, 's>(
|
2023-09-03 19:44:18 +00:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, ExportSnippet<'s>> {
|
2023-07-19 04:37:51 +00:00
|
|
|
let (remaining, _) = tag("@@")(input)?;
|
|
|
|
let (remaining, backend_name) = backend(context, remaining)?;
|
2023-09-03 16:07:51 +00:00
|
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
class: ExitClass::Gamma,
|
|
|
|
exit_matcher: &export_snippet_end,
|
|
|
|
});
|
|
|
|
let parser_context = context.with_additional_node(&parser_context);
|
2023-08-14 17:13:32 +00:00
|
|
|
let (remaining, backend_contents) = opt(tuple((
|
|
|
|
tag(":"),
|
|
|
|
parser_with_context!(contents)(&parser_context),
|
|
|
|
)))(remaining)?;
|
2023-07-19 04:37:51 +00:00
|
|
|
let (remaining, _) = tag("@@")(remaining)?;
|
2023-08-31 19:44:44 +00:00
|
|
|
let (remaining, _trailing_whitespace) =
|
|
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-07-19 04:37:51 +00:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
ExportSnippet {
|
2023-08-23 04:30:26 +00:00
|
|
|
source: source.into(),
|
|
|
|
backend: backend_name.into(),
|
|
|
|
contents: backend_contents.map(|(_colon, backend_contents)| backend_contents.into()),
|
2023-07-19 04:37:51 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-10-09 22:00:48 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(_context))
|
|
|
|
)]
|
2023-09-03 19:44:18 +00:00
|
|
|
fn backend<'b, 'g, 'r, 's>(
|
|
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-07-19 04:37:51 +00:00
|
|
|
let (remaining, backend_name) =
|
|
|
|
recognize(many1(verify(anychar, |c| c.is_alphanumeric() || *c == '-')))(input)?;
|
|
|
|
|
|
|
|
Ok((remaining, backend_name))
|
|
|
|
}
|
|
|
|
|
2023-10-09 22:00:48 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(context))
|
|
|
|
)]
|
2023-09-03 19:44:18 +00:00
|
|
|
fn contents<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-07-19 04:37:51 +00:00
|
|
|
let (remaining, source) = recognize(verify(
|
2023-08-14 17:13:32 +00:00
|
|
|
many_till(anychar, parser_with_context!(exit_matcher_parser)(context)),
|
2023-07-19 04:37:51 +00:00
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
))(input)?;
|
|
|
|
Ok((remaining, source))
|
2023-07-19 04:09:16 +00:00
|
|
|
}
|
2023-08-14 17:13:32 +00:00
|
|
|
|
2023-10-09 22:00:48 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(_context))
|
|
|
|
)]
|
2023-09-03 19:44:18 +00:00
|
|
|
fn export_snippet_end<'b, 'g, 'r, 's>(
|
|
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-08-14 17:13:32 +00:00
|
|
|
tag("@@")(input)
|
|
|
|
}
|