00001 // Copyright 2007-2010 Baptiste Lepilleur 00002 // Distributed under MIT license, or public domain if desired and 00003 // recognized in your jurisdiction. 00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 00005 00006 #ifndef CPPTL_JSON_READER_H_INCLUDED 00007 # define CPPTL_JSON_READER_H_INCLUDED 00008 00009 #if !defined(JSON_IS_AMALGAMATION) 00010 # include "features.h" 00011 # include "value.h" 00012 #endif // if !defined(JSON_IS_AMALGAMATION) 00013 # include <deque> 00014 # include <stack> 00015 # include <string> 00016 # include <iostream> 00017 00018 namespace Json { 00019 00023 class JSON_API Reader 00024 { 00025 public: 00026 typedef char Char; 00027 typedef const Char *Location; 00028 00032 Reader(); 00033 00037 Reader( const Features &features ); 00038 00049 bool parse( const std::string &document, 00050 Value &root, 00051 bool collectComments = true ); 00052 00065 bool parse( const char *beginDoc, const char *endDoc, 00066 Value &root, 00067 bool collectComments = true ); 00068 00071 bool parse( std::istream &is, 00072 Value &root, 00073 bool collectComments = true ); 00074 00081 JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") 00082 std::string getFormatedErrorMessages() const; 00083 00089 std::string getFormattedErrorMessages() const; 00090 00091 private: 00092 enum TokenType 00093 { 00094 tokenEndOfStream = 0, 00095 tokenObjectBegin, 00096 tokenObjectEnd, 00097 tokenArrayBegin, 00098 tokenArrayEnd, 00099 tokenString, 00100 tokenNumber, 00101 tokenTrue, 00102 tokenFalse, 00103 tokenNull, 00104 tokenArraySeparator, 00105 tokenMemberSeparator, 00106 tokenComment, 00107 tokenError 00108 }; 00109 00110 class Token 00111 { 00112 public: 00113 TokenType type_; 00114 Location start_; 00115 Location end_; 00116 }; 00117 00118 class ErrorInfo 00119 { 00120 public: 00121 Token token_; 00122 std::string message_; 00123 Location extra_; 00124 }; 00125 00126 typedef std::deque<ErrorInfo> Errors; 00127 00128 bool expectToken( TokenType type, Token &token, const char *message ); 00129 bool readToken( Token &token ); 00130 void skipSpaces(); 00131 bool match( Location pattern, 00132 int patternLength ); 00133 bool readComment(); 00134 bool readCStyleComment(); 00135 bool readCppStyleComment(); 00136 bool readString(); 00137 void readNumber(); 00138 bool readValue(); 00139 bool readObject( Token &token ); 00140 bool readArray( Token &token ); 00141 bool decodeNumber( Token &token ); 00142 bool decodeString( Token &token ); 00143 bool decodeString( Token &token, std::string &decoded ); 00144 bool decodeDouble( Token &token ); 00145 bool decodeUnicodeCodePoint( Token &token, 00146 Location ¤t, 00147 Location end, 00148 unsigned int &unicode ); 00149 bool decodeUnicodeEscapeSequence( Token &token, 00150 Location ¤t, 00151 Location end, 00152 unsigned int &unicode ); 00153 bool addError( const std::string &message, 00154 Token &token, 00155 Location extra = 0 ); 00156 bool recoverFromError( TokenType skipUntilToken ); 00157 bool addErrorAndRecover( const std::string &message, 00158 Token &token, 00159 TokenType skipUntilToken ); 00160 void skipUntilSpace(); 00161 Value ¤tValue(); 00162 Char getNextChar(); 00163 void getLocationLineAndColumn( Location location, 00164 int &line, 00165 int &column ) const; 00166 std::string getLocationLineAndColumn( Location location ) const; 00167 void addComment( Location begin, 00168 Location end, 00169 CommentPlacement placement ); 00170 void skipCommentTokens( Token &token ); 00171 00172 typedef std::stack<Value *> Nodes; 00173 Nodes nodes_; 00174 Errors errors_; 00175 std::string document_; 00176 Location begin_; 00177 Location end_; 00178 Location current_; 00179 Location lastValueEnd_; 00180 Value *lastValue_; 00181 std::string commentsBefore_; 00182 Features features_; 00183 bool collectComments_; 00184 }; 00185 00210 std::istream& operator>>( std::istream&, Value& ); 00211 00212 } // namespace Json 00213 00214 #endif // CPPTL_JSON_READER_H_INCLUDED