194 lines
7.4 KiB
Rust
194 lines
7.4 KiB
Rust
use super::error::CustomError;
|
|
use super::error::MyError;
|
|
use super::error::Res;
|
|
use super::greater_element::PlainList;
|
|
use super::greater_element::PlainListItem;
|
|
use super::parser_with_context::parser_with_context;
|
|
use super::util::non_whitespace_character;
|
|
use super::Context;
|
|
use crate::parser::element::element;
|
|
use crate::parser::parser_context::ChainBehavior;
|
|
use crate::parser::parser_context::ContextElement;
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
use crate::parser::util::exit_matcher_parser;
|
|
use crate::parser::util::get_consumed;
|
|
use crate::parser::util::regurgitate;
|
|
use crate::parser::util::start_of_line;
|
|
use crate::parser::util::trailing_whitespace;
|
|
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::character::complete::digit1;
|
|
use nom::character::complete::line_ending;
|
|
use nom::character::complete::one_of;
|
|
use nom::character::complete::space0;
|
|
use nom::combinator::eof;
|
|
use nom::combinator::recognize;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many0;
|
|
use nom::multi::many_till;
|
|
use nom::sequence::tuple;
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> {
|
|
let (remaining, first_item) = plain_list_item(context, input)?;
|
|
let plain_list_item_matcher = parser_with_context!(plain_list_item)(context);
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(context);
|
|
let (remaining, (mut children, _exit_contents)) = many_till(
|
|
verify(plain_list_item_matcher, |pli| {
|
|
pli.indentation == first_item.indentation
|
|
}),
|
|
exit_matcher,
|
|
)(remaining)?;
|
|
let (remaining, _trailing_whitespace) = trailing_whitespace(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
children.insert(0, first_item);
|
|
Ok((remaining, PlainList { source, children }))
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
pub fn plain_list_item<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: &'s str,
|
|
) -> Res<&'s str, PlainListItem<'s>> {
|
|
start_of_line(context, input)?;
|
|
let (remaining, leading_whitespace) = space0(input)?;
|
|
// It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09)
|
|
let indent_level = leading_whitespace.len();
|
|
let parser_context = context
|
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
exit_matcher: ChainBehavior::AndParent(Some(&plain_list_item_end)),
|
|
}))
|
|
.with_additional_node(ContextElement::ListItem(indent_level));
|
|
|
|
let element_matcher = parser_with_context!(element)(&parser_context);
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
|
let (remaining, (bull, _ws)) = tuple((bullet, space0))(remaining)?;
|
|
let (remaining, (contents, _exit_contents)) = many_till(element_matcher, |i| {
|
|
let with_whitespace_added_back = regurgitate(input, i);
|
|
exit_matcher(with_whitespace_added_back)
|
|
})(remaining)?;
|
|
let remaining = regurgitate(input, remaining);
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
remaining,
|
|
PlainListItem {
|
|
source,
|
|
indentation: indent_level,
|
|
bullet: bull,
|
|
contents,
|
|
},
|
|
))
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> {
|
|
alt((
|
|
tag("*"),
|
|
tag("-"),
|
|
tag("+"),
|
|
recognize(tuple((counter, alt((tag("."), tag(")")))))),
|
|
))(i)
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
fn counter<'s>(i: &'s str) -> Res<&'s str, &'s str> {
|
|
alt((recognize(one_of("abcdefghijklmnopqrstuvwxyz")), digit1))(i)
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
let plain_list_item_matcher = parser_with_context!(plain_list_item)(context);
|
|
let line_indented_lte_matcher = parser_with_context!(line_indented_lte)(context);
|
|
alt((
|
|
recognize(tuple((line_ending, plain_list_item_matcher))),
|
|
recognize(tuple((line_ending, line_indented_lte_matcher))),
|
|
eof,
|
|
))(input)
|
|
}
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
fn line_indented_lte<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
let current_item_indent_level: &usize =
|
|
get_context_item_indent(context).ok_or(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Not inside a plain list item",
|
|
))))?;
|
|
|
|
start_of_line(context, input)?;
|
|
|
|
let matched = recognize(verify(
|
|
tuple((space0::<&str, _>, non_whitespace_character)),
|
|
// It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09)
|
|
|(_space0, _anychar)| _space0.len() <= *current_item_indent_level,
|
|
))(input)?;
|
|
|
|
Ok(matched)
|
|
}
|
|
|
|
fn get_context_item_indent<'r, 's>(context: Context<'r, 's>) -> Option<&'r usize> {
|
|
for thing in context.iter() {
|
|
match thing.get_data() {
|
|
ContextElement::ListItem(depth) => return Some(depth),
|
|
_ => {}
|
|
};
|
|
}
|
|
None
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::parser::parser_context::ContextElement;
|
|
use crate::parser::parser_context::ContextTree;
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn plain_list_item_empty() {
|
|
let input = "1.";
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
let document_context =
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context);
|
|
let (remaining, result) = plain_list_item_matcher(input).unwrap();
|
|
assert_eq!(remaining, "");
|
|
assert_eq!(result.source, "1.");
|
|
}
|
|
|
|
#[test]
|
|
fn plain_list_item_simple() {
|
|
let input = "1. foo";
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
let document_context =
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context);
|
|
let (remaining, result) = plain_list_item_matcher(input).unwrap();
|
|
assert_eq!(remaining, "");
|
|
assert_eq!(result.source, "1. foo");
|
|
}
|
|
|
|
#[test]
|
|
fn plain_list_empty() {
|
|
let input = "1.";
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
let document_context =
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
let plain_list_matcher = parser_with_context!(plain_list)(&document_context);
|
|
let (remaining, result) = plain_list_matcher(input).unwrap();
|
|
assert_eq!(remaining, "");
|
|
assert_eq!(result.source, "1.");
|
|
}
|
|
|
|
#[test]
|
|
fn plain_list_simple() {
|
|
let input = "1. foo";
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
let document_context =
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
let plain_list_matcher = parser_with_context!(plain_list)(&document_context);
|
|
let (remaining, result) = plain_list_matcher(input).unwrap();
|
|
assert_eq!(remaining, "");
|
|
assert_eq!(result.source, "1. foo");
|
|
}
|
|
}
|