reader.h
Go to the documentation of this file.00001 #ifndef CPPTL_JSON_READER_H_INCLUDED
00002 # define CPPTL_JSON_READER_H_INCLUDED
00003
00004 # include "forwards.h"
00005 # include "value.h"
00006 # include <deque>
00007 # include <stack>
00008 # include <string>
00009
00010 namespace Json {
00011
00012 class Value;
00013
00018 class JSON_API Reader
00019 {
00020 public:
00021 typedef char Char;
00022 typedef const Char *Location;
00023
00024 Reader();
00025
00034 bool parse( const std::string &document,
00035 Value &root,
00036 bool collectComments = true );
00037
00046 bool parse( const char *beginDoc, const char *endDoc,
00047 Value &root,
00048 bool collectComments = true );
00049
00055 std::string getFormatedErrorMessages() const;
00056
00057 private:
00058 enum TokenType
00059 {
00060 tokenEndOfStream = 0,
00061 tokenObjectBegin,
00062 tokenObjectEnd,
00063 tokenArrayBegin,
00064 tokenArrayEnd,
00065 tokenString,
00066 tokenNumber,
00067 tokenTrue,
00068 tokenFalse,
00069 tokenNull,
00070 tokenArraySeparator,
00071 tokenMemberSeparator,
00072 tokenComment,
00073 tokenError
00074 };
00075
00076 class Token
00077 {
00078 public:
00079 TokenType type_;
00080 Location start_;
00081 Location end_;
00082 };
00083
00084 class ErrorInfo
00085 {
00086 public:
00087 Token token_;
00088 std::string message_;
00089 Location extra_;
00090 };
00091
00092 typedef std::deque<ErrorInfo> Errors;
00093
00094 bool expectToken( TokenType type, Token &token, const char *message );
00095 bool readToken( Token &token );
00096 void skipSpaces();
00097 bool match( Location pattern,
00098 int patternLength );
00099 bool readComment();
00100 bool readCStyleComment();
00101 bool readCppStyleComment();
00102 bool readString();
00103 void readNumber();
00104 bool readValue();
00105 bool readObject( Token &token );
00106 bool readArray( Token &token );
00107 bool decodeNumber( Token &token );
00108 bool decodeString( Token &token );
00109 bool decodeString( Token &token, std::string &decoded );
00110 bool decodeDouble( Token &token );
00111 bool decodeUnicodeEscapeSequence( Token &token,
00112 Location ¤t,
00113 Location end,
00114 unsigned int &unicode );
00115 bool addError( const std::string &message,
00116 Token &token,
00117 Location extra = 0 );
00118 bool recoverFromError( TokenType skipUntilToken );
00119 bool addErrorAndRecover( const std::string &message,
00120 Token &token,
00121 TokenType skipUntilToken );
00122 void skipUntilSpace();
00123 Value ¤tValue();
00124 Char getNextChar();
00125 void getLocationLineAndColumn( Location location,
00126 int &line,
00127 int &column ) const;
00128 std::string getLocationLineAndColumn( Location location ) const;
00129 void addComment( Location begin,
00130 Location end,
00131 CommentPlacement placement );
00132 void skipCommentTokens( Token &token );
00133
00134 typedef std::stack<Value *> Nodes;
00135 Nodes nodes_;
00136 Errors errors_;
00137 std::string document_;
00138 Location begin_;
00139 Location end_;
00140 Location current_;
00141 Location lastValueEnd_;
00142 Value *lastValue_;
00143 std::string commentsBefore_;
00144 bool collectComments_;
00145 };
00146
00147
00148 }
00149
00150 #endif // CPPTL_JSON_READER_H_INCLUDED