error.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. public:
  88. //! Default constructor, no error.
  89. ParseResult() : code_(kParseErrorNone), offset_(0) {}
  90. //! Constructor to set an error.
  91. ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
  92. //! Get the error code.
  93. ParseErrorCode Code() const { return code_; }
  94. //! Get the error offset, if \ref IsError(), 0 otherwise.
  95. size_t Offset() const { return offset_; }
  96. //! Conversion to \c bool, returns \c true, iff !\ref IsError().
  97. operator bool() const { return !IsError(); }
  98. //! Whether the result is an error.
  99. bool IsError() const { return code_ != kParseErrorNone; }
  100. bool operator==(const ParseResult& that) const { return code_ == that.code_; }
  101. bool operator==(ParseErrorCode code) const { return code_ == code; }
  102. friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
  103. //! Reset error code.
  104. void Clear() { Set(kParseErrorNone); }
  105. //! Update error code and offset.
  106. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
  107. private:
  108. ParseErrorCode code_;
  109. size_t offset_;
  110. };
  111. //! Function pointer type of GetParseError().
  112. /*! \ingroup RAPIDJSON_ERRORS
  113. This is the prototype for \c GetParseError_X(), where \c X is a locale.
  114. User can dynamically change locale in runtime, e.g.:
  115. \code
  116. GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
  117. const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
  118. \endcode
  119. */
  120. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
  121. RAPIDJSON_NAMESPACE_END
  122. #ifdef __clang__
  123. RAPIDJSON_DIAG_POP
  124. #endif
  125. #endif // RAPIDJSON_ERROR_ERROR_H_