All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
error.h
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_ERROR_ERROR_H__
22 #define RAPIDJSON_ERROR_ERROR_H__
23 
24 ///////////////////////////////////////////////////////////////////////////////
25 // RAPIDJSON_ERROR_CHARTYPE
26 
27 //! Character type of error messages.
28 /*! The default charater type is char.
29  On Windows, user can define this macro as TCHAR for supporting both
30  unicode/non-unicode settings.
31 */
32 #ifndef RAPIDJSON_ERROR_CHARTYPE
33 #define RAPIDJSON_ERROR_CHARTYPE char
34 #endif
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // RAPIDJSON_ERROR_STRING
38 
39 //! Macro for converting string literial to RAPIDJSON_ERROR_CHARTYPE[].
40 /*! By default this conversion macro does nothing.
41  On Windows, user can define this macro as _T(x) for supporting both
42  unicode/non-unicode settings.
43 */
44 #ifndef RAPIDJSON_ERROR_STRING
45 #define RAPIDJSON_ERROR_STRING(x) x
46 #endif
47 
48 namespace rapidjson {
49 
50 ///////////////////////////////////////////////////////////////////////////////
51 // ParseErrorCode
52 
53 //! Error code of parsing.
54 /*! \see GenericReader::Parse, GenericReader::GetParseErrorCode
55 */
57  kParseErrorNone = 0, //!< No error.
58 
59  kParseErrorDocumentEmpty, //!< The document is empty.
60  kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
61 
62  kParseErrorValueInvalid, //!< Invalid value.
63 
64  kParseErrorObjectMissName, //!< Missing a name for object member.
65  kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
66  kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
67 
68  kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
69 
70  kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
71  kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
72  kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
73  kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
74  kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
75 
76  kParseErrorNumberTooBig, //!< Number too big to be stored in double.
77  kParseErrorNumberMissFraction, //!< Miss fraction part in number.
78  kParseErrorNumberMissExponent, //!< Miss exponent in number.
79 
80  kParseErrorTermination, //!< Parsing was terminated.
81  kParseErrorUnspecificSyntaxError, //!< Unspecific syntax error.
82 };
83 
84 //! Result of parsing (wraps ParseErrorCode)
85 /*!
86  \code
87  Document doc;
88  ParseResult ok = doc.Parse("[42]");
89  if (!ok) {
90  fprintf(stderr, "JSON parse error: %s (%u)",
91  GetParseError_En(ok.Code()), ok.Offset());
92  exit(EXIT_FAILURE);
93  }
94  \endcode
95  \see GenericReader::Parse, GenericDocument::Parse
96 */
97 struct ParseResult {
98 
99  //! Default constructor, no error.
100  ParseResult() : code_(kParseErrorNone), offset_(0) {}
101  //! Constructor to set an error.
102  ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
103 
104  //! Get the error code.
105  ParseErrorCode Code() const { return code_; }
106  //! Get the error offset, if \ref IsError(), 0 otherwise.
107  size_t Offset() const { return offset_; }
108 
109  //! Conversion to \c bool, returns \c true, iff !\ref IsError().
110  operator bool() const { return !IsError(); }
111  //! Whether the result is an error.
112  bool IsError() const { return code_ != kParseErrorNone; }
113 
114  bool operator==(const ParseResult& that) const { return code_ == that.code_; }
115  bool operator==(ParseErrorCode code) const { return code_ == code; }
116  friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
117 
118  //! Reset error code.
119  void Clear() { Set(kParseErrorNone); }
120  //! Update error code and offset.
121  void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
122 
123 private:
124  ParseErrorCode code_;
125  size_t offset_;
126 };
127 
128 //! Function pointer type of GetParseError().
129 /*! This is the prototype for GetParseError_X(), where X is a locale.
130  User can dynamically change locale in runtime, e.g.:
131 
132 \code
133  GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
134  const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
135 \endcode
136 */
137 
138 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
139 
140 } // namespace rapidjson
141 
142 #endif // RAPIDJSON_ERROR_ERROR_H__
Result of parsing (wraps ParseErrorCode)
Definition: error.h:97
void Set(ParseErrorCode code, size_t offset=0)
Update error code and offset.
Definition: error.h:121
The document root must not follow by other values.
Definition: error.h:60
No error.
Definition: error.h:57
ParseResult()
Default constructor, no error.
Definition: error.h:100
Invalid value.
Definition: error.h:62
Miss fraction part in number.
Definition: error.h:77
The document is empty.
Definition: error.h:59
The surrogate pair in string is invalid.
Definition: error.h:71
Missing a colon after a name of object member.
Definition: error.h:65
Missing a closing quotation mark in string.
Definition: error.h:73
Invalid escape character in string.
Definition: error.h:72
Missing a name for object member.
Definition: error.h:64
Number too big to be stored in double.
Definition: error.h:76
Invalid encoding in string.
Definition: error.h:74
bool IsError() const
Whether the result is an error.
Definition: error.h:112
ParseErrorCode Code() const
Get the error code.
Definition: error.h:105
Missing a comma or '}' after an object member.
Definition: error.h:66
ParseErrorCode
Error code of parsing.
Definition: error.h:56
Unspecific syntax error.
Definition: error.h:81
Parsing was terminated.
Definition: error.h:80
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition: error.h:107
ParseResult(ParseErrorCode code, size_t offset)
Constructor to set an error.
Definition: error.h:102
Incorrect hex digit after \u escape in string.
Definition: error.h:70
Miss exponent in number.
Definition: error.h:78
Missing a comma or ']' after an array element.
Definition: error.h:68
void Clear()
Reset error code.
Definition: error.h:119