2023-04-15 16:59:30 -04:00
|
|
|
use nom::branch::alt;
|
2023-04-15 16:53:58 -04:00
|
|
|
use nom::bytes::complete::is_not;
|
|
|
|
use nom::bytes::complete::tag;
|
2023-04-15 16:59:30 -04:00
|
|
|
use nom::character::complete::line_ending;
|
2023-04-15 16:53:58 -04:00
|
|
|
use nom::character::complete::space0;
|
2023-04-15 17:08:22 -04:00
|
|
|
use nom::character::complete::space1;
|
2023-04-15 16:59:30 -04:00
|
|
|
use nom::combinator::eof;
|
2023-04-15 16:53:58 -04:00
|
|
|
use nom::combinator::not;
|
2023-04-15 16:59:30 -04:00
|
|
|
use nom::combinator::opt;
|
2023-04-15 16:53:58 -04:00
|
|
|
use nom::multi::many0;
|
|
|
|
use nom::sequence::preceded;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
|
|
|
use super::util::get_consumed;
|
2023-04-15 16:34:33 -04:00
|
|
|
use super::Context;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::error::CustomError;
|
|
|
|
use crate::parser::error::MyError;
|
2023-04-15 16:34:33 -04:00
|
|
|
use crate::parser::error::Res;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::parser_context::ContextElement;
|
2023-04-15 16:53:58 -04:00
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::util::immediate_in_section;
|
2023-04-15 16:59:30 -04:00
|
|
|
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
2023-04-15 16:34:33 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
|
|
|
use crate::parser::Comment;
|
2023-04-15 16:31:38 -04:00
|
|
|
|
2023-04-15 16:34:33 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Comment<'s>> {
|
2023-04-18 22:10:44 -04:00
|
|
|
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);
|
2023-04-15 16:53:58 -04:00
|
|
|
let (remaining, first_line) = comment_line_matcher(input)?;
|
|
|
|
let (remaining, remaining_lines) =
|
|
|
|
many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?;
|
2023-04-15 16:59:30 -04:00
|
|
|
|
|
|
|
let (remaining, _trailing_ws) =
|
|
|
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
|
|
|
2023-04-15 16:53:58 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, Comment { source }))
|
2023-04-15 16:34:33 -04:00
|
|
|
}
|
2023-04-15 16:37:25 -04:00
|
|
|
|
2023-04-15 16:53:58 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-04-15 16:37:25 -04:00
|
|
|
fn comment_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
start_of_line(context, input)?;
|
2023-04-15 16:53:58 -04:00
|
|
|
let (remaining, _indent) = space0(input)?;
|
2023-04-15 17:08:22 -04:00
|
|
|
let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) =
|
|
|
|
tuple((tag("#"), opt(tuple((space1, is_not("\r\n")))), alt((line_ending, eof))))(remaining)?;
|
2023-04-15 16:53:58 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
2023-04-15 16:37:25 -04:00
|
|
|
}
|
2023-04-15 17:04:47 -04:00
|
|
|
|
|
|
|
#[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 require_space_after_hash() {
|
|
|
|
let input = "# Comment line
|
|
|
|
#not a comment
|
|
|
|
# Comment again";
|
|
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
|
|
let document_context =
|
|
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
|
|
let comment_matcher =
|
|
|
|
parser_with_context!(comment)(&document_context);
|
|
|
|
let (remaining, first_comment) =
|
|
|
|
comment_matcher(input).expect("Parse first comment");
|
|
|
|
assert_eq!(remaining, r#"#not a comment
|
|
|
|
# Comment again"#);
|
|
|
|
assert_eq!(
|
|
|
|
first_comment.source,
|
|
|
|
"# Comment line
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|