messageformat2_errors.h (5725B)
1 // © 2024 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 4 #include "unicode/utypes.h" 5 6 #ifndef U_HIDE_DEPRECATED_API 7 8 #ifndef MESSAGEFORMAT2_ERRORS_H 9 #define MESSAGEFORMAT2_ERRORS_H 10 11 #if U_SHOW_CPLUSPLUS_API 12 13 /** 14 * \file 15 * \brief C++ API: Formats messages using the draft MessageFormat 2.0. 16 */ 17 18 #if !UCONFIG_NO_NORMALIZATION 19 20 #if !UCONFIG_NO_FORMATTING 21 22 #if !UCONFIG_NO_MF2 23 24 #include "unicode/messageformat2_data_model_names.h" 25 #include "unicode/unistr.h" 26 27 #include "uvector.h" 28 29 U_NAMESPACE_BEGIN 30 31 namespace message2 { 32 33 using namespace data_model; 34 35 // Errors 36 // ---------- 37 38 class DynamicErrors; 39 class StaticErrors; 40 41 // Internal class -- used as a private field in MessageFormatter 42 template <typename ErrorType> 43 class Error : public UObject { 44 public: 45 Error(ErrorType ty) : type(ty) {} 46 Error(ErrorType ty, const UnicodeString& s) : type(ty), contents(s) {} 47 virtual ~Error(); 48 private: 49 friend class DynamicErrors; 50 friend class StaticErrors; 51 52 ErrorType type; 53 UnicodeString contents; 54 }; // class Error 55 56 enum StaticErrorType { 57 DuplicateDeclarationError, 58 DuplicateOptionName, 59 DuplicateVariant, 60 MissingSelectorAnnotation, 61 NonexhaustivePattern, 62 SyntaxError, 63 VariantKeyMismatchError 64 }; 65 66 enum DynamicErrorType { 67 UnresolvedVariable, 68 FormattingError, 69 BadOptionError, 70 /** 71 This is used to signal errors from :number and :integer when a 72 bad `select` option is passed. In this case, fallback output 73 is not used, so it must be distinguished from a regular bad 74 option error (but it maps to a bad option error in the final 75 error code). 76 See https://github.com/unicode-org/message-format-wg/blob/main/spec/functions/number.md#number-selection 77 "The formatting of the _resolved value_ is not affected by the `select` option.") 78 */ 79 RecoverableBadOptionError, 80 OperandMismatchError, 81 SelectorError, 82 UnknownFunction, 83 }; 84 85 using StaticError = Error<StaticErrorType>; 86 using DynamicError = Error<DynamicErrorType>; 87 88 // These explicit instantiations have to come before the 89 // destructor definitions 90 template<> 91 Error<StaticErrorType>::~Error(); 92 template<> 93 Error<DynamicErrorType>::~Error(); 94 95 class StaticErrors : public UObject { 96 private: 97 friend class DynamicErrors; 98 99 LocalPointer<UVector> syntaxAndDataModelErrors; 100 bool dataModelError = false; 101 bool missingSelectorAnnotationError = false; 102 bool syntaxError = false; 103 104 public: 105 StaticErrors(UErrorCode&); 106 107 void setMissingSelectorAnnotation(UErrorCode&); 108 void setDuplicateOptionName(UErrorCode&); 109 void addSyntaxError(UErrorCode&); 110 bool hasDataModelError() const { return dataModelError; } 111 bool hasSyntaxError() const { return syntaxError; } 112 bool hasMissingSelectorAnnotationError() const { return missingSelectorAnnotationError; } 113 void addError(StaticError&&, UErrorCode&); 114 void checkErrors(UErrorCode&) const; 115 116 void clear(); 117 const StaticError& first() const; 118 StaticErrors(const StaticErrors&, UErrorCode&); 119 StaticErrors(StaticErrors&&) noexcept; 120 virtual ~StaticErrors(); 121 }; // class StaticErrors 122 123 class DynamicErrors : public UObject { 124 private: 125 const StaticErrors& staticErrors; 126 LocalPointer<UVector> resolutionAndFormattingErrors; 127 bool formattingError = false; 128 bool badOptionError = false; 129 bool selectorError = false; 130 bool unknownFunctionError = false; 131 bool unresolvedVariableError = false; 132 133 public: 134 DynamicErrors(const StaticErrors&, UErrorCode&); 135 136 int32_t count() const; 137 void setSelectorError(const FunctionName&, UErrorCode&); 138 void setUnresolvedVariable(const VariableName&, UErrorCode&); 139 void setUnknownFunction(const FunctionName&, UErrorCode&); 140 void setFormattingError(const FunctionName&, UErrorCode&); 141 // Used when the name of the offending formatter is unknown 142 void setFormattingError(UErrorCode&); 143 void setBadOption(const FunctionName&, UErrorCode&); 144 void setRecoverableBadOption(const FunctionName&, UErrorCode&); 145 void setOperandMismatchError(const FunctionName&, UErrorCode&); 146 bool hasDataModelError() const { return staticErrors.hasDataModelError(); } 147 bool hasFormattingError() const { return formattingError; } 148 bool hasBadOptionError() const { return badOptionError; } 149 bool hasSelectorError() const { return selectorError; } 150 bool hasSyntaxError() const { return staticErrors.hasSyntaxError(); } 151 bool hasUnknownFunctionError() const { return unknownFunctionError; } 152 bool hasMissingSelectorAnnotationError() const { return staticErrors.hasMissingSelectorAnnotationError(); } 153 bool hasUnresolvedVariableError() const { return unresolvedVariableError; } 154 void addError(DynamicError&&, UErrorCode&); 155 void checkErrors(UErrorCode&) const; 156 bool hasError() const; 157 bool hasStaticError() const; 158 159 const DynamicError& first() const; 160 virtual ~DynamicErrors(); 161 }; // class DynamicErrors 162 163 } // namespace message2 164 165 U_NAMESPACE_END 166 167 #endif /* #if !UCONFIG_NO_MF2 */ 168 169 #endif /* #if !UCONFIG_NO_FORMATTING */ 170 171 #endif /* #if !UCONFIG_NO_NORMALIZATION */ 172 173 #endif /* U_SHOW_CPLUSPLUS_API */ 174 175 #endif // MESSAGEFORMAT2_ERRORS_H 176 177 #endif // U_HIDE_DEPRECATED_API 178 // eof