From 3a3cee337cb034b41d1704a00c58b842652ab583 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 18 Apr 2023 21:47:07 -0400 Subject: [PATCH 1/3] To pass this test, we need to not allow nesting of elements directly inside themselves. --- toy_language.txt | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/toy_language.txt b/toy_language.txt index f4dc9de..8b8048b 100644 --- a/toy_language.txt +++ b/toy_language.txt @@ -1,10 +1,5 @@ -* Headline -before -:candle: -inside -** Headline inside the drawer -the +:firstdrawer: +:seconddrawer: +foo +:end: :end: - -drawer -after From 45d0ce17c3077fa968373cb204775011225363f6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 18 Apr 2023 22:10:44 -0400 Subject: [PATCH 2/3] Do not allow elements to immediately nest inside themselves. Plain list is omitted because they can nest. --- src/parser/comment.rs | 14 ++++++++++++-- src/parser/drawer.rs | 9 +++++++++ src/parser/footnote_definition.rs | 9 +++++++++ src/parser/greater_block.rs | 8 ++++++++ src/parser/paragraph.rs | 13 +++++++++++-- src/parser/plain_list.rs | 5 +++-- 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/parser/comment.rs b/src/parser/comment.rs index 34c2ee0..92f79f6 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -13,17 +13,27 @@ use nom::sequence::tuple; use super::util::get_consumed; use super::Context; +use crate::parser::error::CustomError; +use crate::parser::error::MyError; use crate::parser::error::Res; +use crate::parser::parser_context::ContextElement; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; +use crate::parser::util::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; use crate::parser::Comment; #[tracing::instrument(ret, level = "debug")] pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Comment<'s>> { - let comment_line_matcher = parser_with_context!(comment_line)(context); - let exit_matcher = parser_with_context!(exit_matcher_parser)(context); + if immediate_in_section(context, "comment") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } + let parser_context = context.with_additional_node(ContextElement::Context("comment")); + let comment_line_matcher = parser_with_context!(comment_line)(&parser_context); + let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, first_line) = comment_line_matcher(input)?; let (remaining, remaining_lines) = many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?; diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index c126af3..44871e7 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -10,6 +10,8 @@ use nom::sequence::tuple; use super::Context; use crate::parser::element::element; +use crate::parser::error::CustomError; +use crate::parser::error::MyError; use crate::parser::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::parser_context::ContextElement; @@ -17,6 +19,7 @@ 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::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; @@ -24,6 +27,11 @@ use crate::parser::Drawer; #[tracing::instrument(ret, level = "debug")] pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> { + if immediate_in_section(context, "drawer") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple(( @@ -35,6 +43,7 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) + .with_additional_node(ContextElement::Context("drawer")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &drawer_end, diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 307b04b..6f4fa12 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -2,6 +2,8 @@ use super::error::Res; use super::util::WORD_CONSTITUENT_CHARACTERS; use super::Context; use crate::parser::element::element; +use crate::parser::error::CustomError; +use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; use crate::parser::greater_element::FootnoteDefinition; use crate::parser::parser_context::ContextElement; @@ -10,6 +12,7 @@ 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; use crate::parser::util::maybe_consume_trailing_whitespace; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; @@ -30,12 +33,18 @@ pub fn footnote_definition<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, FootnoteDefinition<'s>> { + if immediate_in_section(context, "footnote definition") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } start_of_line(context, input)?; // Cannot be indented. let (remaining, (_lead_in, lbl, _lead_out, _ws)) = tuple((tag_no_case("[fn:"), label, tag("]"), space0))(input)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) + .with_additional_node(ContextElement::Context("footnote definition")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &footnote_definition_end, diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index f83d041..2061e48 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -13,6 +13,7 @@ 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; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; use crate::parser::Element; @@ -35,6 +36,12 @@ 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. + if immediate_in_section(context, "greater block") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; // TODO: Not handling indentation before start of block @@ -49,6 +56,7 @@ pub fn greater_block<'r, 's>( let (remaining, _nl) = line_ending(remaining)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) + .with_additional_node(ContextElement::Context("greater block")) .with_additional_node(ContextElement::GreaterBlock(name)) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index c350529..c0f9611 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -6,12 +6,15 @@ use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; +use crate::parser::error::CustomError; +use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; use crate::parser::object::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::exit_matcher_parser; +use crate::parser::util::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; @@ -24,8 +27,14 @@ use super::Context; #[tracing::instrument(ret, level = "debug")] pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + if immediate_in_section(context, "paragraph") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } + let parser_context = context + .with_additional_node(ContextElement::Context("paragraph")) + .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: ¶graph_end, })); diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index f474022..a1825fa 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -35,8 +35,9 @@ use tracing::span; #[tracing::instrument(ret, level = "debug")] pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let parser_context = context + .with_additional_node(ContextElement::Context("plain list")) + .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &plain_list_end, })); From 400e253cf17323096cc8ceaa6d367c1b0496dcde Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 18 Apr 2023 22:18:21 -0400 Subject: [PATCH 3/3] Don't give paragraph a context since it just contains objects. --- src/parser/paragraph.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index c0f9611..afb641b 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -27,14 +27,8 @@ use super::Context; #[tracing::instrument(ret, level = "debug")] pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> { - if immediate_in_section(context, "paragraph") { - return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", - )))); - } - let parser_context = context - .with_additional_node(ContextElement::Context("paragraph")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let parser_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: ¶graph_end, }));