2023-03-23 23:35:32 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2023-03-23 23:53:20 +00:00
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space1;
|
2023-03-23 23:35:32 +00:00
|
|
|
use nom::combinator::eof;
|
2023-03-24 21:19:46 +00:00
|
|
|
use nom::combinator::map;
|
2023-03-24 00:12:42 +00:00
|
|
|
use nom::combinator::not;
|
2023-03-23 23:35:32 +00:00
|
|
|
use nom::combinator::recognize;
|
2023-03-24 21:19:46 +00:00
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many0;
|
2023-03-24 00:00:35 +00:00
|
|
|
use nom::multi::many1;
|
2023-03-23 23:35:32 +00:00
|
|
|
use nom::multi::many1_count;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-03-24 21:00:27 +00:00
|
|
|
use crate::parser::element::element;
|
2023-03-23 23:35:32 +00:00
|
|
|
use crate::parser::error::CustomError;
|
|
|
|
use crate::parser::error::MyError;
|
2023-03-23 23:53:20 +00:00
|
|
|
use crate::parser::object::standard_set_object;
|
2023-03-23 23:35:32 +00:00
|
|
|
use crate::parser::parser_context::ChainBehavior;
|
2023-03-23 21:59:39 +00:00
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ContextTree;
|
2023-03-23 23:35:32 +00:00
|
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
2023-03-23 21:59:39 +00:00
|
|
|
|
2023-03-23 21:51:49 +00:00
|
|
|
use super::element::Element;
|
2023-03-23 21:59:39 +00:00
|
|
|
use super::error::Res;
|
2023-03-24 00:03:45 +00:00
|
|
|
use super::object::Object;
|
2023-03-23 23:35:32 +00:00
|
|
|
use super::parser_with_context::parser_with_context;
|
2023-03-23 21:51:49 +00:00
|
|
|
use super::source::Source;
|
2023-03-23 23:35:32 +00:00
|
|
|
use super::Context;
|
2022-10-15 00:17:48 +00:00
|
|
|
|
2023-03-23 21:51:49 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Document<'s> {
|
|
|
|
pub source: &'s str,
|
|
|
|
pub zeroth_section: Option<Section<'s>>,
|
|
|
|
pub children: Vec<Heading<'s>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Heading<'s> {
|
|
|
|
pub source: &'s str,
|
2023-03-24 21:19:46 +00:00
|
|
|
pub stars: usize,
|
2023-03-23 21:51:49 +00:00
|
|
|
pub children: Vec<DocumentElement<'s>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Section<'s> {
|
|
|
|
pub source: &'s str,
|
|
|
|
pub children: Vec<Element<'s>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DocumentElement<'s> {
|
|
|
|
Heading(Heading<'s>),
|
|
|
|
Section(Section<'s>),
|
|
|
|
}
|
2022-12-04 03:44:53 +00:00
|
|
|
|
2023-03-23 21:51:49 +00:00
|
|
|
impl<'s> Source<'s> for Document<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 09:22:28 +00:00
|
|
|
|
2023-03-23 21:51:49 +00:00
|
|
|
impl<'s> Source<'s> for DocumentElement<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
|
|
|
match self {
|
|
|
|
DocumentElement::Heading(obj) => obj.source,
|
|
|
|
DocumentElement::Section(obj) => obj.source,
|
|
|
|
}
|
|
|
|
}
|
2022-11-27 00:14:19 +00:00
|
|
|
}
|
2023-03-23 21:59:39 +00:00
|
|
|
|
2023-03-24 00:12:42 +00:00
|
|
|
#[allow(dead_code)]
|
2023-03-23 21:59:39 +00:00
|
|
|
pub fn document(input: &str) -> Res<&str, Document> {
|
|
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
|
|
let document_context =
|
|
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
|
|
|
|
|
|
todo!()
|
|
|
|
}
|
2023-03-23 23:35:32 +00:00
|
|
|
|
|
|
|
fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Section<'s>> {
|
|
|
|
// TODO: The zeroth section is specialized so it probably needs its own parser
|
|
|
|
let parser_context = context
|
|
|
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
exit_matcher: ChainBehavior::AndParent(Some(§ion_end)),
|
|
|
|
}))
|
|
|
|
.with_additional_node(ContextElement::Context("section"));
|
2023-03-24 00:12:42 +00:00
|
|
|
not(|i| parser_context.check_exit_matcher(i))(input)?;
|
2023-03-24 21:00:27 +00:00
|
|
|
let element_matcher = parser_with_context!(element)(&parser_context);
|
|
|
|
let (remaining, children) = many1(element_matcher)(input)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, Section { source, children }))
|
2023-03-23 23:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
let heading_matcher = parser_with_context!(heading)(context);
|
|
|
|
alt((recognize(heading_matcher), eof))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> {
|
2023-03-24 00:12:42 +00:00
|
|
|
not(|i| context.check_exit_matcher(i))(input)?;
|
|
|
|
let (remaining, (star_count, _ws, title, _ws2)) = headline(context, input)?;
|
2023-03-24 21:19:46 +00:00
|
|
|
let section_matcher = parser_with_context!(section)(context);
|
|
|
|
// TODO: This needs to only match headings below the current level
|
|
|
|
let heading_matcher = parser_with_context!(heading)(context);
|
|
|
|
let (remaining, children) = many0(alt((
|
|
|
|
map(
|
|
|
|
verify(heading_matcher, |h| h.stars > star_count),
|
|
|
|
DocumentElement::Heading,
|
|
|
|
),
|
|
|
|
map(section_matcher, DocumentElement::Section),
|
|
|
|
)))(remaining)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Heading {
|
|
|
|
source: source,
|
|
|
|
stars: star_count,
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
))
|
2023-03-23 23:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 00:03:45 +00:00
|
|
|
fn headline<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, (usize, &'s str, Vec<Object<'s>>, &'s str)> {
|
2023-03-23 23:53:20 +00:00
|
|
|
let parser_context =
|
|
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
exit_matcher: ChainBehavior::AndParent(Some(&headline_end)),
|
|
|
|
}));
|
2023-03-24 00:00:35 +00:00
|
|
|
let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context);
|
2023-03-24 20:37:34 +00:00
|
|
|
let start_of_line_matcher = parser_with_context!(start_of_line)(&parser_context);
|
2023-03-23 23:53:20 +00:00
|
|
|
|
2023-03-24 20:37:34 +00:00
|
|
|
let (remaining, (_sol, star_count, ws, title, ws2)) = tuple((
|
|
|
|
start_of_line_matcher,
|
2023-03-23 23:35:32 +00:00
|
|
|
many1_count(tag("*")),
|
2023-03-23 23:53:20 +00:00
|
|
|
space1,
|
2023-03-24 00:00:35 +00:00
|
|
|
many1(standard_set_object_matcher),
|
|
|
|
alt((line_ending, eof)),
|
2023-03-24 20:37:34 +00:00
|
|
|
))(input)?;
|
|
|
|
Ok((remaining, (star_count, ws, title, ws2)))
|
2023-03-23 23:35:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 23:53:20 +00:00
|
|
|
fn headline_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
line_ending(input)
|
|
|
|
}
|
|
|
|
|
2023-03-24 20:37:34 +00:00
|
|
|
/// Check that we are at the start of a line
|
|
|
|
fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> {
|
|
|
|
let document_root = context.get_document_root().unwrap();
|
|
|
|
let preceding_character = get_one_before(document_root, input)
|
|
|
|
.map(|slice| slice.chars().next())
|
|
|
|
.flatten();
|
|
|
|
match preceding_character {
|
|
|
|
Some('\n') => {}
|
|
|
|
Some(_) => {
|
|
|
|
// Not at start of line, cannot be a heading
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"Not at start of line",
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
// If None, we are at the start of the file which allows for headings
|
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
Ok((input, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get one character from before the current position.
|
2023-03-23 23:35:32 +00:00
|
|
|
fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Option<&'s str> {
|
|
|
|
assert!(is_slice_of(document, current_position));
|
|
|
|
if document.as_ptr() as usize == current_position.as_ptr() as usize {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let offset = current_position.as_ptr() as usize - document.as_ptr() as usize;
|
|
|
|
let previous_character_offset = document.floor_char_boundary(offset - 1);
|
|
|
|
Some(&document[previous_character_offset..offset])
|
|
|
|
}
|
|
|
|
|
2023-03-24 20:37:34 +00:00
|
|
|
/// Check if the child string slice is a slice of the parent string slice.
|
2023-03-23 23:35:32 +00:00
|
|
|
fn is_slice_of(parent: &str, child: &str) -> bool {
|
|
|
|
let parent_start = parent.as_ptr() as usize;
|
|
|
|
let parent_end = parent_start + parent.len();
|
|
|
|
let child_start = child.as_ptr() as usize;
|
|
|
|
let child_end = child_start + child.len();
|
|
|
|
child_start >= parent_start && child_end <= parent_end
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:00:27 +00:00
|
|
|
/// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser.
|
|
|
|
fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str {
|
|
|
|
assert!(is_slice_of(input, remaining));
|
|
|
|
let source = {
|
|
|
|
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
|
|
|
&input[..offset]
|
|
|
|
};
|
|
|
|
source
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:35:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_one_before_unicode() {
|
|
|
|
let input = "🧡💛💚💙💜";
|
|
|
|
let (green_heart_index, _) = input.char_indices().skip(2).next().unwrap();
|
|
|
|
let starting_with_green_heart = &input[green_heart_index..];
|
|
|
|
let yellow_heart = get_one_before(input, starting_with_green_heart).unwrap();
|
|
|
|
assert!(is_slice_of(input, yellow_heart));
|
|
|
|
assert_eq!(yellow_heart, "💛");
|
|
|
|
}
|
|
|
|
}
|