organic/src/parser/angle_link.rs

64 lines
2.1 KiB
Rust
Raw Normal View History

2023-07-14 03:02:12 +00:00
use nom::bytes::complete::tag;
use nom::character::complete::anychar;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many_till;
use super::org_source::OrgSource;
2023-07-14 02:42:42 +00:00
use super::Context;
use crate::error::Res;
2023-07-14 03:02:12 +00:00
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::plain_link::protocol;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
2023-07-14 02:42:42 +00:00
use crate::parser::AngleLink;
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn angle_link<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, AngleLink<'s>> {
2023-07-14 03:02:12 +00:00
let (remaining, _) = tag("<")(input)?;
let (remaining, proto) = protocol(context, remaining)?;
let (remaining, _separator) = tag(":")(remaining)?;
let (remaining, path) = path_angle(context, remaining)?;
let (remaining, _) = tag(">")(remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
AngleLink {
source: source.into(),
link_type: proto.into(),
path: path.into(),
2023-07-14 03:02:12 +00:00
},
))
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn path_angle<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
2023-07-14 03:02:12 +00:00
let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
2023-07-14 03:02:12 +00:00
exit_matcher: &path_angle_end,
}));
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, path) = recognize(many_till(anychar, peek(exit_matcher)))(input)?;
Ok((remaining, path))
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn path_angle_end<'r, 's>(
2023-08-24 23:43:41 +00:00
_context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
2023-07-14 03:02:12 +00:00
tag(">")(input)
2023-07-14 02:42:42 +00:00
}