116 lines
3.8 KiB
Rust
116 lines
3.8 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::escaped;
|
|
use nom::bytes::complete::tag;
|
|
use nom::bytes::complete::take_till1;
|
|
use nom::character::complete::one_of;
|
|
use nom::character::complete::space0;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many_till;
|
|
|
|
use super::org_source::OrgSource;
|
|
use super::parser_with_context::parser_with_context;
|
|
use super::util::get_consumed;
|
|
use super::Context;
|
|
use super::Object;
|
|
use super::RegularLink;
|
|
use crate::error::Res;
|
|
use crate::parser::exiting::ExitClass;
|
|
use crate::parser::object_parser::regular_link_description_object_set;
|
|
use crate::parser::parser_context::ContextElement;
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
use crate::parser::util::exit_matcher_parser;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn regular_link<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
|
alt((
|
|
parser_with_context!(regular_link_without_description)(context),
|
|
parser_with_context!(regular_link_with_description)(context),
|
|
))(input)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn regular_link_without_description<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
|
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
|
let (remaining, _path) = pathreg(context, remaining)?;
|
|
let (remaining, _closing_bracket) = tag("]]")(remaining)?;
|
|
let (remaining, _trailing_whitespace) = space0(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
RegularLink {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn regular_link_with_description<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
|
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
|
let (remaining, _path) = pathreg(context, remaining)?;
|
|
let (remaining, _closing_bracket) = tag("][")(remaining)?;
|
|
let (remaining, _description) = description(context, remaining)?;
|
|
let (remaining, _closing_bracket) = tag("]]")(remaining)?;
|
|
let (remaining, _trailing_whitespace) = space0(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
RegularLink {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn pathreg<'r, 's>(
|
|
_context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let (remaining, path) = escaped(
|
|
take_till1(|c| match c {
|
|
'\\' | ']' => true,
|
|
_ => false,
|
|
}),
|
|
'\\',
|
|
one_of(r#"]"#),
|
|
)(input)?;
|
|
Ok((remaining, path))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn description<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
|
let parser_context =
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Beta,
|
|
exit_matcher: &description_end,
|
|
}));
|
|
let (remaining, (children, _exit_contents)) = verify(
|
|
many_till(
|
|
parser_with_context!(regular_link_description_object_set)(&parser_context),
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
),
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
)(input)?;
|
|
|
|
Ok((remaining, children))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn description_end<'r, 's>(
|
|
_context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
tag("]]")(input)
|
|
}
|