OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef RUNTIME_VM_JSON_PARSER_H_ | |
6 #define RUNTIME_VM_JSON_PARSER_H_ | |
7 | |
8 #include "vm/allocation.h" | |
9 #include "vm/zone.h" | |
10 #include "vm/growable_array.h" | |
11 | |
12 namespace dart { | |
13 | |
14 class ParsedJSONArray; | |
15 | |
16 class ParsedJSONValue : public ZoneAllocated { | |
17 public: | |
18 ParsedJSONValue() {} | |
Florian Schneider
2016/12/15 19:09:35
Empty constructor not needed.
rmacnak
2016/12/15 21:27:19
Done.
| |
19 virtual ~ParsedJSONValue() {} | |
20 | |
21 virtual bool IsObject() const { return false; } | |
22 virtual bool IsArray() const { return false; } | |
23 virtual bool IsString() const { return false; } | |
24 virtual bool IsNumber() const { return false; } | |
25 virtual bool IsBoolean() const { return false; } | |
26 virtual bool IsError() const { return false; } | |
27 }; | |
28 | |
29 class ParsedJSONString : public ParsedJSONValue { | |
30 public: | |
31 explicit ParsedJSONString(const char* value) : value_(value) {} | |
32 bool Equals(const char* other) { return strcmp(value_, other) == 0; } | |
33 const char* value() { return value_; } | |
34 virtual bool IsString() const { return true; } | |
35 | |
36 private: | |
37 const char* value_; | |
38 }; | |
39 | |
40 class ParsedJSONNumber : public ParsedJSONValue { | |
41 public: | |
42 explicit ParsedJSONNumber(int64_t value) : value_(value) {} | |
43 | |
44 int64_t value() { return value_; } | |
45 virtual bool IsNumber() const { return true; } | |
46 | |
47 private: | |
48 int64_t value_; | |
49 }; | |
50 | |
51 class ParsedJSONBoolean : public ParsedJSONValue { | |
52 public: | |
53 explicit ParsedJSONBoolean(bool value) : value_(value) {} | |
54 | |
55 bool value() { return value_; } | |
56 virtual bool IsBoolean() const { return true; } | |
57 | |
58 private: | |
59 bool value_; | |
60 }; | |
61 | |
62 class ParsedJSONNull : public ParsedJSONValue { | |
63 public: | |
64 ParsedJSONNull() {} | |
Florian Schneider
2016/12/15 19:09:35
Empty constructor not needed.
rmacnak
2016/12/15 21:27:19
Done.
| |
65 | |
66 virtual bool IsNull() const { return true; } | |
67 }; | |
68 | |
69 class ParsedJSONObject : public ParsedJSONValue { | |
70 public: | |
71 explicit ParsedJSONObject(intptr_t length, ParsedJSONValue** keys_and_values) | |
Florian Schneider
2016/12/15 19:09:35
explicit not needed.
rmacnak
2016/12/15 21:27:19
Done.
| |
72 : length_(length), keys_and_values_(keys_and_values) {} | |
73 | |
74 ParsedJSONValue* At(const char* key) const { | |
75 for (intptr_t i = 0; i < length_; i += 2) { | |
76 ASSERT(keys_and_values_[i]->IsString()); | |
77 ParsedJSONString* jskey = | |
78 static_cast<ParsedJSONString*>(keys_and_values_[i]); | |
79 if (jskey->Equals(key)) { | |
80 return keys_and_values_[i + 1]; | |
81 } | |
82 } | |
83 return NULL; | |
84 } | |
85 | |
86 virtual bool IsObject() const { return true; } | |
87 | |
88 ParsedJSONNumber* NumberAt(const char* key) { | |
89 ParsedJSONValue* member = At(key); | |
90 if ((member == NULL) || !member->IsNumber()) { | |
91 return NULL; | |
92 } | |
93 return static_cast<ParsedJSONNumber*>(member); | |
94 } | |
95 | |
96 ParsedJSONString* StringAt(const char* key) { | |
97 ParsedJSONValue* member = At(key); | |
98 if ((member == NULL) || !member->IsString()) { | |
99 return NULL; | |
100 } | |
101 return static_cast<ParsedJSONString*>(member); | |
102 } | |
103 | |
104 ParsedJSONBoolean* BooleanAt(const char* key) { | |
105 ParsedJSONValue* member = At(key); | |
106 if ((member == NULL) || !member->IsBoolean()) { | |
107 return NULL; | |
108 } | |
109 return static_cast<ParsedJSONBoolean*>(member); | |
110 } | |
111 | |
112 ParsedJSONArray* ArrayAt(const char* key); | |
113 | |
114 private: | |
115 intptr_t length_; | |
116 ParsedJSONValue** keys_and_values_; | |
117 }; | |
118 | |
119 class ParsedJSONArray : public ParsedJSONValue { | |
120 public: | |
121 explicit ParsedJSONArray(intptr_t length, ParsedJSONValue** elements) | |
Florian Schneider
2016/12/15 19:09:35
explicit not needed.
rmacnak
2016/12/15 21:27:19
Done.
| |
122 : length_(length), elements_(elements) {} | |
123 | |
124 ParsedJSONValue* At(intptr_t index) const { | |
125 ASSERT(index < length_); | |
126 return elements_[index]; | |
127 } | |
128 | |
129 intptr_t Length() const { return length_; } | |
130 | |
131 virtual bool IsArray() const { return true; } | |
132 | |
133 ParsedJSONObject* ObjectAt(intptr_t index) { | |
134 ParsedJSONValue* element = At(index); | |
135 if ((element == NULL) || !element->IsObject()) { | |
136 return NULL; | |
137 } | |
138 return static_cast<ParsedJSONObject*>(element); | |
139 } | |
140 | |
141 ParsedJSONNumber* NumberAt(intptr_t index) { | |
142 ParsedJSONValue* element = At(index); | |
143 if ((element == NULL) || !element->IsNumber()) { | |
144 return NULL; | |
145 } | |
146 return static_cast<ParsedJSONNumber*>(element); | |
147 } | |
148 | |
149 private: | |
150 intptr_t length_; | |
151 ParsedJSONValue** elements_; | |
152 }; | |
153 | |
154 class ParsedJSONError : public ParsedJSONValue { | |
155 public: | |
156 explicit ParsedJSONError(const char* message, intptr_t position) | |
157 : message_(message), position_(position) {} | |
158 | |
159 virtual bool IsError() const { return true; } | |
160 | |
161 const char* message() const { return message_; } | |
162 intptr_t position() const { return position_; } | |
163 | |
164 private: | |
165 const char* message_; | |
166 intptr_t position_; | |
167 }; | |
168 | |
169 class JSONParser { | |
170 public: | |
171 JSONParser(const char* buffer, intptr_t length, Zone* zone) | |
172 : buffer_(buffer), position_(0), length_(length), zone_(zone) {} | |
173 | |
174 ParsedJSONValue* ParseValue() { | |
175 ConsumeWhitespace(); | |
176 if (Peek() == '\"') return ParseString(); | |
177 if (IsDigitOrMinus(Peek())) return ParseNumber(); | |
178 if (Peek() == '{') return ParseObject(); | |
179 if (Peek() == '[') return ParseArray(); | |
180 if (PeekAndConsume("true")) return new (zone_) ParsedJSONBoolean(true); | |
181 if (PeekAndConsume("false")) return new (zone_) ParsedJSONBoolean(false); | |
182 if (PeekAndConsume("null")) return new (zone_) ParsedJSONNull(); | |
183 return Error("value expected"); | |
184 } | |
185 | |
186 private: | |
187 intptr_t Available() const { return length_ - position_; } | |
188 char Peek() const { | |
189 if (position_ < length_) return buffer_[position_]; | |
190 return 0; | |
191 } | |
192 char Consume() { | |
193 ASSERT(position_ < length_); | |
194 return buffer_[position_++]; | |
195 } | |
196 bool PeekAndConsume(const char* expected) { | |
197 intptr_t n = strlen(expected); | |
198 if (Available() < n) return false; | |
199 if (strncmp(&buffer_[position_], expected, n) != 0) return false; | |
200 position_ += n; | |
201 return true; | |
202 } | |
203 void ConsumeWhitespace() { | |
204 while ((Available() > 0) && (buffer_[position_] < ' ')) | |
205 position_++; | |
206 } | |
207 bool IsDigit(char c) { return c >= '0' && c <= '9'; } | |
208 bool IsDigitOrMinus(char c) { return (c == '-') || (c >= '0' && c <= '9'); } | |
209 | |
210 ParsedJSONValue* ParseString() { | |
211 ConsumeWhitespace(); | |
212 if (Peek() != '\"') return Error("string expected"); | |
213 Consume(); | |
214 intptr_t start = position_; | |
215 for (;;) { | |
216 if (Available() == 0) return Error("unterminated string"); | |
217 if (Consume() == '\"') break; | |
218 } | |
219 intptr_t end = position_ - 1; | |
220 | |
221 char* cstr = zone_->Alloc<char>(end - start + 1); | |
222 intptr_t dst_pos = 0; | |
223 for (intptr_t src_pos = start; src_pos < end; src_pos++) { | |
224 if (buffer_[src_pos] == '\\') { | |
225 src_pos++; | |
226 } | |
227 cstr[dst_pos++] = buffer_[src_pos]; | |
228 } | |
229 cstr[dst_pos] = '\0'; | |
230 | |
231 return new (zone_) ParsedJSONString(cstr); | |
232 } | |
233 | |
234 ParsedJSONValue* ParseNumber() { | |
235 ConsumeWhitespace(); | |
236 bool negate = false; | |
237 if (Peek() == '-') { | |
238 Consume(); | |
239 negate = true; | |
240 } | |
241 if (!IsDigit(Peek())) return Error("number expected"); | |
242 int64_t value = 0; | |
243 for (;;) { | |
244 if (!IsDigit(Peek())) break; | |
245 char c = Consume(); | |
246 value *= 10; | |
247 value += (c - '0'); | |
248 } | |
249 if (negate) { | |
250 value = -value; | |
251 } | |
252 return new (zone_) ParsedJSONNumber(value); | |
253 } | |
254 | |
255 ParsedJSONValue* ParseObject() { | |
256 ConsumeWhitespace(); | |
257 if (Peek() != '{') return Error("object expected"); | |
258 Consume(); | |
259 ConsumeWhitespace(); | |
260 if (Peek() == '}') return new (zone_) ParsedJSONObject(0, NULL); | |
261 ZoneGrowableArray<ParsedJSONValue*>* keys_and_values = | |
262 new (zone_) ZoneGrowableArray<ParsedJSONValue*>(zone_, 6); | |
263 for (;;) { | |
264 ParsedJSONValue* key = ParseString(); | |
265 if (key->IsError()) return key; | |
266 ConsumeWhitespace(); | |
267 if (Consume() != ':') return Error(": expected"); | |
268 ConsumeWhitespace(); | |
269 ParsedJSONValue* value = ParseValue(); | |
270 if (value->IsError()) return value; | |
271 ConsumeWhitespace(); | |
272 | |
273 keys_and_values->Add(key); | |
274 keys_and_values->Add(value); | |
275 | |
276 char c = Consume(); | |
277 if (c == '}') break; | |
278 if (c != ',') return Error(", expected (object)"); | |
279 ConsumeWhitespace(); | |
280 } | |
281 | |
282 return new (zone_) | |
283 ParsedJSONObject(keys_and_values->length(), keys_and_values->data()); | |
284 } | |
285 | |
286 ParsedJSONValue* ParseArray() { | |
287 ConsumeWhitespace(); | |
288 if (Peek() != '[') return Error("array expected"); | |
289 Consume(); | |
290 ConsumeWhitespace(); | |
291 if (Peek() == ']') { | |
292 Consume(); | |
293 return new (zone_) ParsedJSONArray(0, NULL); | |
294 } | |
295 ZoneGrowableArray<ParsedJSONValue*>* elements = | |
296 new (zone_) ZoneGrowableArray<ParsedJSONValue*>(zone_, 6); | |
297 for (;;) { | |
298 ParsedJSONValue* element = ParseValue(); | |
299 if (element->IsError()) return element; | |
300 ConsumeWhitespace(); | |
301 | |
302 elements->Add(element); | |
303 | |
304 char c = Consume(); | |
305 if (c == ']') break; | |
306 if (c != ',') return Error(", expected (array)"); | |
307 ConsumeWhitespace(); | |
308 } | |
309 | |
310 return new (zone_) ParsedJSONArray(elements->length(), elements->data()); | |
311 } | |
312 | |
313 private: | |
314 ParsedJSONError* Error(const char* message) { | |
315 return new (zone_) ParsedJSONError(message, position_); | |
316 } | |
317 | |
318 const char* const buffer_; | |
319 intptr_t position_; | |
320 intptr_t length_; | |
321 Zone* zone_; | |
322 }; | |
323 | |
324 } // namespace dart | |
325 | |
326 #endif // RUNTIME_VM_JSON_PARSER_H_ | |
OLD | NEW |