-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.grammar
77 lines (60 loc) · 2.61 KB
/
json.grammar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// based on https://github.com/dimfeld/lezer-json5/blob/3468e6435cfb4ec06da093378faba355cd3fd0ae/src/json5.grammar
// MIT License
// Copyright (c) 2021 Daniel Imfeld
// based on https://github.com/lezer-parser/json/blob/fb6ce500ca64c3345eee09f8bd4566cf65ad0af0/src/json.grammar
// MIT License
// Copyright (C) 2020 by Marijn Haverbeke <[email protected]>, Arun Srinivasan <[email protected]>, and others
@top JsonText { value }
@dialects { json5 }
value[@isGroup=Value] { True | False | Null | Number | String | Object | Array }
String { string }
Object { "{" list<Property>? "}" }
Array { "[" list<ArrayValue>? "]" }
Property { PropertyName ":" value }
PropertyName { string | identifier }
ArrayValue { value }
@tokens {
True { "true" }
False { "false" }
Null { "null" }
Number { NumberDec | NumberFrac | NumberFracRev | NumberHex | extremes }
NumberDec { ($[+-])? int frac? exp? }
NumberFrac[@dialect=json5] { ($[+-])? frac exp? }
NumberFracRev[@dialect=json5] { ($[+-])? fracRev exp? }
NumberHex[@dialect=json5] { ($[+-])? "0x" $[0-9a-fA-F]+ }
int { '0' | $[1-9] std.digit* }
frac { '.' std.digit+ }
fracRev[@dialect=json5] { std.digit+ '.' }
exp { $[eE] $[+\-]? std.digit+ }
extremes[@dialect=json5] { ($[+-])? ("Infinity" | "NaN") }
string { stringjson | stringjson5 }
stringjson[@dialect=json] { '"' char* '"' }
stringjson5[@dialect=json5] {
"\"" ("\\" ("'" | "\"" | escapableChar | newline) | !["\\])* "\"" |
"'" ("\\" ("'" | "\"" | escapableChar | newline) | !['\\])* "'"
}
escapableChar[@dialect=json5] {
$[\\bfnrtv0xua]
}
newline[@dialect=json5] { $[\r\n\u2028\u2029] }
char { $[\u{20}\u{21}\u{23}-\u{5b}\u{5d}-\u{10ffff}] | "\\" esc }
esc { $["\\\/bfnrt] | "u" hex hex hex hex }
hex { $[0-9a-fA-F] }
whitespace { $[ \n\r\t] | whitespaceAdd }
whitespaceAdd[@dialect=json5] { $[\u{b}\u{c}\u{a0}\u{feff}\u{2028}\u{2029}\u{2003}] }
"{" "}" "[" "]"
identifierChar[@dialect=json5] { std.asciiLetter
| $[_$\u{a1}-\u{2002}\u{2004}-\u{2027}\u{2030}-\u{fefe}\u{ff00}-\u{10ffff}]
| "\\u" hex hex hex hex
}
word[@dialect=json5] { identifierChar (identifierChar | std.digit)* }
identifier[@dialect=json5] { word }
LineComment[@dialect=json5] { "//" ![\r\n\u2028\u2029]* }
BlockComment[@dialect=json5] { "/*" blockCommentRest }
blockCommentRest[@dialect=json5] { ![*] blockCommentRest | "*" blockCommentAfterStar }
blockCommentAfterStar[@dialect=json5] { "/" | "*" blockCommentAfterStar | ![/*] blockCommentRest }
}
@skip { whitespace | LineComment | BlockComment }
// TODO trailing comma only in JSON5 dialect
list<item> { item ("," item)* ','? }
@detectDelim