00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #pragma once
00039
00040 #include "features.h"
00041 #include "value.h"
00042 #include <deque>
00043 #include <stack>
00044 #include <string>
00045 #include <iosfwd>
00046
00047 namespace Json {
00048
00052 class JSON_API Reader
00053 {
00054 public:
00055 typedef char Char;
00056 typedef const Char *Location;
00057
00061 Reader();
00062
00066 Reader( const Features &features );
00067
00078 bool parse( const std::string &document,
00079 Value &root,
00080 bool collectComments = true );
00081
00092 bool parse( const char *beginDoc, const char *endDoc,
00093 Value &root,
00094 bool collectComments = true );
00095
00098 bool parse( std::istream &is,
00099 Value &root,
00100 bool collectComments = true );
00101
00107 std::string getFormatedErrorMessages() const;
00108
00109 private:
00110 enum TokenType
00111 {
00112 tokenEndOfStream = 0,
00113 tokenObjectBegin,
00114 tokenObjectEnd,
00115 tokenArrayBegin,
00116 tokenArrayEnd,
00117 tokenString,
00118 tokenNumber,
00119 tokenTrue,
00120 tokenFalse,
00121 tokenNull,
00122 tokenArraySeparator,
00123 tokenMemberSeparator,
00124 tokenComment,
00125 tokenError
00126 };
00127
00128 class Token
00129 {
00130 public:
00131 TokenType type_;
00132 Location start_;
00133 Location end_;
00134 };
00135
00136 class ErrorInfo
00137 {
00138 public:
00139 Token token_;
00140 std::string message_;
00141 Location extra_;
00142 };
00143
00144 typedef std::deque<ErrorInfo> Errors;
00145
00146 bool expectToken( TokenType type, Token &token, const char *message );
00147 bool readToken( Token &token );
00148 void skipSpaces();
00149 bool match( Location pattern,
00150 int patternLength );
00151 bool readComment();
00152 bool readCStyleComment();
00153 bool readCppStyleComment();
00154 bool readString();
00155 void readNumber();
00156 bool readValue();
00157 bool readObject( Token &token );
00158 bool readArray( Token &token );
00159 bool decodeNumber( Token &token );
00160 bool decodeString( Token &token );
00161 bool decodeString( Token &token, std::string &decoded );
00162 bool decodeDouble( Token &token );
00163 bool decodeUnicodeCodePoint( Token &token,
00164 Location ¤t,
00165 Location end,
00166 unsigned int &unicode );
00167 bool decodeUnicodeEscapeSequence( Token &token,
00168 Location ¤t,
00169 Location end,
00170 unsigned int &unicode );
00171 bool addError( const std::string &message,
00172 Token &token,
00173 Location extra = 0 );
00174 bool recoverFromError( TokenType skipUntilToken );
00175 bool addErrorAndRecover( const std::string &message,
00176 Token &token,
00177 TokenType skipUntilToken );
00178 void skipUntilSpace();
00179 Value ¤tValue();
00180 Char getNextChar();
00181 void getLocationLineAndColumn( Location location,
00182 int &line,
00183 int &column ) const;
00184 std::string getLocationLineAndColumn( Location location ) const;
00185 void addComment( Location begin,
00186 Location end,
00187 CommentPlacement placement );
00188 void skipCommentTokens( Token &token );
00189
00190 typedef std::stack<Value *> Nodes;
00191 Nodes nodes_;
00192 Errors errors_;
00193 std::string document_;
00194 Location begin_;
00195 Location end_;
00196 Location current_;
00197 Location lastValueEnd_;
00198 Value *lastValue_;
00199 std::string commentsBefore_;
00200 Features features_;
00201 bool collectComments_;
00202 };
00203
00228 std::istream& operator>>( std::istream&, Value& );
00229
00230 }