error.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef RAPIDJSON_ERROR_ERROR_H_
  15. #define RAPIDJSON_ERROR_ERROR_H_
  16. #include "../rapidjson.h"
  17. #ifdef __clang__
  18. RAPIDJSON_DIAG_PUSH
  19. RAPIDJSON_DIAG_OFF(padded)
  20. #endif
  21. /*! \file error.h */
  22. /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // RAPIDJSON_ERROR_CHARTYPE
  25. //! Character type of error messages.
  26. /*! \ingroup RAPIDJSON_ERRORS
  27. The default character type is \c char.
  28. On Windows, user can define this macro as \c TCHAR for supporting both
  29. unicode/non-unicode settings.
  30. */
  31. #ifndef RAPIDJSON_ERROR_CHARTYPE
  32. #define RAPIDJSON_ERROR_CHARTYPE char
  33. #endif
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // RAPIDJSON_ERROR_STRING
  36. //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
  37. /*! \ingroup RAPIDJSON_ERRORS
  38. By default this conversion macro does nothing.
  39. On Windows, user can define this macro as \c _T(x) for supporting both
  40. unicode/non-unicode settings.
  41. */
  42. #ifndef RAPIDJSON_ERROR_STRING
  43. #define RAPIDJSON_ERROR_STRING(x) x
  44. #endif
  45. RAPIDJSON_NAMESPACE_BEGIN
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // ParseErrorCode
  48. //! Error code of parsing.
  49. /*! \ingroup RAPIDJSON_ERRORS
  50. \see GenericReader::Parse, GenericReader::GetParseErrorCode
  51. */
  52. enum ParseErrorCode {
  53. kParseErrorNone = 0, //!< No error.
  54. kParseErrorDocumentEmpty, //!< The document is empty.
  55. kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
  56. kParseErrorValueInvalid, //!< Invalid value.
  57. kParseErrorObjectMissName, //!< Missing a name for object member.
  58. kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
  59. kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
  60. kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
  61. kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
  62. kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
  63. kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
  64. kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
  65. kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
  66. kParseErrorNumberTooBig, //!< Number too big to be stored in double.
  67. kParseErrorNumberMissFraction, //!< Miss fraction part in number.
  68. kParseErrorNumberMissExponent, //!< Miss exponent in number.
  69. kParseErrorTermination, //!< Parsing was terminated.
  70. kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
  71. };
  72. //! Result of parsing (wraps ParseErrorCode)
  73. /*!
  74. \ingroup RAPIDJSON_ERRORS
  75. \code
  76. Document doc;
  77. ParseResult ok = doc.Parse("[42]");
  78. if (!ok) {
  79. fprintf(stderr, "JSON parse error: %s (%u)",
  80. GetParseError_En(ok.Code()), ok.Offset());
  81. exit(EXIT_FAILURE);
  82. }
  83. \endcode
  84. \see GenericReader::Parse, GenericDocument::Parse
  85. */
  86. struct ParseResult {
  87. //!! Unspecified boolean type
  88. typedef bool (ParseResult::*BooleanType)() const;
  89. public:
  90. //! Default constructor, no error.
  91. ParseResult() : code_(kParseErrorNone), offset_(0) {}
  92. //! Constructor to set an error.
  93. ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
  94. //! Get the error code.
  95. ParseErrorCode Code() const { return code_; }
  96. //! Get the error offset, if \ref IsError(), 0 otherwise.
  97. size_t Offset() const { return offset_; }
  98. //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError().
  99. operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; }
  100. //! Whether the result is an error.
  101. bool IsError() const { return code_ != kParseErrorNone; }
  102. bool operator==(const ParseResult& that) const { return code_ == that.code_; }
  103. bool operator==(ParseErrorCode code) const { return code_ == code; }
  104. friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
  105. bool operator!=(const ParseResult& that) const { return !(*this == that); }
  106. bool operator!=(ParseErrorCode code) const { return !(*this == code); }
  107. friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; }
  108. //! Reset error code.
  109. void Clear() { Set(kParseErrorNone); }
  110. //! Update error code and offset.
  111. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
  112. private:
  113. ParseErrorCode code_;
  114. size_t offset_;
  115. };
  116. //! Function pointer type of GetParseError().
  117. /*! \ingroup RAPIDJSON_ERRORS
  118. This is the prototype for \c GetParseError_X(), where \c X is a locale.
  119. User can dynamically change locale in runtime, e.g.:
  120. \code
  121. GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
  122. const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
  123. \endcode
  124. */
  125. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
  126. RAPIDJSON_NAMESPACE_END
  127. #ifdef __clang__
  128. RAPIDJSON_DIAG_POP
  129. #endif
  130. #endif // RAPIDJSON_ERROR_ERROR_H_