2023-04-21 18:36:01 -04:00
|
|
|
use crate::error::CustomError;
|
|
|
|
use crate::error::MyError;
|
|
|
|
use crate::error::Res;
|
2023-04-15 17:56:07 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2023-04-19 19:22:23 -04:00
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-04-15 17:56:07 -04:00
|
|
|
use nom::bytes::complete::take_while;
|
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space0;
|
2023-04-19 14:52:31 -04:00
|
|
|
use nom::combinator::consumed;
|
2023-04-15 17:56:07 -04:00
|
|
|
use nom::combinator::eof;
|
|
|
|
use nom::combinator::recognize;
|
|
|
|
use nom::multi::many_till;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-04-15 17:36:07 -04:00
|
|
|
use super::Context;
|
2023-04-21 16:10:56 -04:00
|
|
|
use crate::parser::element_parser::element;
|
2023-04-18 20:33:01 -04:00
|
|
|
use crate::parser::exiting::ExitClass;
|
2023-04-15 17:56:07 -04:00
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-04-19 14:52:31 -04:00
|
|
|
use crate::parser::util::blank_line;
|
2023-04-15 17:56:07 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
|
|
|
use crate::parser::util::get_consumed;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::util::immediate_in_section;
|
2023-04-21 23:55:18 -04:00
|
|
|
|
2023-04-15 17:56:07 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
|
|
|
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
|
2023-04-15 17:36:07 -04:00
|
|
|
use crate::parser::Drawer;
|
2023-04-19 14:52:31 -04:00
|
|
|
use crate::parser::Element;
|
|
|
|
use crate::parser::Paragraph;
|
2023-04-15 17:36:07 -04:00
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> {
|
2023-04-18 22:10:44 -04:00
|
|
|
if immediate_in_section(context, "drawer") {
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"Cannot nest objects of the same element",
|
|
|
|
))));
|
|
|
|
}
|
2023-04-15 17:56:07 -04:00
|
|
|
start_of_line(context, input)?;
|
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
|
|
|
let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple((
|
|
|
|
tag(":"),
|
|
|
|
name,
|
|
|
|
tag(":"),
|
|
|
|
recognize(tuple((space0, line_ending))),
|
|
|
|
))(remaining)?;
|
|
|
|
|
|
|
|
let parser_context = context
|
|
|
|
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
2023-04-18 22:10:44 -04:00
|
|
|
.with_additional_node(ContextElement::Context("drawer"))
|
2023-04-15 17:56:07 -04:00
|
|
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
2023-04-18 20:33:01 -04:00
|
|
|
class: ExitClass::Alpha,
|
|
|
|
exit_matcher: &drawer_end,
|
2023-04-15 17:56:07 -04:00
|
|
|
}));
|
|
|
|
|
2023-04-22 01:08:45 -04:00
|
|
|
let element_matcher = element(true);
|
|
|
|
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
|
2023-04-15 17:56:07 -04:00
|
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
2023-04-19 14:52:31 -04:00
|
|
|
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {
|
|
|
|
Ok((remaining, (whitespace, (_children, _exit_contents)))) => (
|
|
|
|
remaining,
|
|
|
|
vec![Element::Paragraph(Paragraph::of_text(whitespace))],
|
|
|
|
),
|
|
|
|
Err(_) => {
|
|
|
|
let (remaining, (children, _exit_contents)) =
|
|
|
|
many_till(element_matcher, exit_matcher)(remaining)?;
|
|
|
|
(remaining, children)
|
|
|
|
}
|
|
|
|
};
|
2023-04-15 17:56:07 -04:00
|
|
|
let (remaining, _end) = drawer_end(&parser_context, remaining)?;
|
|
|
|
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Drawer {
|
|
|
|
source,
|
|
|
|
name: drawer_name,
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
start_of_line(context, input)?;
|
|
|
|
recognize(tuple((
|
|
|
|
space0,
|
2023-04-19 19:22:23 -04:00
|
|
|
tag_no_case(":end:"),
|
2023-04-15 17:56:07 -04:00
|
|
|
space0,
|
|
|
|
alt((line_ending, eof)),
|
|
|
|
)))(input)
|
2023-04-15 17:36:07 -04:00
|
|
|
}
|