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. | |
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* json_text); | |
33 | |
34 void SetText(const char* json_text); | |
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 }; | |
56 | |
57 | |
58 // JSONReader is a higher level interface that allows for lookup of | |
59 // name-value pairs in JSON objects. | |
60 class JSONReader : ValueObject { | |
61 public: | |
62 enum JSONType { | |
63 kString, | |
64 kInteger, | |
65 kObject, | |
66 kArray, | |
67 kLiteral, | |
68 kNone | |
69 }; | |
70 | |
71 explicit JSONReader(const char* json_object); | |
72 void Set(const char* json_object); | |
73 | |
74 // Returns true if a pair with the given name was found. | |
75 bool Seek(const char* name); | |
76 | |
77 // Returns true if a syntax error was found. | |
78 bool Error() const { return error_; } | |
79 | |
80 JSONType Type() const; | |
81 const char* ValueChars() const { | |
82 return (Type() != kNone) ? scanner_.TokenChars() : NULL; | |
83 } | |
84 int ValueLen() const { | |
85 return (Type() != kNone) ? scanner_.TokenLen() : 0; | |
86 } | |
87 bool IsStringLiteral(const char* literal) const { | |
88 return scanner_.IsStringLiteral(literal); | |
89 } | |
90 bool IsTrue() const { | |
91 return scanner_.CurrentToken() == JSONScanner::TokenTrue; | |
92 } | |
93 bool IsFalse() const { | |
94 return scanner_.CurrentToken() == JSONScanner::TokenFalse; | |
95 } | |
96 bool IsNull() const { | |
97 return scanner_.CurrentToken() == JSONScanner::TokenNull; | |
98 } | |
99 | |
100 private: | |
101 JSONScanner scanner_; | |
102 const char* json_object_; | |
103 bool error_; | |
104 }; | |
105 | |
106 | |
107 // TextBuffer maintains a dynamic character buffer with a printf-style way to | |
108 // append text. | |
109 class TextBuffer : ValueObject { | |
110 public: | |
111 explicit TextBuffer(intptr_t buf_size); | |
112 ~TextBuffer(); | |
113 | |
114 intptr_t Printf(const char* format, ...); | |
115 void Clear(); | |
116 | |
117 char* buf() { return buf_; } | |
118 | |
119 private: | |
120 void GrowBuffer(intptr_t len); | |
121 char* buf_; | |
122 intptr_t buf_size_; | |
123 intptr_t msg_len_; | |
124 }; | |
125 | |
126 } // namespace dart | |
127 | |
128 #endif // VM_JSON_H_ | |
OLD | NEW |