organic/src/parser/greater_block.rs

138 lines
4.9 KiB
Rust
Raw Normal View History

use super::Context;
2023-04-21 22:36:01 +00:00
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
2023-04-21 20:10:56 +00:00
use crate::parser::element_parser::element;
use crate::parser::exiting::ExitClass;
use crate::parser::greater_element::GreaterBlock;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ExitMatcherNode;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::blank_line;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::immediate_in_section;
2023-04-22 03:55:18 +00:00
use crate::parser::util::start_of_line;
use crate::parser::Element;
use crate::parser::Paragraph;
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::character::complete::space1;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::opt;
2023-04-03 22:38:35 +00:00
use nom::combinator::verify;
use nom::multi::many_till;
use nom::sequence::tuple;
2023-04-03 21:36:56 +00:00
2023-04-03 22:38:35 +00:00
#[tracing::instrument(ret, level = "debug")]
pub fn greater_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, GreaterBlock<'s>> {
// TODO: Do I need to differentiate between different greater block types.
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
2023-04-03 22:38:35 +00:00
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &str| match name.to_lowercase().as_str() {
"comment" | "example" | "export" | "src" | "verse" => false,
_ => true,
}),
))(remaining)?;
let context_name = match name.to_lowercase().as_str() {
"center" => "center block",
"quote" => "quote block",
_ => "greater block",
};
if immediate_in_section(context, context_name) {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element",
))));
}
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context(context_name))
.with_additional_node(ContextElement::GreaterBlock(name))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Alpha,
exit_matcher: &greater_block_end,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
// Check for a completely empty block
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {
2023-04-19 17:51:18 +00:00
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)
}
};
let (remaining, _end) = greater_block_end(&parser_context, remaining)?;
// Not checking if parent exit matcher is causing exit because the greater_block_end matcher asserts we matched a full greater block
let source = get_consumed(input, remaining);
Ok((
remaining,
GreaterBlock {
source,
name,
parameters,
children,
},
))
}
2023-04-03 22:38:35 +00:00
#[tracing::instrument(ret, level = "debug")]
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not(" \t\r\n")(input)
}
2023-04-03 22:38:35 +00:00
#[tracing::instrument(ret, level = "debug")]
fn parameters<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not("\r\n")(input)
}
#[tracing::instrument(ret, level = "debug")]
fn greater_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
start_of_line(context, input)?;
let current_name: &str = get_context_greater_block_name(context).ok_or(nom::Err::Error(
CustomError::MyError(MyError("Not inside a greater block")),
))?;
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, _name, _ws)) = tuple((
tag_no_case("#+end_"),
tag_no_case(current_name),
alt((eof, line_ending)),
))(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, source))
}
fn get_context_greater_block_name<'r, 's>(context: Context<'r, 's>) -> Option<&'s str> {
for thing in context.iter() {
match thing.get_data() {
ContextElement::GreaterBlock(name) => return Some(name),
_ => {}
};
}
None
}