File indexing completed on 2021-02-14 13:26:25
0001 #ifndef CPPTL_JSON_READER_H_INCLUDED
0002 #define CPPTL_JSON_READER_H_INCLUDED
0003
0004 #include "features.h"
0005 #include "value.h"
0006 #include <deque>
0007 #include <stack>
0008 #include <string>
0009 #include <iostream>
0010
0011 namespace Json {
0012
0013
0014
0015
0016 class JSON_API Reader {
0017 public:
0018 typedef char Char;
0019 typedef const Char *Location;
0020
0021
0022
0023
0024 Reader();
0025
0026
0027
0028
0029 Reader(const Features &features);
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 bool parse(const std::string &document, Value &root, bool collectComments = true);
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 bool parse(const char *beginDoc, const char *endDoc, Value &root, bool collectComments = true);
0054
0055
0056
0057 bool parse(std::istream &is, Value &root, bool collectComments = true);
0058
0059
0060
0061
0062
0063
0064 std::string getFormatedErrorMessages() const;
0065
0066 private:
0067 enum TokenType {
0068 tokenEndOfStream = 0,
0069 tokenObjectBegin,
0070 tokenObjectEnd,
0071 tokenArrayBegin,
0072 tokenArrayEnd,
0073 tokenString,
0074 tokenNumber,
0075 tokenTrue,
0076 tokenFalse,
0077 tokenNull,
0078 tokenArraySeparator,
0079 tokenMemberSeparator,
0080 tokenComment,
0081 tokenError
0082 };
0083
0084 class Token {
0085 public:
0086 TokenType type_;
0087 Location start_;
0088 Location end_;
0089 };
0090
0091 class ErrorInfo {
0092 public:
0093 Token token_;
0094 std::string message_;
0095 Location extra_;
0096 };
0097
0098 typedef std::deque<ErrorInfo> Errors;
0099
0100 bool expectToken(TokenType type, Token &token, const char *message);
0101 bool readToken(Token &token);
0102 void skipSpaces();
0103 bool match(Location pattern, int patternLength);
0104 bool readComment();
0105 bool readCStyleComment();
0106 bool readCppStyleComment();
0107 bool readString();
0108 void readNumber();
0109 bool readValue();
0110 bool readObject(Token &token);
0111 bool readArray(Token &token);
0112 bool decodeNumber(Token &token);
0113 bool decodeString(Token &token);
0114 bool decodeString(Token &token, std::string &decoded);
0115 bool decodeDouble(Token &token);
0116 bool decodeUnicodeCodePoint(Token &token, Location ¤t, Location end, unsigned int &unicode);
0117 bool decodeUnicodeEscapeSequence(Token &token, Location ¤t, Location end, unsigned int &unicode);
0118 bool addError(const std::string &message, Token &token, Location extra = nullptr);
0119 bool recoverFromError(TokenType skipUntilToken);
0120 bool addErrorAndRecover(const std::string &message, Token &token, TokenType skipUntilToken);
0121 void skipUntilSpace();
0122 Value ¤tValue();
0123 Char getNextChar();
0124 void getLocationLineAndColumn(Location location, int &line, int &column) const;
0125 std::string getLocationLineAndColumn(Location location) const;
0126 void addComment(Location begin, Location end, CommentPlacement placement);
0127 void skipCommentTokens(Token &token);
0128
0129 typedef std::stack<Value *> Nodes;
0130 Nodes nodes_;
0131 Errors errors_;
0132 std::string document_;
0133 Location begin_;
0134 Location end_;
0135 Location current_;
0136 Location lastValueEnd_;
0137 Value *lastValue_;
0138 std::string commentsBefore_;
0139 Features features_;
0140 bool collectComments_;
0141 };
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 std::istream &operator>>(std::istream &, Value &);
0168
0169 }
0170
0171 #endif