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