From 5ea76f73d91b06692f421ce931a37622ba95360a Mon Sep 17 00:00:00 2001 From: chirsz-ever Date: Sun, 17 May 2026 17:28:35 +0800 Subject: [PATCH] Update the ABNF rules for single-line comments Specify that greedy mode should be used. Resolve cases where single-line comments end with EOF, LS or PS. --- grammar/JSONC.abnf | 5 ++--- grammar/README.md | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/grammar/JSONC.abnf b/grammar/JSONC.abnf index e89ae02..87f4886 100644 --- a/grammar/JSONC.abnf +++ b/grammar/JSONC.abnf @@ -21,9 +21,8 @@ comment = single-line-comment / multi-line-comment ; Single-line comment: starts with //, continues until line ending ; Note that the single-line-comment-end is optional, allowing comments to end at the end of the file without a line terminator. single-line-comment-start = %x2F.2F ; // double solidus -single-line-comment-end = %x0D.0A / %x0A / %x0D -single-line-comment = single-line-comment-start *single-line-comment-char [ single-line-comment-end ] -single-line-comment-char = %x00-09 / %x0B-0C / %x0E-10FFFF ; Any source character except CR and LF (line terminator) +single-line-comment = single-line-comment-start *single-line-comment-char ; Note that this rule should be greedy to consume all characters until the end of the line or the end of the input. +single-line-comment-char = %x00-09 / %x0B-0C / %x0E-2027 / %x2029-10FFFF ; Any source character except CR, LF, LS and PS (line terminator) ; Multi-line comment: /* ... */ ; Cannot be nested. The first */ closes the comment. diff --git a/grammar/README.md b/grammar/README.md index af7cd7a..0b8c5fb 100644 --- a/grammar/README.md +++ b/grammar/README.md @@ -64,10 +64,12 @@ npm run railroad -- grammar/JSONC.abnf grammar/railroad-diagram.html --title "JS ### Notes on EOF for single-line comments -The grammar already allows inline comments to terminate at end-of-file because the line terminator is optional: +The grammar already allows inline comments to terminate at end-of-file because the `single-line-comment` rule does not contain the line terminator: ```abnf -single-line-comment = "//" *single-line-comment-char [ comment-terminator ] +single-line-comment = "//" *single-line-comment-char ``` -So diagrams generated from this ABNF should not imply a mandatory line ending. \ No newline at end of file +ABNF (RFC 5234) does not specify whether greedy matching should be used for repetitive syntax. However, greedy matching is mandatory in the `single-line-comment` rule to ensure correct behavior. + +Additionally, when LS (U+2028) or PS (U+2029) appears in a single-line comment, it will end the comment and be used as the next input, thus causing parsing failure as an illegal character.