diff --git a/src/parser/comment.rs b/src/parser/comment.rs index 34c2ee0a..92f79f6b 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 c126af32..44871e78 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 307b04bc..6f4fa124 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 f83d0416..2061e489 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 c3505295..afb641b0 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; diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index f4740225..a1825faf 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, })); diff --git a/toy_language.txt b/toy_language.txt index f4dc9de6..8b8048b6 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