OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 VM_JSON_H_ | |
6 #define VM_JSON_H_ | |
7 | |
8 #include "vm/allocation.h" | |
9 #include "vm/globals.h" | |
10 | |
11 namespace dart { | |
12 | |
13 | |
14 // A low level interface to tokenize JSON strings. | |
turnidge
2012/04/09 22:26:42
Will anybody ever use this directly, or will they
hausner
2012/04/10 16:26:51
The JSONReader declarations in this file need it.
| |
15 class JSONScanner : ValueObject { | |
16 public: | |
17 enum Token { | |
18 TokenIllegal = 0, | |
19 TokenLBrace, | |
20 TokenRBrace, | |
21 TokenLBrack, | |
22 TokenRBrack, | |
23 TokenColon, | |
24 TokenComma, | |
25 TokenString, | |
26 TokenInteger, | |
27 TokenTrue, | |
28 TokenFalse, | |
29 TokenNull, | |
30 TokenEOM | |
31 }; | |
32 explicit JSONScanner(const char* msg); | |
turnidge
2012/04/09 22:26:42
For consistency, consider renaming msg -> json_obj
hausner
2012/04/10 16:26:51
Done.
| |
33 | |
34 void Set(const char* str); | |
turnidge
2012/04/09 22:26:42
str -> json_object
hausner
2012/04/10 16:26:51
Done.
| |
35 void Scan(); | |
36 Token CurrentToken() const { return token_; } | |
37 bool EOM() const { return token_ == TokenEOM; } | |
38 const char* TokenChars() const { return token_start_; } | |
39 int TokenLen() const { return token_length_; } | |
40 bool IsStringLiteral(const char* literal) const; | |
41 void Skip(Token matching_token); | |
42 | |
43 private: | |
44 bool IsLetter(char ch) const; | |
45 bool IsDigit(char ch) const; | |
46 bool IsLiteral(const char* literal); | |
47 void ScanNumber(); | |
48 void ScanString(); | |
49 void Recognize(Token t); | |
50 | |
51 const char* current_pos_; | |
52 const char* token_start_; | |
53 int token_length_; | |
54 Token token_; | |
55 const char* string_; | |
turnidge
2012/04/09 22:26:42
string_ => json_object_
hausner
2012/04/10 16:26:51
Actuall, string_ wasn't even used anywhere. Remove
| |
56 }; | |
57 | |
58 | |
59 // JSONReader is a higher level interface that allows for lookup of | |
60 // name-value pairs in JSON objects. | |
turnidge
2012/04/09 22:26:42
JSONReader reparses the whole json text on every c
hausner
2012/04/10 16:26:51
As we discussed in person, this is a low-level int
| |
61 class JSONReader : ValueObject { | |
62 public: | |
63 enum JSONType { | |
64 kString, | |
65 kInteger, | |
66 kObject, | |
67 kArray, | |
68 kLiteral, | |
69 kNone | |
70 }; | |
71 | |
72 explicit JSONReader(const char* json_object); | |
73 void Set(const char* json_object); | |
turnidge
2012/04/09 22:26:42
Set is a pretty generic name. Consider something
hausner
2012/04/10 16:26:51
Done.
| |
74 | |
75 // Returns true if the a pair with the given name was found. | |
turnidge
2012/04/09 22:26:42
typo: "the a"
hausner
2012/04/10 16:26:51
Done.
| |
76 bool Seek(const char* name); | |
77 | |
78 // Returns true if a syntax error was found. | |
79 bool Error() const { return error_; } | |
80 | |
81 JSONType Type() const; | |
82 const char* ValueChars() const { | |
83 return (Type() != kNone) ? scanner_.TokenChars() : NULL; | |
84 } | |
85 int ValueLen() const { | |
86 return (Type() != kNone) ? scanner_.TokenLen() : 0; | |
87 } | |
88 bool IsStringLiteral(const char* literal) const { | |
89 return scanner_.IsStringLiteral(literal); | |
90 } | |
91 | |
92 private: | |
93 JSONScanner scanner_; | |
94 const char* json_object_; | |
95 bool error_; | |
96 }; | |
97 | |
98 | |
turnidge
2012/04/09 22:26:42
Add a class-level comment here?
hausner
2012/04/10 16:26:51
Done.
| |
99 class JSONWriter : ValueObject { | |
turnidge
2012/04/09 22:26:42
It's confusing to call this a JSONWriter since it
hausner
2012/04/10 16:26:51
I agree, this has nothing to do with JSON at this
| |
100 public: | |
101 explicit JSONWriter(intptr_t buf_size); | |
102 ~JSONWriter(); | |
103 | |
104 intptr_t Printf(const char* format, ...); | |
105 void Clear(); | |
106 | |
107 char* buf() { return buf_; } | |
108 | |
109 private: | |
110 void GrowBuffer(intptr_t len); | |
111 char* buf_; | |
112 intptr_t buf_size_; | |
113 intptr_t msg_len_; | |
114 }; | |
115 | |
116 } // namespace dart | |
117 | |
118 #endif // VM_JSON_H_ | |
OLD | NEW |