OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_JSON_JSON_PARSER_H_ | |
6 #define BASE_JSON_JSON_PARSER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/json/json_reader.h" | |
13 #include "base/string_piece.h" | |
14 | |
15 namespace base { | |
16 class Value; | |
17 } | |
18 | |
19 // Chromium and Chromium OS check out gtest to different places, so we're | |
20 // unable to compile on both if we include gtest_prod.h here. Instead, include | |
21 // its only contents -- this will need to be updated if the macro ever changes. | |
22 #define FRIEND_TEST(test_case_name, test_name)\ | |
23 friend class test_case_name##_##test_name##_Test | |
24 | |
25 #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \ | |
26 FRIEND_TEST(test_case_name, test_name); \ | |
27 FRIEND_TEST(test_case_name, DISABLED_##test_name); \ | |
28 FRIEND_TEST(test_case_name, FLAKY_##test_name); \ | |
29 FRIEND_TEST(test_case_name, FAILS_##test_name) | |
30 | |
31 namespace base { | |
32 namespace internal { | |
33 | |
34 class JSONParserTest; | |
35 | |
36 // The implementation behind the JSONReader interface. This class is not meant | |
37 // to be used directly; it encapsultes logic that need not be exposed publicly. | |
Avi (use Gerrit)
2012/04/18 21:57:54
spelling
Robert Sesek
2012/04/18 22:06:37
Done.
| |
38 // | |
39 // This parser guarantees O(n) time through the input string. It also optimizes | |
40 // base::StringValue by using StringPiece where possible when returning Value | |
41 // objects by using "hidden roots," discussed in the implementation. | |
42 // | |
43 // Iteration happens on the byte level, with the functions CanConsume and | |
44 // NextChar. The conversion from byte to JSON token happens without advancing | |
45 // the parser in GetNextToken/ParseToken, that is that tokenization operates on | |
46 // the current parser position without advancing. | |
47 // | |
48 // Built on top of these are a family of Consume functions that iterate | |
49 // internally. Invariant: on entry of a Consume function, the parser is wound | |
50 // to the first byte of a valid JSON token. On exit, it is on the last byte | |
51 // of a token, such that the next loop of the parser will be at the byte | |
52 // immediately following the token, which would likely be the first byte of the | |
53 // next token. | |
54 class JSONParser { | |
55 public: | |
56 explicit JSONParser(int options); | |
57 virtual ~JSONParser(); | |
58 | |
59 // Parses the input string according to the set options and returns the | |
60 // result as a Value owned by the caller. | |
61 Value* Parse(const std::string& input); | |
62 | |
63 // Returns the error code. | |
64 JSONReader::JsonParseError error_code() const; | |
65 | |
66 // Returns the human-friendly error message. | |
67 std::string GetErrorMessage() const; | |
68 | |
69 private: | |
70 enum Token { | |
71 T_OBJECT_BEGIN, // { | |
72 T_OBJECT_END, // } | |
73 T_ARRAY_BEGIN, // [ | |
74 T_ARRAY_END, // ] | |
75 T_STRING, | |
76 T_NUMBER, | |
77 T_BOOL_TRUE, // true | |
78 T_BOOL_FALSE, // false | |
79 T_NULL, // null | |
80 T_LIST_SEPARATOR, // , | |
81 T_OBJECT_PAIR_SEPARATOR, // : | |
82 T_END_OF_INPUT, | |
83 T_INVALID_TOKEN, | |
84 }; | |
85 | |
86 // A helper class used for parsing strings. One optimization performed is to | |
87 // create base::Value with a StringPiece to avoid unnecessary std::string | |
88 // copies. This is not possible if the input string needs to be decoded from | |
89 // UTF-16 to UTF-8, or if an escape sequence causes characters to be skipped. | |
90 // This class centralizes that logic. | |
91 class StringBuilder { | |
92 public: | |
93 // Empty constructor. Used for creating a builder with which to Swap(). | |
94 StringBuilder(); | |
95 | |
96 // |pos| is the beginning of an input string, excluding the |"|. | |
97 explicit StringBuilder(const char* pos); | |
98 ~StringBuilder(); | |
99 | |
100 // Swaps the contents of |other| with this. | |
101 void Swap(StringBuilder* other); | |
102 | |
103 // Either increases the |length_| of the string or copies the character if | |
104 // the StringBuilder has been converted. | |
105 void Append(const int32& c); | |
106 | |
107 // Appends a string to the std::string. Must be Convert()ed to use. | |
108 void AppendString(const std::string& str); | |
109 | |
110 // Converts the builder from its default StringPiece to a full std::string, | |
111 // performing a copy. | |
112 void Convert(); | |
113 | |
114 // Returns whether the builder can be converted to a StringPiece. | |
115 bool CanBeStringPiece(); | |
116 | |
117 // Returns the StringPiece representation. Returns an empty piece if it | |
118 // cannot be converted. | |
119 StringPiece AsStringPiece(); | |
120 | |
121 // Returns the builder as a std::string. | |
122 std::string AsString(); | |
123 | |
124 private: | |
125 // The beginning of the input string. | |
126 const char* pos_; | |
127 // Number of bytes in |pos_| that compose its length. | |
128 size_t length_; | |
129 // The copied string representation. NULL until Convert() is called. | |
130 // Strong. scoped_ptr<T> has too much of an overhead here. | |
131 std::string* string_; | |
132 }; | |
133 | |
134 // Quick check that the stream has enough to consume |length| more bytes. | |
135 inline bool CanConsume(int length); | |
136 | |
137 // The basic way to consume a single character in the stream. Consumes one | |
138 // byte of the input stream and returns a pointer to the rest of it. | |
139 const char* NextChar(); | |
140 // Performs the equivalent of NextChar N times. | |
141 void NextNChars(int n); | |
142 | |
143 // Skips over whitespace and comments to find the next token in the stream. | |
144 // This does not advance the parser for non-whitespace or comment chars. | |
145 Token GetNextToken(); | |
146 | |
147 // Consumes whitespace characters and comments until the next non-that is | |
148 // encountered. | |
149 void EatWhitespaceAndComments(); | |
150 // Helper function that consumes a comment, assuming that the parser is | |
151 // currently wound to a '/'. | |
152 bool EatComment(); | |
153 | |
154 // Calls GetNextToken() and then ParseToken(). Caller owns the result. | |
155 Value* ParseNextToken(); | |
156 | |
157 // Takes a token that represents the start of a Value ("a structural token" | |
158 // in RFC terms) and consumes it, returning the result as an object the | |
159 // caller owns. | |
160 Value* ParseToken(Token token); | |
161 | |
162 // Assuming that the parser is currently wound to '{', this parses a JSON | |
163 // object into a DictionaryValue. | |
164 Value* ConsumeDictionary(); | |
165 | |
166 // Assuming that the parser is wound to '[', this parses a JSON list into a | |
167 // ListValue. | |
168 Value* ConsumeList(); | |
169 | |
170 // Calls through ConsumeStringRaw and wraps it in a value. | |
171 Value* ConsumeString(); | |
172 | |
173 // Assuming that the parser is wound to a double quote, this parses a string, | |
174 // potentially performing a UTF-16 to UTF-8 conversion. Returns true on | |
175 // success and Swap()s the result into |out|. Returns false on failure with | |
176 // error information set. | |
177 bool ConsumeStringRaw(StringBuilder* out); | |
178 // Helper function for ConsumeStringRaw() that consumes the next four to 10 | |
179 // bytes (parser is wound to the first character of a HEX sequence, with the | |
180 // potential for consuming another \uXXXX for a surrogate). Returns true on | |
181 // success and places the UTF8 code units in |dest_string|, and false on | |
182 // failure. | |
183 bool DecodeUTF16(std::string* dest_string); | |
184 | |
185 // Assuming that the parser is wound to the start of a valid JSON number, | |
186 // this parses and converts it to either an int or double value. | |
187 Value* ConsumeNumber(); | |
188 // Helper that reads characters that are ints. Returns true if a number was | |
189 // read and false on error. | |
190 bool ReadInt(bool allow_leading_zeros); | |
191 | |
192 // Consumes the literal values of |true|, |false|, and |null|, assuming the | |
193 // parser is wound to the first character of any of those. | |
194 Value* ConsumeLiteral(); | |
195 | |
196 // Compares two string buffers of a given length. | |
197 bool StringsAreEqual(const char* left, const char* right, size_t len); | |
198 | |
199 // Sets the error information to |code| at the current column, based on | |
200 // |index_| and |index_last_line_|, with an optional positive/negative | |
201 // adjustment by |column_adjust|. | |
202 void ReportError(JSONReader::JsonParseError code, int column_adjust); | |
203 static std::string FormatErrorMessage(int line, int column, | |
204 const std::string& description); | |
205 | |
206 // Options that control parsing. | |
207 int options_; | |
208 | |
209 // Pointer to the start of the input data. | |
210 const char* start_pos_; | |
211 // Pointer to the current position in the input data. Equivalent to | |
212 // |start_pos_ + index_|. | |
213 const char* pos_; | |
214 // Pointer to the last character of the input data. | |
215 const char* end_pos_; | |
216 // The index in the input stream to which the parser is wound. | |
217 int index_; | |
218 | |
219 // The number of times the parser has recursed (current stack depth). | |
220 int stack_depth_; | |
221 | |
222 // The line number that the parser is at currently. | |
223 int line_number_; | |
224 // The last value of |index_| on the previous line. | |
225 int index_last_line_; | |
226 | |
227 // Error information. | |
228 JSONReader::JsonParseError error_code_; | |
229 int error_line_; | |
230 int error_column_; | |
231 | |
232 friend class JSONParserTest; | |
233 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, NextChar); | |
234 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ConsumeDictionary); | |
235 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ConsumeList); | |
236 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ConsumeString); | |
237 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ConsumeLiterals); | |
238 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ConsumeNumbers); | |
239 FRIEND_TEST_ALL_PREFIXES(JSONParserTest, ErrorMessages); | |
240 DISALLOW_COPY_AND_ASSIGN(JSONParser); | |
241 }; | |
242 | |
243 } // namespace internal | |
244 } // namespace base | |
245 | |
246 #endif // BASE_JSON_JSON_PARSER_H_ | |
OLD | NEW |