forked from nez-peg/nez-grammar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.nez
114 lines (108 loc) · 2.65 KB
/
json.nez
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* The JSON Data Interchange Standard (RFC7159)
*
* Reference:
* http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
* http://rfc7159.net/rfc7159
*
* Authors:
* K. Kuramitsu
*/
/* Starting Point */
File = S* Value S* !.
/* Code Layout, Tokens */
S = [ \t\n\r]
"[" = '[' S*
"]" = ']' S*
"{" = '{' S*
"}" = '}' S*
"," = ',' S*
":" = ':' S*
/* Expression */
Expression = Value
Value = String
/ Number
/ Object
/ Array
/ Null
/ True
/ False
/ ObjectId
Object = { "{" ( $(Member) ( "," $(Member) )* )? "}" #JSON }
Member = { $key(String) ":" $value(Value) #$$ }
Key = String
ObjectId = 'ObjectId' '("' { ID #ObjectId } '")'
ID = [0-9a-z]+
Array = { "[" ( $(Value) ( "," $(Value) )* )? "]" #List }
String = '"' { ( '\\"' / '\\\\' / !'"' . )* #String } '"' S*
True = { 'true' #True } S*
False = { 'false' #False } S*
Null = { 'null' #Null } S*
Number = { '-'? INT (FRAC EXP? #Float / '' #Integer) } S*
INT = '0'
/ [1-9] DIGIT*
DIGIT = [0-9]
FRAC = '.' DIGIT+
EXP = [Ee] ( '-' / '+' )? DIGIT+
/* Example tested by nez test */
example Object&Value ~4492d3 '''
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
'''
example Array&Value ~a54e06 '''
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
'''
example String&Value ~78593c '''
"Hello world!"
'''
example Number&Value ~55ecfb '''
42
'''
example Value ~1e4f2d '''
true
'''
/* Format */
format #JSON[*] `{$[0 `,` -1]}`
format #$$[*] `$[0]:$[1]`
format #List[*] `[$[0 `,` -1]]`
format #String[*] `"${text}"`
format #Integer[*] `${text}`
format #Float[*] `${text}`
format #True[*] `true`
format #False[*] `false`
format #Null[*] `null`
format #ObjectId[*] `ObjectId("$[0]")`
// formatted by $ nez format