• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_ERROR_ERROR_H__
16 #define RAPIDJSON_ERROR_ERROR_H__
17 
18 #include "../rapidjson.h"
19 
20 /*! \file error.h */
21 
22 /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
23 
24 ///////////////////////////////////////////////////////////////////////////////
25 // RAPIDJSON_ERROR_CHARTYPE
26 
27 //! Character type of error messages.
28 /*! \ingroup RAPIDJSON_ERRORS
29     The default character type is \c char.
30     On Windows, user can define this macro as \c TCHAR for supporting both
31     unicode/non-unicode settings.
32 */
33 #ifndef RAPIDJSON_ERROR_CHARTYPE
34 #define RAPIDJSON_ERROR_CHARTYPE char
35 #endif
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // RAPIDJSON_ERROR_STRING
39 
40 //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
41 /*! \ingroup RAPIDJSON_ERRORS
42     By default this conversion macro does nothing.
43     On Windows, user can define this macro as \c _T(x) for supporting both
44     unicode/non-unicode settings.
45 */
46 #ifndef RAPIDJSON_ERROR_STRING
47 #define RAPIDJSON_ERROR_STRING(x) x
48 #endif
49 
50 RAPIDJSON_NAMESPACE_BEGIN
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 // ParseErrorCode
54 
55 //! Error code of parsing.
56 /*! \ingroup RAPIDJSON_ERRORS
57     \see GenericReader::Parse, GenericReader::GetParseErrorCode
58 */
59 enum ParseErrorCode {
60     kParseErrorNone = 0,                        //!< No error.
61 
62     kParseErrorDocumentEmpty,                   //!< The document is empty.
63     kParseErrorDocumentRootNotSingular,         //!< The document root must not follow by other values.
64 
65     kParseErrorValueInvalid,                    //!< Invalid value.
66 
67     kParseErrorObjectMissName,                  //!< Missing a name for object member.
68     kParseErrorObjectMissColon,                 //!< Missing a colon after a name of object member.
69     kParseErrorObjectMissCommaOrCurlyBracket,   //!< Missing a comma or '}' after an object member.
70 
71     kParseErrorArrayMissCommaOrSquareBracket,   //!< Missing a comma or ']' after an array element.
72 
73     kParseErrorStringUnicodeEscapeInvalidHex,   //!< Incorrect hex digit after \\u escape in string.
74     kParseErrorStringUnicodeSurrogateInvalid,   //!< The surrogate pair in string is invalid.
75     kParseErrorStringEscapeInvalid,             //!< Invalid escape character in string.
76     kParseErrorStringMissQuotationMark,         //!< Missing a closing quotation mark in string.
77     kParseErrorStringInvalidEncoding,           //!< Invalid encoding in string.
78 
79     kParseErrorNumberTooBig,                    //!< Number too big to be stored in double.
80     kParseErrorNumberMissFraction,              //!< Miss fraction part in number.
81     kParseErrorNumberMissExponent,              //!< Miss exponent in number.
82 
83     kParseErrorTermination,                     //!< Parsing was terminated.
84     kParseErrorUnspecificSyntaxError            //!< Unspecific syntax error.
85 };
86 
87 //! Result of parsing (wraps ParseErrorCode)
88 /*!
89     \ingroup RAPIDJSON_ERRORS
90     \code
91         Document doc;
92         ParseResult ok = doc.Parse("[42]");
93         if (!ok) {
94             fprintf(stderr, "JSON parse error: %s (%u)",
95                     GetParseError_En(ok.Code()), ok.Offset());
96             exit(EXIT_FAILURE);
97         }
98     \endcode
99     \see GenericReader::Parse, GenericDocument::Parse
100 */
101 struct ParseResult {
102 
103     //! Default constructor, no error.
ParseResultParseResult104     ParseResult() : code_(kParseErrorNone), offset_(0) {}
105     //! Constructor to set an error.
ParseResultParseResult106     ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
107 
108     //! Get the error code.
CodeParseResult109     ParseErrorCode Code() const { return code_; }
110     //! Get the error offset, if \ref IsError(), 0 otherwise.
OffsetParseResult111     size_t Offset() const { return offset_; }
112 
113     //! Conversion to \c bool, returns \c true, iff !\ref IsError().
114     operator bool() const { return !IsError(); }
115     //! Whether the result is an error.
IsErrorParseResult116     bool IsError() const { return code_ != kParseErrorNone; }
117 
118     bool operator==(const ParseResult& that) const { return code_ == that.code_; }
119     bool operator==(ParseErrorCode code) const { return code_ == code; }
120     friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
121 
122     //! Reset error code.
ClearParseResult123     void Clear() { Set(kParseErrorNone); }
124     //! Update error code and offset.
125     void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
126 
127 private:
128     ParseErrorCode code_;
129     size_t offset_;
130 };
131 
132 //! Function pointer type of GetParseError().
133 /*! \ingroup RAPIDJSON_ERRORS
134 
135     This is the prototype for \c GetParseError_X(), where \c X is a locale.
136     User can dynamically change locale in runtime, e.g.:
137 \code
138     GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
139     const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
140 \endcode
141 */
142 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
143 
144 RAPIDJSON_NAMESPACE_END
145 
146 #endif // RAPIDJSON_ERROR_ERROR_H__
147