118 lines
4.0 KiB
Rust
118 lines
4.0 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::combinator::verify;
|
|
use nom::multi::many_till;
|
|
|
|
use super::object_parser::regular_link_description_object_set;
|
|
use super::org_source::OrgSource;
|
|
use super::util::exit_matcher_parser;
|
|
use super::util::get_consumed;
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
|
use crate::context::parser_with_context;
|
|
use crate::context::ContextElement;
|
|
use crate::context::ExitClass;
|
|
use crate::context::ExitMatcherNode;
|
|
use crate::context::RefContext;
|
|
use crate::error::Res;
|
|
use crate::types::Object;
|
|
use crate::types::RegularLink;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn regular_link<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, '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<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, '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) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, 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<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, '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) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, 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<'b, 'g, 'r, 's>(
|
|
_context: RefContext<'b, 'g, '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<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Beta,
|
|
exit_matcher: &description_end,
|
|
});
|
|
let parser_context = context.with_additional_node(&parser_context);
|
|
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<'b, 'g, 'r, 's>(
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
tag("]]")(input)
|
|
}
|