Do not allow elements to immediately nest inside themselves.

Plain list is omitted because they can nest.
This commit is contained in:
Tom Alexander
2023-04-18 22:10:44 -04:00
parent 3a3cee337c
commit 45d0ce17c3
6 changed files with 52 additions and 6 deletions

View File

@@ -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)?;