Skip to content

Commit 5ea76f7

Browse files
committed
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.
1 parent 5efcb36 commit 5ea76f7

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

grammar/JSONC.abnf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ comment = single-line-comment / multi-line-comment
2121
; Single-line comment: starts with //, continues until line ending
2222
; Note that the single-line-comment-end is optional, allowing comments to end at the end of the file without a line terminator.
2323
single-line-comment-start = %x2F.2F ; // double solidus
24-
single-line-comment-end = %x0D.0A / %x0A / %x0D
25-
single-line-comment = single-line-comment-start *single-line-comment-char [ single-line-comment-end ]
26-
single-line-comment-char = %x00-09 / %x0B-0C / %x0E-10FFFF ; Any source character except CR and LF (line terminator)
24+
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.
25+
single-line-comment-char = %x00-09 / %x0B-0C / %x0E-2027 / %x2029-10FFFF ; Any source character except CR, LF, LS and PS (line terminator)
2726

2827
; Multi-line comment: /* ... */
2928
; Cannot be nested. The first */ closes the comment.

grammar/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ npm run railroad -- grammar/JSONC.abnf grammar/railroad-diagram.html --title "JS
6464

6565
### Notes on EOF for single-line comments
6666

67-
The grammar already allows inline comments to terminate at end-of-file because the line terminator is optional:
67+
The grammar already allows inline comments to terminate at end-of-file because the `single-line-comment` rule does not contain the line terminator:
6868

6969
```abnf
70-
single-line-comment = "//" *single-line-comment-char [ comment-terminator ]
70+
single-line-comment = "//" *single-line-comment-char
7171
```
7272

73-
So diagrams generated from this ABNF should not imply a mandatory line ending.
73+
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.
74+
75+
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.

0 commit comments

Comments
 (0)