Skip to content

Commit

Permalink
Example JSON grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Nov 27, 2024
1 parent 5fffea6 commit 2ed698f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/JSON/JSON-ab.ebnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<quoted_string> ::= <quote> ( <unescaped_char> | <escaped_char> )+ <quote>

<escaped_char> ::= <escape> ( '"' | "/" | "b" | "f" | "n" | "r" | "t" | <unicode> | <escape> )
<escaped_literal> ::= <escaped_char> | <escape> "`"
<unescaped_char> ::= <digit> | <letter> | " " | "!" | "#" | "$" | "%" | "&" | "'" | "(" | ")" | "*+" | "," | "-" | "." | "/" | ":" | ";" | "<" | ">" | "?" | "@" | "[" | "]" | "^" | "_" | "`" | "{" | "|" | "}" | "~"
<unescaped_literal> ::= <digit> | <letter> | " " | "!" | "#" | "$" | "%" | "&" | "'" | "(" | ")" | "*+" | "," | "-" | "." | "/" | ":" | ";" | "<" | ">" | "?" | "@" | "[" | "]" | "^" | "_" | "{" | "|" | "}" | "~"

<unicode> ::= "u" <digit> <digit> <digit> <digit>

<escape> ::= "\"
<digit> ::= [0-9]
<letter> ::= [A-Z] | [a-z] | "_"
<quote> ::= '"'

/* The ``json-value`` is any valid JSON value with the one exception that the */
/* ``%x60`` character must be escaped. While it's encouraged that implementations */
/* use any existing JSON parser for this grammar rule (after handling the escaped */
/* literal characters), the grammar rule is shown below for completeness:: */

<json_value> ::= <json_array>
| <json_boolean>
| <json_null>
| <json_number>
| <json_object>
| <json_string>

<json_null> ::= "null"
<json_boolean> ::= "true" | "false"
<json_number> ::= "-"? ( "0" | [1-9] [0-9]* ) ( "." [0-9]+ )? ( "e" ( "-" | "+" ) [0-9]+ )?
<json_array> ::= <ws> "[" ( <ws> <json_value> <ws> ( "," <ws> <json_value> <ws> )* )? "]" <ws>
<json_object> ::= <ws> "{" <ws> ( <member> <ws> ( "," <ws> <member> <ws> )* )? "}" <ws>
<json_string> ::= <quote> ( <unescaped_literal> | <escaped_literal> )* <quote>

<member> ::= <quoted_string> <ws> ":" <ws> <json_value>
<ws> ::= " "*

38 changes: 38 additions & 0 deletions examples/JSON/JSON.ebnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
quoted_string ::= quote ( unescaped_char | escaped_char )+ quote

escaped_char ::= escape ( '"' | "/" | "b" | "f" | "n" | "r" | "t" | unicode | escape )
escaped_literal ::= escaped_char | escape "`"
unescaped_char ::= digit | letter | " " | "!" | "#" | "$" | "%" | "&" | "(" | ")" | "*+" | "," | "-" | "." | "/" | ":" | ";" | "<" | ">" | "?" | "@" | "[" | "]" | "^" | "_" | "`" | "{" | "|" | "}" | "~"
unescaped_literal ::= digit | letter | " " | "!" | "#" | "$" | "%" | "&" | "'" | "(" | ")" | "*+" | "," | "-" | "." | "/" | ":" | ";" | "<" | ">" | "?" | "@" | "[" | "]" | "^" | "_" | "{" | "|" | "}" | "~"

unicode ::= "u" digit digit digit digit

escape ::= "\"

digit ::= [0-9]

letter ::= [A-Z] | [a-z] | "_"
quote ::= '"'

/* The ``json-value`` is any valid JSON value with the one exception that the */
/* ``%x60`` character must be escaped. While it's encouraged that implementations */
/* use any existing JSON parser for this grammar rule (after handling the escaped */
/* literal characters), the grammar rule is shown below for completeness:: */

json_value ::= json_array
| json_boolean
| json_null
| json_number
| json_object
| json_string

json_null ::= "null"
json_boolean ::= "true" | "false"
json_number ::= "-"? ( "0" | [1-9] [0-9]* ) ( "." [0-9]+ )? ( "e" ( "-" | "+" ) [0-9]+ )?
json_array ::= ws "[" ( ws json_value ws ( "," ws json_value ws )* )? "]" ws
json_object ::= ws "{" ws ( member ws ( "," ws member ws )* )? "}" ws
json_string ::= quote ( unescaped_literal | escaped_literal )* quote

member ::= quoted_string ws ":" ws json_value
ws ::= " "*

0 comments on commit 2ed698f

Please sign in to comment.