0014dfc21f
The parser depends on the rematch_target parser which is not yet implemented.
97 lines
3.1 KiB
Rust
97 lines
3.1 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::character::complete::line_ending;
|
|
use nom::character::complete::space0;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many_till;
|
|
|
|
use super::Context;
|
|
use super::Object;
|
|
use crate::error::CustomError;
|
|
use crate::error::MyError;
|
|
use crate::error::Res;
|
|
use crate::parser::exiting::ExitClass;
|
|
use crate::parser::object_parser::minimal_set_object;
|
|
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::RadioLink;
|
|
use crate::parser::RadioTarget;
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
pub fn radio_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, RadioLink<'s>> {
|
|
let radio_targets = context
|
|
.iter()
|
|
.filter_map(|context_element| match context_element.get_data() {
|
|
ContextElement::RadioTarget(targets) => Some(targets),
|
|
_ => None,
|
|
})
|
|
.flatten();
|
|
for radio_target in radio_targets {
|
|
let rematched_target = rematch_target(context, radio_target, input);
|
|
if let Ok((remaining, rematched_target)) = rematched_target {
|
|
let source = get_consumed(input, remaining);
|
|
return Ok((
|
|
remaining,
|
|
RadioLink {
|
|
source,
|
|
children: rematched_target,
|
|
},
|
|
));
|
|
}
|
|
}
|
|
Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"NoRadioLink",
|
|
))))
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
pub fn rematch_target<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
target: &'r Vec<Object<'s>>,
|
|
input: &'s str,
|
|
) -> Res<&'s str, Vec<Object<'s>>> {
|
|
todo!()
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
pub fn radio_target<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: &'s str,
|
|
) -> Res<&'s str, RadioTarget<'s>> {
|
|
let (remaining, _opening) = tag("<<<")(input)?;
|
|
let parser_context =
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Beta,
|
|
exit_matcher: &radio_target_end,
|
|
}));
|
|
|
|
let (remaining, (children, _exit_contents)) = verify(
|
|
many_till(
|
|
parser_with_context!(minimal_set_object)(&parser_context),
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
),
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
)(remaining)?;
|
|
|
|
let (remaining, _closing) = tag(">>>")(remaining)?;
|
|
let (remaining, _trailing_whitespace) = space0(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((remaining, RadioTarget { source, children }))
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
fn radio_target_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
alt((tag("<"), tag(">"), line_ending))(input)
|
|
}
|
|
|
|
pub trait RematchObject {
|
|
fn rematch_object<'r, 's>(
|
|
&self,
|
|
context: Context<'r, 's>,
|
|
input: &'s str,
|
|
) -> Res<&'s str, Object<'s>>;
|
|
}
|