From 6586fb6706702f776137f591a5bf623efa9c3819 Mon Sep 17 00:00:00 2001 From: Ewan Redfield <29620734+chryoung@users.noreply.github.com> Date: Tue, 22 Mar 2022 18:26:04 +0800 Subject: [PATCH 1/2] Ignore single line comment --- src/jsonparser.rs | 4 ++++ src/jsontokenizer.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/jsonparser.rs b/src/jsonparser.rs index 9ccf937..6a5696d 100644 --- a/src/jsonparser.rs +++ b/src/jsonparser.rs @@ -128,6 +128,10 @@ impl<'a> JsonParser<'a> { panic!("Should have just consumed whitespace"); } + JsonToken::Comment => { + panic!("Should have just consumed comment"); + } + JsonToken::Error => { return Err("Parse error".to_string()); } diff --git a/src/jsontokenizer.rs b/src/jsontokenizer.rs index e35e708..5dd607a 100644 --- a/src/jsontokenizer.rs +++ b/src/jsontokenizer.rs @@ -35,6 +35,9 @@ pub enum JsonToken { #[regex("[ \t\r]+", logos::skip)] Whitespace, + #[regex("//[^\n]*\n?", logos::skip)] + Comment, + #[error] Error, } From 16588f9732250ee7c8262684c466dd6fb0d56080 Mon Sep 17 00:00:00 2001 From: Ewan Redfield <29620734+chryoung@users.noreply.github.com> Date: Tue, 22 Mar 2022 18:47:45 +0800 Subject: [PATCH 2/2] Add unit test --- src/jsonparser.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/jsonparser.rs b/src/jsonparser.rs index 6a5696d..04c24bd 100644 --- a/src/jsonparser.rs +++ b/src/jsonparser.rs @@ -500,4 +500,14 @@ mod tests { assert_eq!(rows[7].range, 46..51); // false assert_eq!(rows[8].range, 51..52); // ] } + + #[test] + fn test_parsing_json_with_comments() { + let json = r#"// This is a JSON with comments file +{ + "a": 1 // This is a comment +}"#.to_owned(); + let json_object = parse(json); + assert!(json_object.is_ok()); + } }