Basic implementation of greater block parser.

This commit is contained in:
Tom Alexander 2023-04-03 18:33:07 -04:00
parent 9e0bea4c3e
commit 6e6ec56d1f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 97 additions and 3 deletions

View File

@ -1,4 +1,5 @@
use super::error::Res;
use super::greater_block::greater_block;
use super::greater_element::GreaterBlock;
use super::greater_element::PlainList;
use super::lesser_element::Paragraph;
@ -48,5 +49,9 @@ pub fn non_paragraph_element<'r, 's>(
input: &'s str,
) -> Res<&'s str, Element<'s>> {
let plain_list_matcher = parser_with_context!(plain_list)(context);
map(plain_list_matcher, Element::PlainList)(input)
let greater_block_matcher = parser_with_context!(greater_block)(context);
alt((
map(plain_list_matcher, Element::PlainList),
map(greater_block_matcher, Element::GreaterBlock),
))(input)
}

View File

@ -1 +1,89 @@
use super::error::Res;
use super::Context;
use crate::parser::element::element;
use crate::parser::error::CustomError;
use crate::parser::error::MyError;
use crate::parser::greater_element::GreaterBlock;
use crate::parser::parser_context::ChainBehavior;
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::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
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::space1;
use nom::combinator::eof;
use nom::combinator::opt;
use nom::multi::many_till;
use nom::sequence::tuple;
pub fn greater_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, GreaterBlock<'s>> {
let (remaining, (_begin, name)) = tuple((tag_no_case("#+begin_"), name))(input)?;
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let parser_context = context
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
exit_matcher: ChainBehavior::IgnoreParent(Some(&greater_block_end)),
}))
.with_additional_node(ContextElement::GreaterBlock(name));
let element_matcher = parser_with_context!(element)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, (children, _exit_contents)) =
many_till(element_matcher, exit_matcher)(remaining)?;
let (remaining, _end) = greater_block_end(&parser_context, remaining)?;
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let source = get_consumed(input, remaining);
Ok((
remaining,
GreaterBlock {
source,
name,
parameters,
children,
},
))
}
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not(" \t\r\n")(input)
}
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, (_begin, _name, _ws)) = tuple((
tag_no_case("#+end_"),
tag_no_case(current_name),
alt((eof, line_ending)),
))(input)?;
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
}

View File

@ -128,6 +128,9 @@ pub enum ContextElement<'r, 's> {
/// Stores the indentation level of the current list item
ListItem(usize),
/// Stores the name of the greater block
GreaterBlock(&'s str),
}
#[derive(Debug)]
@ -138,8 +141,6 @@ pub struct ExitMatcherNode<'r> {
#[derive(Clone)]
pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
#[allow(dead_code)] // Will be used when inside code/quote blocks
IgnoreParent(Option<&'r Matcher>),
}