Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows empty one-line comments. #474

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/text/parsers/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::text::parsers::whitespace;

use nom::branch::alt;
use nom::bytes::streaming::{is_not, tag, take_until};
use nom::combinator::recognize;
use nom::character::streaming::one_of;
use nom::combinator::{peek, recognize, value};
use nom::multi::many0_count;
use nom::sequence::{delimited, preceded};

Expand All @@ -28,8 +29,14 @@ fn rest_of_line_comment(input: &str) -> IonParseResult<&str> {
preceded(
// Matches a leading "//"...
tag("//"),
// ...followed by any characters that are not a '\r' or '\n'.
is_not("\r\n"), // Note that this will not consume the newline.
// ...followed by either...
alt((
// ...one or more non-EOL characters...
is_not("\r\n"),
// ...or any EOL character.
value("", peek(recognize(one_of("\r\n")))),
// In either case, the line ending will not be consumed.
)),
)(input)
.upgrade()
}
Expand All @@ -55,6 +62,8 @@ pub(crate) mod comment_parser_tests {
#[rstest]
#[case("//hello!\n", "hello!")]
#[case("//😎 😎 😎\n", "😎 😎 😎")]
#[case("//\n ", "")] // Empty comments are allowed
#[case("//\t \n ", "\t ")] // Whitespace is allowed
#[should_panic]
#[case("// no newline ", "<should panic>")]
fn test_parse_rest_of_line_comment(#[case] text: &str, #[case] expected: &str) {
Expand Down