-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.pegjs
189 lines (152 loc) · 3.94 KB
/
grammar.pegjs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
{
}
/* ===== Syntactical Elements ===== */
start
= element*
element
= declaration
/ definition
declaration
= _ ids:(id:identifier _ { return id; })+ mods:modifiers?
val:assignment dstr:dstring ";" _ {
return {
"element": "declaration",
"qualifiers": ids.slice(0, ids.length-2),
"typename": ids.slice(-2, -1)[0],
"varname": ids.slice(-1)[0],
"modifiers": mods,
"value": val,
"description": dstr,
"file": null,
"location": location(),
};
}
modification
= _ id:identifier _ "=" _ e:expr _ { return [id, e]; }
modifiers
= "(" con:(head:modification rest:("," m:modification { return m; })* {
var ret = {};
rest.push(head);
for(var i=0;i<rest.length;i++) { ret[rest[i][0]] = rest[i][1]; }
return ret;
})? ")" { return con ? con : {}; }
definition
= ids:(id:identifier _ { return id; })+ mods:modifiers? dstr:dstring _ "{"
_ contents: (elem:element _ { return elem; })* "}" _ {
return {
"element": "definition",
"qualifiers": ids.slice(0, ids.length-1),
"name": ids.slice(-1)[0],
"modifiers": mods,
"description": dstr,
"contents": contents,
"file": null,
"location": location(),
};
}
dstring
= (_ s:string { return s; })?
assignment
= (_ "=" _ e:expr { return e; })?
firstchar
= [a-zA-Z_]
restchar
= [a-zA-Z_0-9]
identifier
/* Should be '$(firstchar restchar+)', but I couldn't get that to work! */
= chars:$(restchar+) { return chars; }
/ "'" chars:[^'\\\0-\x1F\x7f]+ "'" { return chars.join(""); }
expr
= value
json
= _ object:object { return object; }
object
= "{" _ "}" _ { return {}; }
/ "{" _ members:members "}" _ { return members; }
members
= head:pair tail:("," _ pair)* {
var result = {};
result[head[0]] = head[1];
for (var i = 0; i < tail.length; i++) {
result[tail[i][2][0]] = tail[i][2][1];
}
return result;
}
pair
= name:string ":" _ value:value { return [name, value]; }
array
= "[" _ "]" _ { return []; }
/ "[" _ elements:elements "]" _ { return elements; }
elements
= head:value tail:("," _ value)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][2]);
}
return result;
}
value
= string
/ number
/ object
/ array
/ "true" _ { return true; }
/ "false" _ { return false; }
/ "null" _ { return null; }
/* ===== Lexical Elements ===== */
string "string"
= '"' '"' _ { return ""; }
/ '"' chars:chars '"' _ { return chars; }
chars
= chars:char+ { return chars.join(""); }
char
// In the original JSON grammar: "any-Unicode-character-except-"-or-\-or-control-character"
= [^"\\\0-\x1F\x7f]
/ '\\"' { return '"'; }
/ "\\\\" { return "\\"; }
/ "\\/" { return "/"; }
/ "\\b" { return "\b"; }
/ "\\f" { return "\f"; }
/ "\\n" { return "\n"; }
/ "\\r" { return "\r"; }
/ "\\t" { return "\t"; }
/ "\\u" digits:$(hexDigit hexDigit hexDigit hexDigit) {
return String.fromCharCode(parseInt(digits, 16));
}
number "number"
= parts:$(int frac exp) _ { return parseFloat(parts); }
/ parts:$(int frac) _ { return parseFloat(parts); }
/ parts:$(int exp) _ { return parseFloat(parts); }
/ parts:$(int) _ { return parseFloat(parts); }
int
= digit19 digits
/ digit
/ "-" digit19 digits
/ "-" digit
frac
= "." digits
exp
= e digits
digits
= digit+
e
= [eE] [+-]?
/*
* The following rules are not present in the original JSON gramar, but they are
* assumed to exist implicitly.
*
* FIXME: Define them according to ECMA-262, 5th ed.
*/
digit
= [0-9]
digit19
= [1-9]
hexDigit
= [0-9a-fA-F]
/* ===== Whitespace ===== */
_ "whitespace"
= whitespace*
// Whitespace is undefined in the original JSON grammar, so I assume a simple
// conventional definition consistent with ECMA-262, 5th ed.
whitespace
= [ \t\n\r]