A first stab at lesser blocks.

This commit is contained in:
Tom Alexander 2023-04-21 16:57:49 -04:00
parent 0293ca6b12
commit 47844caa5c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 97 additions and 2 deletions

View File

@ -37,7 +37,6 @@ pub fn greater_block<'r, 's>(
// TODO: Do I need to differentiate between different greater block types.
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
// TODO: Not handling indentation before start of block
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &str| match name.to_lowercase().as_str() {

View File

@ -1,11 +1,107 @@
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;
use nom::combinator::verify;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::error::Res;
use super::Context;
use crate::parser::exiting::ExitClass;
use crate::parser::lesser_element::LesserBlock;
use crate::parser::object_parser::standard_set_object;
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::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::parser::util::start_of_line;
#[tracing::instrument(ret, level = "debug")]
pub fn lesser_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, LesserBlock<'s>> {
todo!()
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &str| match name.to_lowercase().as_str() {
"comment" | "example" | "export" | "src" | "verse" => true,
_ => false,
}),
))(remaining)?;
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let object_matcher = parser_with_context!(standard_set_object)(&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) {
Ok((remaining, (whitespace, (_children, _exit_contents)))) => todo!(),
Err(_) => {
let (remaining, (children, _exit_contents)) =
many_till(object_matcher, exit_matcher)(remaining)?;
(remaining, children)
}
};
let (remaining, _end) = lesser_block_end(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
LesserBlock {
source,
name,
data: parameters,
children,
},
))
}
#[tracing::instrument(ret, level = "debug")]
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not(" \t\r\n")(input)
}
#[tracing::instrument(ret, level = "debug")]
fn data<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not("\r\n")(input)
}
#[tracing::instrument(ret, level = "debug")]
fn lesser_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
start_of_line(context, input)?;
let current_name = "comment"; // TODO
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))
}