Implement the export snippet parser.
This commit is contained in:
@@ -1,6 +1,19 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::peek;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::Context;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::not_yet_implemented;
|
||||
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::ExportSnippet;
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
@@ -8,6 +21,41 @@ pub fn export_snippet<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, ExportSnippet<'s>> {
|
||||
not_yet_implemented()?;
|
||||
todo!()
|
||||
let (remaining, _) = tag("@@")(input)?;
|
||||
let (remaining, backend_name) = backend(context, remaining)?;
|
||||
let (remaining, backend_contents) =
|
||||
opt(tuple((tag(":"), parser_with_context!(contents)(context))))(remaining)?;
|
||||
let (remaining, _) = tag("@@")(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
ExportSnippet {
|
||||
source,
|
||||
backend: backend_name,
|
||||
contents: backend_contents.map(|(_colon, backend_contents)| backend_contents),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn backend<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, backend_name) =
|
||||
recognize(many1(verify(anychar, |c| c.is_alphanumeric() || *c == '-')))(input)?;
|
||||
|
||||
Ok((remaining, backend_name))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn contents<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, source) = recognize(verify(
|
||||
many_till(
|
||||
anychar,
|
||||
peek(alt((
|
||||
parser_with_context!(exit_matcher_parser)(context),
|
||||
tag("@@"),
|
||||
))),
|
||||
),
|
||||
|(children, _exit_contents)| !children.is_empty(),
|
||||
))(input)?;
|
||||
Ok((remaining, source))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user