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