error.h 6.3 KB

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