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