2023-04-19 17:03:46 -04:00
|
|
|
use nom::branch::alt;
|
2023-04-19 18:10:29 -04:00
|
|
|
use nom::bytes::complete::is_not;
|
2023-04-19 17:03:46 -04:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-08-29 22:03:20 -04:00
|
|
|
use nom::character::complete::anychar;
|
2023-04-19 17:03:46 -04:00
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space0;
|
2023-04-19 18:10:29 -04:00
|
|
|
use nom::character::complete::space1;
|
2023-04-19 17:03:46 -04:00
|
|
|
use nom::combinator::eof;
|
2023-04-19 18:10:29 -04:00
|
|
|
use nom::combinator::opt;
|
2023-04-19 17:03:46 -04:00
|
|
|
use nom::combinator::recognize;
|
2023-08-29 22:03:20 -04:00
|
|
|
use nom::combinator::verify;
|
2023-04-19 17:15:00 -04:00
|
|
|
use nom::multi::many_till;
|
2023-04-19 17:03:46 -04:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-09-03 00:27:50 -04:00
|
|
|
use super::util::exit_matcher_parser;
|
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::ContextElement;
|
|
|
|
use crate::context::ExitClass;
|
|
|
|
use crate::context::ExitMatcherNode;
|
|
|
|
use crate::context::RefContext;
|
2023-04-22 21:45:18 -04:00
|
|
|
use crate::error::CustomError;
|
|
|
|
use crate::error::MyError;
|
|
|
|
use crate::error::Res;
|
2023-04-19 17:15:00 -04:00
|
|
|
use crate::parser::util::get_consumed;
|
2023-04-19 16:54:17 -04:00
|
|
|
use crate::parser::util::immediate_in_section;
|
2023-04-19 17:15:00 -04:00
|
|
|
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
2023-04-19 17:03:46 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
2023-09-03 00:27:50 -04:00
|
|
|
use crate::types::NodeProperty;
|
|
|
|
use crate::types::PropertyDrawer;
|
2023-04-19 16:51:00 -04:00
|
|
|
|
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
|
|
|
pub fn property_drawer<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, PropertyDrawer<'s>> {
|
2023-04-19 16:54:17 -04:00
|
|
|
if immediate_in_section(context, "property-drawer") {
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
2023-08-23 00:30:26 -04:00
|
|
|
"Cannot nest objects of the same element".into(),
|
2023-04-19 16:54:17 -04:00
|
|
|
))));
|
|
|
|
}
|
2023-04-19 17:03:46 -04:00
|
|
|
let (
|
|
|
|
remaining,
|
2023-04-21 18:42:31 -04:00
|
|
|
(_start_of_line, _leading_whitespace, _open_tag, _trailing_whitespace, _line_ending),
|
2023-04-19 17:03:46 -04:00
|
|
|
) = tuple((
|
2023-08-24 19:29:00 -04:00
|
|
|
start_of_line,
|
2023-04-19 17:03:46 -04:00
|
|
|
space0,
|
|
|
|
tag_no_case(":PROPERTIES:"),
|
|
|
|
space0,
|
|
|
|
line_ending,
|
|
|
|
))(input)?;
|
2023-04-19 16:54:17 -04:00
|
|
|
|
2023-09-03 00:27:50 -04:00
|
|
|
let contexts = [
|
|
|
|
ContextElement::ConsumeTrailingWhitespace(true),
|
|
|
|
ContextElement::Context("property-drawer"),
|
|
|
|
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
2023-04-19 17:03:46 -04:00
|
|
|
class: ExitClass::Alpha,
|
|
|
|
exit_matcher: &property_drawer_end,
|
2023-09-03 00:27:50 -04:00
|
|
|
}),
|
|
|
|
];
|
2023-09-03 12:28:45 -04:00
|
|
|
let parser_context = context.with_additional_node(&contexts[0]);
|
|
|
|
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
|
|
|
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
2023-04-19 16:54:17 -04:00
|
|
|
|
2023-04-19 18:10:29 -04:00
|
|
|
let node_property_matcher = parser_with_context!(node_property)(&parser_context);
|
2023-04-19 17:15:00 -04:00
|
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
|
|
|
let (remaining, (children, _exit_contents)) =
|
2023-04-19 18:10:29 -04:00
|
|
|
many_till(node_property_matcher, exit_matcher)(remaining)?;
|
2023-04-19 17:15:00 -04:00
|
|
|
let (remaining, _end) = property_drawer_end(&parser_context, remaining)?;
|
2023-04-19 16:54:17 -04:00
|
|
|
|
2023-04-19 17:15:00 -04:00
|
|
|
let (remaining, _trailing_ws) =
|
|
|
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
2023-04-19 16:54:17 -04:00
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PropertyDrawer {
|
|
|
|
source: source.into(),
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
))
|
2023-04-19 16:51:00 -04:00
|
|
|
}
|
2023-04-19 17:03:46 -04:00
|
|
|
|
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 property_drawer_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-19 17:03:46 -04:00
|
|
|
recognize(tuple((
|
2023-08-24 19:29:00 -04:00
|
|
|
start_of_line,
|
2023-04-19 17:03:46 -04:00
|
|
|
space0,
|
2023-04-19 19:03:51 -04:00
|
|
|
tag_no_case(":end:"),
|
2023-04-19 17:03:46 -04:00
|
|
|
space0,
|
|
|
|
alt((line_ending, eof)),
|
|
|
|
)))(input)
|
|
|
|
}
|
2023-04-19 17:15:00 -04:00
|
|
|
|
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 node_property<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, NodeProperty<'s>> {
|
2023-04-21 18:42:31 -04:00
|
|
|
let (remaining, (_start_of_line, _leading_whitespace, _open_colon, _name, _close_colon)) =
|
2023-04-19 18:10:29 -04:00
|
|
|
tuple((
|
2023-08-24 19:29:00 -04:00
|
|
|
start_of_line,
|
2023-04-19 18:10:29 -04:00
|
|
|
space0,
|
|
|
|
tag(":"),
|
|
|
|
parser_with_context!(node_property_name)(context),
|
|
|
|
tag(":"),
|
|
|
|
))(input)?;
|
2023-08-23 00:30:26 -04:00
|
|
|
match tuple((
|
|
|
|
space0::<OrgSource<'_>, nom::error::Error<OrgSource<'_>>>,
|
|
|
|
line_ending,
|
|
|
|
))(remaining)
|
|
|
|
{
|
2023-04-19 18:10:29 -04:00
|
|
|
Ok((remaining, _ws)) => {
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
NodeProperty {
|
2023-08-23 00:30:26 -04:00
|
|
|
source: source.into(),
|
2023-04-19 18:10:29 -04:00
|
|
|
value: None,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Err(_) => {
|
2023-04-19 19:03:51 -04:00
|
|
|
let (remaining, (_ws, value, _line_ending)) =
|
|
|
|
tuple((space1, is_not("\r\n"), line_ending))(remaining)?;
|
2023-04-19 18:10:29 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
NodeProperty {
|
2023-08-23 00:30:26 -04:00
|
|
|
source: source.into(),
|
|
|
|
value: Some(value.into()),
|
2023-04-19 18:10:29 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 node_property_name<'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-09-03 00:27:50 -04:00
|
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
class: ExitClass::Beta,
|
|
|
|
exit_matcher: &node_property_name_end,
|
|
|
|
});
|
|
|
|
let parser_context = context.with_additional_node(&parser_context);
|
2023-04-19 18:10:29 -04:00
|
|
|
|
|
|
|
let (remaining, name) = recognize(tuple((
|
2023-08-29 22:03:20 -04:00
|
|
|
verify(
|
|
|
|
many_till(
|
|
|
|
anychar,
|
|
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
|
|
),
|
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
),
|
2023-04-19 18:10:29 -04:00
|
|
|
opt(tag("+")),
|
|
|
|
)))(input)?;
|
2023-08-29 22:03:20 -04:00
|
|
|
|
2023-04-19 18:10:29 -04:00
|
|
|
Ok((remaining, name))
|
|
|
|
}
|
|
|
|
|
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 node_property_name_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-09-06 18:41:02 -04:00
|
|
|
recognize(tuple((
|
|
|
|
alt((tag("+:"), tag(":"))),
|
|
|
|
alt((space1, line_ending, eof)),
|
|
|
|
)))(input)
|
2023-04-19 17:15:00 -04:00
|
|
|
}
|