2023-04-23 16:53:02 -04:00
|
|
|
use nom::branch::alt;
|
2023-04-23 17:37:35 -04:00
|
|
|
use nom::bytes::complete::escaped;
|
2023-04-23 16:53:02 -04:00
|
|
|
use nom::bytes::complete::tag;
|
2023-04-23 17:37:35 -04:00
|
|
|
use nom::bytes::complete::take_till1;
|
2023-09-14 02:58:57 -04:00
|
|
|
use nom::character::complete::anychar;
|
2023-10-06 17:21:43 -04:00
|
|
|
use nom::combinator::consumed;
|
|
|
|
use nom::combinator::eof;
|
2023-10-06 18:56:11 -04:00
|
|
|
use nom::combinator::map;
|
2023-10-06 17:21:43 -04:00
|
|
|
use nom::combinator::map_parser;
|
2023-10-06 18:56:11 -04:00
|
|
|
use nom::combinator::opt;
|
2023-10-06 17:21:43 -04:00
|
|
|
use nom::combinator::peek;
|
|
|
|
use nom::combinator::recognize;
|
|
|
|
use nom::combinator::rest;
|
2023-04-23 16:53:02 -04:00
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many_till;
|
2023-10-06 17:21:43 -04:00
|
|
|
use nom::sequence::tuple;
|
2023-04-24 16:19:29 -04:00
|
|
|
|
2023-09-08 17:45:49 -04:00
|
|
|
use super::object_parser::regular_link_description_set_object;
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-10-06 19:18:58 -04:00
|
|
|
use super::plain_link::protocol;
|
2023-09-03 00:27:50 -04:00
|
|
|
use super::util::exit_matcher_parser;
|
2023-04-23 16:53:02 -04:00
|
|
|
use super::util::get_consumed;
|
2023-08-31 15:44:44 -04:00
|
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
2023-09-03 00:27:50 -04:00
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::ContextElement;
|
|
|
|
use crate::context::ExitClass;
|
|
|
|
use crate::context::ExitMatcherNode;
|
|
|
|
use crate::context::RefContext;
|
2023-04-23 16:12:34 -04:00
|
|
|
use crate::error::Res;
|
2023-10-06 17:21:43 -04:00
|
|
|
use crate::types::LinkType;
|
2023-09-03 00:27:50 -04:00
|
|
|
use crate::types::Object;
|
|
|
|
use crate::types::RegularLink;
|
2023-04-23 16:12:34 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
pub(crate) fn regular_link<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
2023-04-23 16:53:02 -04:00
|
|
|
alt((
|
|
|
|
parser_with_context!(regular_link_without_description)(context),
|
|
|
|
parser_with_context!(regular_link_with_description)(context),
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
fn regular_link_without_description<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
2023-04-23 16:53:02 -04:00
|
|
|
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
2023-10-06 17:21:43 -04:00
|
|
|
let (remaining, path) = pathreg(context, remaining)?;
|
2023-04-23 16:53:02 -04:00
|
|
|
let (remaining, _closing_bracket) = tag("]]")(remaining)?;
|
2023-08-31 15:44:44 -04:00
|
|
|
let (remaining, _trailing_whitespace) =
|
|
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-04-23 16:53:02 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
RegularLink {
|
|
|
|
source: source.into(),
|
2023-10-06 17:21:43 -04:00
|
|
|
link_type: path.link_type,
|
|
|
|
path: path.path,
|
|
|
|
raw_link: path.raw_link,
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: path.search_option,
|
2023-08-23 00:30:26 -04:00
|
|
|
},
|
|
|
|
))
|
2023-04-23 16:53:02 -04:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
fn regular_link_with_description<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
2023-04-23 16:53:02 -04:00
|
|
|
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
2023-10-06 17:21:43 -04:00
|
|
|
let (remaining, path) = pathreg(context, remaining)?;
|
2023-04-23 16:53:02 -04:00
|
|
|
let (remaining, _closing_bracket) = tag("][")(remaining)?;
|
|
|
|
let (remaining, _description) = description(context, remaining)?;
|
|
|
|
let (remaining, _closing_bracket) = tag("]]")(remaining)?;
|
2023-08-31 15:44:44 -04:00
|
|
|
let (remaining, _trailing_whitespace) =
|
|
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-04-23 16:53:02 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
RegularLink {
|
|
|
|
source: source.into(),
|
2023-10-06 17:21:43 -04:00
|
|
|
link_type: path.link_type,
|
|
|
|
path: path.path,
|
|
|
|
raw_link: path.raw_link,
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: path.search_option,
|
2023-08-23 00:30:26 -04:00
|
|
|
},
|
|
|
|
))
|
2023-04-23 16:53:02 -04:00
|
|
|
}
|
|
|
|
|
2023-10-06 17:21:43 -04:00
|
|
|
struct PathReg<'s> {
|
|
|
|
link_type: LinkType<'s>,
|
|
|
|
path: &'s str,
|
|
|
|
raw_link: &'s str,
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: Option<&'s str>,
|
2023-10-06 17:21:43 -04:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
fn pathreg<'b, 'g, 'r, 's>(
|
2023-10-06 19:18:58 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
2023-10-06 17:21:43 -04:00
|
|
|
) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, path) = map_parser(
|
|
|
|
escaped(
|
|
|
|
take_till1(|c| match c {
|
|
|
|
'\\' | '[' | ']' => true,
|
|
|
|
_ => false,
|
|
|
|
}),
|
|
|
|
'\\',
|
|
|
|
anychar,
|
|
|
|
),
|
2023-10-06 19:18:58 -04:00
|
|
|
parser_with_context!(parse_path_reg)(context),
|
2023-04-23 17:37:35 -04:00
|
|
|
)(input)?;
|
|
|
|
Ok((remaining, path))
|
2023-04-23 16:12:34 -04:00
|
|
|
}
|
2023-04-23 16:53:02 -04:00
|
|
|
|
2023-10-06 19:18:58 -04:00
|
|
|
fn parse_path_reg<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, PathReg<'s>> {
|
2023-10-06 17:21:43 -04:00
|
|
|
alt((
|
|
|
|
file_path_reg,
|
|
|
|
id_path_reg,
|
|
|
|
custom_id_path_reg,
|
|
|
|
code_ref_path_reg,
|
2023-10-06 19:18:58 -04:00
|
|
|
parser_with_context!(protocol_path_reg)(context),
|
2023-10-06 17:21:43 -04:00
|
|
|
fuzzy_path_reg,
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
|
2023-10-06 18:56:11 -04:00
|
|
|
let (remaining, (raw_link, (_, path, search_option))) = consumed(tuple((
|
|
|
|
tag("file:"),
|
|
|
|
recognize(many_till(anychar, alt((peek(tag("::")), eof)))),
|
|
|
|
opt(map(tuple((tag("::"), rest)), |(_, search_option)| {
|
|
|
|
search_option
|
|
|
|
})),
|
|
|
|
)))(input)?;
|
2023-10-06 17:21:43 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::File,
|
|
|
|
path: path.into(),
|
|
|
|
raw_link: raw_link.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: search_option.map(Into::<&str>::into),
|
2023-10-06 17:21:43 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, (raw_link, (_, path))) = consumed(tuple((tag("id:"), rest)))(input)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::Id,
|
|
|
|
path: path.into(),
|
|
|
|
raw_link: raw_link.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: None,
|
2023-10-06 17:21:43 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn custom_id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, (raw_link, (_, path))) = consumed(tuple((tag("#"), rest)))(input)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::CustomId,
|
|
|
|
path: path.into(),
|
|
|
|
raw_link: raw_link.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: None,
|
2023-10-06 17:21:43 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn code_ref_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, (raw_link, (_, path, _))) = consumed(tuple((
|
|
|
|
tag("("),
|
|
|
|
recognize(many_till(anychar, peek(tuple((tag(")"), eof))))),
|
|
|
|
tag(")"),
|
|
|
|
)))(input)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::CodeRef,
|
|
|
|
path: path.into(),
|
|
|
|
raw_link: raw_link.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: None,
|
2023-10-06 17:21:43 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-10-06 19:18:58 -04:00
|
|
|
fn protocol_path_reg<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, (raw_link, (protocol, _, path))) = consumed(tuple((
|
|
|
|
parser_with_context!(protocol)(context),
|
|
|
|
tag(":"),
|
|
|
|
rest,
|
|
|
|
)))(input)?;
|
2023-10-06 18:30:08 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::Protocol(protocol.into()),
|
|
|
|
path: path.into(),
|
|
|
|
raw_link: raw_link.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: None,
|
2023-10-06 18:30:08 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-10-06 17:21:43 -04:00
|
|
|
fn fuzzy_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
|
|
|
|
let (remaining, body) = rest(input)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PathReg {
|
|
|
|
link_type: LinkType::Fuzzy,
|
|
|
|
path: body.into(),
|
|
|
|
raw_link: body.into(),
|
2023-10-06 18:56:11 -04:00
|
|
|
search_option: None,
|
2023-10-06 17:21:43 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
fn description<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
2023-09-03 00:27:50 -04:00
|
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
class: ExitClass::Beta,
|
|
|
|
exit_matcher: &description_end,
|
|
|
|
});
|
|
|
|
let parser_context = context.with_additional_node(&parser_context);
|
2023-04-23 16:53:02 -04:00
|
|
|
let (remaining, (children, _exit_contents)) = verify(
|
|
|
|
many_till(
|
2023-09-08 17:45:49 -04:00
|
|
|
parser_with_context!(regular_link_description_set_object)(&parser_context),
|
2023-04-23 16:53:02 -04:00
|
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
|
|
),
|
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
2023-04-24 16:05:46 -04:00
|
|
|
)(input)?;
|
2023-04-23 16:53:02 -04:00
|
|
|
|
|
|
|
Ok((remaining, children))
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn description_end<'b, 'g, 'r, 's>(
|
|
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-04-23 16:53:02 -04:00
|
|
|
tag("]]")(input)
|
|
|
|
}
|