61 lines
2.2 KiB
Rust
61 lines
2.2 KiB
Rust
|
use nom::bytes::complete::tag;
|
||
|
use nom::character::complete::anychar;
|
||
|
use nom::character::complete::one_of;
|
||
|
use nom::character::complete::space0;
|
||
|
use nom::combinator::peek;
|
||
|
use nom::combinator::recognize;
|
||
|
use nom::combinator::verify;
|
||
|
use nom::multi::many_till;
|
||
|
|
||
|
use super::Context;
|
||
|
use crate::error::CustomError;
|
||
|
use crate::error::MyError;
|
||
|
use crate::error::Res;
|
||
|
use crate::parser::exiting::ExitClass;
|
||
|
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::util::get_one_before;
|
||
|
use crate::parser::Target;
|
||
|
|
||
|
#[tracing::instrument(ret, level = "debug")]
|
||
|
pub fn target<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Target<'s>> {
|
||
|
let (remaining, _) = tag("<<")(input)?;
|
||
|
let (remaining, _) = peek(verify(anychar, |c| {
|
||
|
!c.is_whitespace() && !"<>\n".contains(*c)
|
||
|
}))(remaining)?;
|
||
|
|
||
|
let parser_context =
|
||
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||
|
class: ExitClass::Beta,
|
||
|
exit_matcher: &target_end,
|
||
|
}));
|
||
|
let (remaining, body) = recognize(many_till(
|
||
|
anychar,
|
||
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||
|
))(remaining)?;
|
||
|
|
||
|
let document_root = context.get_document_root().unwrap();
|
||
|
let preceding_character = get_one_before(document_root, remaining)
|
||
|
.map(|slice| slice.chars().next())
|
||
|
.flatten()
|
||
|
.expect("We cannot be at the start of the file because we are inside a target.");
|
||
|
if preceding_character.is_whitespace() {
|
||
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||
|
"Targets cannot end with whitespace.",
|
||
|
))));
|
||
|
}
|
||
|
let (remaining, _) = tag(">>")(remaining)?;
|
||
|
let (remaining, _) = space0(remaining)?;
|
||
|
let source = get_consumed(input, remaining);
|
||
|
|
||
|
Ok((remaining, Target { source }))
|
||
|
}
|
||
|
|
||
|
#[tracing::instrument(ret, level = "debug")]
|
||
|
fn target_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||
|
recognize(one_of("<>\n"))(input)
|
||
|
}
|