Skip to content

Commit ad2412a

Browse files
authored
Refactor grammar for better alignment with RFC 8259 (#126)
* Refactor grammar for better alignment with RFC 8259 * Update JSON section introductory comments. * Rename "json-quoted-string" to "json-string" (prefixes RFC name). * Rename "quote" to "quotation-mark" (matches RFC name). * Rename "unescaped-literal" to "json-unescaped" (prefixes RFC name). * Rename "escaped-literal" to "json-escaped" (prefixes RFC name). * Move "json-unescaped" and "json-escaped" into the JSON section. * Define JSON rules after their references. * Add JSON rule section headings. * Align JSON rule EBNP punctuation within each section. * Restore unquoted-string alignment
1 parent 579141d commit ad2412a

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

GRAMMAR.ABNF

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,8 @@ literal = "`" json-value "`" ;; # Literal Expressions
659659
;; search({first: a, type: `"mytype"`}, {"a": "b", "c": "d"}) -> {"first": "b", "type": "mytype"}
660660
;; ```
661661

662-
unescaped-literal = %x20-21 / ; space !
663-
%x23-5B / ; # - [
664-
%x5D-5F / ; ] ^ _
665-
%x61-7A / ; a-z
666-
%x7B-10FFFF ; {|}~ ...
667-
escaped-literal = escaped-char / (escape "`")
668662
number = ["-"] 1*digit
669-
digit = %x30-39
663+
digit = %x30-39 ; 0-9
670664
identifier = unquoted-string / quoted-string ;; # Identifiers
671665
;; An identifier is the most basic expression and can be used to extract a single element from a JSON document.
672666
;; The return value for an identifier is the value associated with the identifier. If the identifier does not exist in the
@@ -694,10 +688,10 @@ unquoted-string = (%x41-5A / %x61-7A / %x5F) *( ; A-Za-z_
694688
%x41-5A / ; A-Z
695689
%x5F / ; _
696690
%x61-7A) ; a-z
697-
quoted-string = quote 1*(unescaped-char / escaped-char) quote
691+
quoted-string = quotation-mark 1*(unescaped-char / escaped-char) quotation-mark
698692
unescaped-char = %x20-21 / %x23-5B / %x5D-10FFFF
699-
escape = "\"
700-
quote = %x22 ; Double quote: '"'
693+
escape = %x5C ; \
694+
quotation-mark = %x22 ; "
701695
escaped-char = escape (
702696
%x22 / ; " quotation mark U+0022
703697
%x5C / ; \ reverse solidus U+005C
@@ -709,38 +703,48 @@ escaped-char = escape (
709703
%x74 / ; t tab U+0009
710704
%x75 4HEXDIG ) ; uXXXX U+XXXX
711705

712-
; The ``json-value`` is any valid JSON value with the one exception that the
713-
; ``%x60`` character must be escaped. While it's encouraged that implementations
714-
; use any existing JSON parser for this grammar rule (after handling the escaped
715-
; literal characters), the grammar rule is shown below for completeness::
716-
706+
; `json-value` is any valid JSON value with the one exception that each
707+
; U+0060 GRAVE ACCENT '`' must be escaped with a preceding backslash.
708+
; While implementations are encouraged to use any existing JSON parser for this
709+
; section of the grammar (after handling the escaped characters), a complete
710+
; set of rules derived from RFC 8259 is included below:
717711
json-value = false / null / true / json-object / json-array /
718-
json-number / json-quoted-string
712+
json-number / json-string
713+
ws = *(
714+
%x20 / ; Space
715+
%x09 / ; Horizontal tab
716+
%x0A / ; Line feed or New line
717+
%x0D ) ; Carriage return
718+
; JSON literals
719719
false = %x66.61.6c.73.65 ; false
720720
null = %x6e.75.6c.6c ; null
721721
true = %x74.72.75.65 ; true
722-
json-quoted-string = %x22 *( unescaped-literal / escaped-literal ) %x22
722+
; JSON strings
723+
json-string = quotation-mark *( json-unescaped / json-escaped ) quotation-mark
724+
json-unescaped = %x20-21 / ; space or '!' (precedes U+0022 '"')
725+
%x23-5B / ; '#' through '[' (precedes U+005C '\')
726+
%x5D-5F / ; ']' through '_' (precedes U+0060 '`')
727+
%x61-10FFFF ; 'a' and all following code points
728+
json-escaped = escaped-char / (escape "`")
729+
; JSON arrays
730+
json-array = begin-array [ json-value *( value-separator json-value ) ] end-array
723731
begin-array = ws %x5B ws ; [ left square bracket
724-
begin-object = ws %x7B ws ; { left curly bracket
725732
end-array = ws %x5D ws ; ] right square bracket
726-
end-object = ws %x7D ws ; } right curly bracket
727-
name-separator = ws %x3A ws ; : colon
728733
value-separator = ws %x2C ws ; , comma
729-
ws = *(%x20 / ; Space
730-
%x09 / ; Horizontal tab
731-
%x0A / ; Line feed or New line
732-
%x0D ; Carriage return
733-
)
734-
json-object = begin-object [ member *( value-separator member ) ] end-object
735-
member = json-quoted-string name-separator json-value
736-
json-array = begin-array [ json-value *( value-separator json-value ) ] end-array
737-
json-number = [ minus ] int [ frac ] [ exp ]
738-
decimal-point = %x2E ; .
739-
digit1-9 = %x31-39 ; 1-9
740-
e = %x65 / %x45 ; e E
741-
exp = e [ minus / plus ] 1*digit
742-
frac = decimal-point 1*digit
743-
int = zero / ( digit1-9 *digit )
744-
minus = %x2D ; -
745-
plus = %x2B ; +
746-
zero = %x30 ; 0
734+
; JSON objects
735+
json-object = begin-object [ member *( value-separator member ) ] end-object
736+
begin-object = ws %x7B ws ; { left curly bracket
737+
end-object = ws %x7D ws ; } right curly bracket
738+
member = json-string name-separator json-value
739+
name-separator = ws %x3A ws ; : colon
740+
; JSON numbers
741+
json-number = [ minus ] int [ frac ] [ exp ]
742+
decimal-point = %x2E ; .
743+
digit1-9 = %x31-39 ; 1-9
744+
e = %x65 / %x45 ; e E
745+
exp = e [ minus / plus ] 1*digit
746+
frac = decimal-point 1*digit
747+
int = zero / ( digit1-9 *digit )
748+
minus = %x2D ; -
749+
plus = %x2B ; +
750+
zero = %x30 ; 0

0 commit comments

Comments
 (0)