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