From 4e460e4a8c66a59227c4347196fbf56d283550e2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 15 Apr 2023 16:59:30 -0400 Subject: [PATCH] Consume line ending in comments. --- src/parser/comment.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/parser/comment.rs b/src/parser/comment.rs index d674c9b7..cb7d093b 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -1,7 +1,11 @@ +use nom::branch::alt; use nom::bytes::complete::is_not; use nom::bytes::complete::tag; +use nom::character::complete::line_ending; use nom::character::complete::space0; +use nom::combinator::eof; use nom::combinator::not; +use nom::combinator::opt; use nom::multi::many0; use nom::sequence::preceded; use nom::sequence::tuple; @@ -11,6 +15,7 @@ use super::Context; use crate::parser::error::Res; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; +use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; use crate::parser::Comment; @@ -21,6 +26,10 @@ pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, let (remaining, first_line) = comment_line_matcher(input)?; let (remaining, remaining_lines) = many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?; + + let (remaining, _trailing_ws) = + maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; + let source = get_consumed(input, remaining); Ok((remaining, Comment { source })) } @@ -29,8 +38,8 @@ pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, fn comment_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { start_of_line(context, input)?; let (remaining, _indent) = space0(input)?; - let (remaining, (_hash, _leading_whitespace, content)) = - tuple((tag("#"), space0, is_not("\r\n")))(remaining)?; + let (remaining, (_hash, _leading_whitespace, _content, _line_ending)) = + tuple((tag("#"), space0, opt(is_not("\r\n")), alt((line_ending, eof))))(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, source)) }