complete JSON grammar

This commit is contained in:
Evan Jones 2023-07-26 21:15:26 -04:00
parent ffb8c87caa
commit 5ef33fbd5c
2 changed files with 11 additions and 12 deletions

View file

@ -9,8 +9,11 @@ SPACE_RULE = '" "?'
PRIMITIVE_RULES = { PRIMITIVE_RULES = {
'boolean': '("true" | "false") space', 'boolean': '("true" | "false") space',
'number': '[0-9]+ space', # TODO complete 'number': '("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? space',
'string': r'"\"" [ \t!#-\[\]-~]* "\"" space', # TODO complete 'string': r''' "\"" (
[^"\\] |
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
)* "\"" space ''',
'null': '"null" space', 'null': '"null" space',
} }

View file

@ -1,29 +1,25 @@
# Grammar for subset of JSON - doesn't support full string or number syntax root ::= object
value ::= object | array | string | number | ("true" | "false" | "null") ws
root ::= object
value ::= object | array | string | number | boolean | "null"
object ::= object ::=
"{" ws ( "{" ws (
string ":" ws value string ":" ws value
("," ws string ":" ws value)* ("," ws string ":" ws value)*
)? "}" )? "}" ws
array ::= array ::=
"[" ws ( "[" ws (
value value
("," ws value)* ("," ws value)*
)? "]" )? "]" ws
string ::= string ::=
"\"" ( "\"" (
[^"\\] | [^"\\] |
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
)* "\"" ws )* "\"" ws
# Only plain integers currently number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
number ::= "-"? [0-9]+ ws
boolean ::= ("true" | "false") ws
# Optional space: by convention, applied in this grammar after literal chars when allowed # Optional space: by convention, applied in this grammar after literal chars when allowed
ws ::= ([ \t\n] ws)? ws ::= ([ \t\n] ws)?