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 PLATFORM_JSON_H_ | |
6 #define PLATFORM_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 // Returns a pointer to the matching closing brace if the text starts | |
81 // with a valid JSON object. Returns NULL otherwise. | |
82 const char* EndOfObject(); | |
83 | |
84 JSONType Type() const; | |
85 const char* ValueChars() const { | |
86 return (Type() != kNone) ? scanner_.TokenChars() : NULL; | |
87 } | |
88 int ValueLen() const { | |
89 return (Type() != kNone) ? scanner_.TokenLen() : 0; | |
90 } | |
91 void GetRawValueChars(char* buf, intptr_t buflen) const; | |
92 void GetDecodedValueChars(char* buf, intptr_t buflen) const; | |
93 bool IsStringLiteral(const char* literal) const { | |
94 return scanner_.IsStringLiteral(literal); | |
95 } | |
96 bool IsTrue() const { | |
97 return scanner_.CurrentToken() == JSONScanner::TokenTrue; | |
98 } | |
99 bool IsFalse() const { | |
100 return scanner_.CurrentToken() == JSONScanner::TokenFalse; | |
101 } | |
102 bool IsNull() const { | |
103 return scanner_.CurrentToken() == JSONScanner::TokenNull; | |
104 } | |
105 | |
106 // Debugging method to check for validity of a JSON message. | |
107 bool CheckMessage(); | |
108 | |
109 private: | |
110 void CheckObject(); | |
111 void CheckArray(); | |
112 void CheckValue(); | |
113 | |
114 JSONScanner scanner_; | |
115 const char* json_object_; | |
116 bool error_; | |
117 }; | |
118 | |
119 | |
120 // TextBuffer maintains a dynamic character buffer with a printf-style way to | |
121 // append text. | |
122 class TextBuffer : ValueObject { | |
123 public: | |
124 explicit TextBuffer(intptr_t buf_size); | |
125 ~TextBuffer(); | |
126 | |
127 intptr_t Printf(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); | |
128 void AddChar(char ch); | |
129 void EscapeAndAddCodeUnit(uint32_t cu); | |
130 void AddString(const char* s); | |
131 void AddEscapedString(const char* s); | |
132 | |
133 void Clear(); | |
134 | |
135 char* buf() { return buf_; } | |
136 intptr_t length() { return msg_len_; } | |
137 | |
138 // Steal ownership of the buffer pointer. | |
139 // NOTE: TextBuffer is empty afterwards. | |
140 const char* Steal(); | |
141 | |
142 private: | |
143 void EnsureCapacity(intptr_t len); | |
144 char* buf_; | |
145 intptr_t buf_size_; | |
146 intptr_t msg_len_; | |
147 }; | |
148 | |
149 } // namespace dart | |
150 | |
151 #endif // PLATFORM_JSON_H_ | |
OLD | NEW |