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_FORMATTING 19 20 #if !UCONFIG_NO_MF2 21 22 #include "unicode/messageformat2_data_model_names.h" 23 #include "unicode/unistr.h" 24 25 #include "uvector.h" 26 27 U_NAMESPACE_BEGIN 28 29 namespace message2 { 30 31 using namespace data_model; 32 33 // Errors 34 // ---------- 35 36 class DynamicErrors; 37 class StaticErrors; 38 39 // Internal class -- used as a private field in MessageFormatter 40 template <typename ErrorType> 41 class Error : public UObject { 42 public: Error(ErrorType ty)43 Error(ErrorType ty) : type(ty) {} Error(ErrorType ty,const UnicodeString & s)44 Error(ErrorType ty, const UnicodeString& s) : type(ty), contents(s) {} 45 virtual ~Error(); 46 private: 47 friend class DynamicErrors; 48 friend class StaticErrors; 49 50 ErrorType type; 51 UnicodeString contents; 52 }; // class Error 53 54 enum StaticErrorType { 55 DuplicateDeclarationError, 56 DuplicateOptionName, 57 DuplicateVariant, 58 MissingSelectorAnnotation, 59 NonexhaustivePattern, 60 SyntaxError, 61 VariantKeyMismatchError 62 }; 63 64 enum DynamicErrorType { 65 UnresolvedVariable, 66 FormattingError, 67 OperandMismatchError, 68 SelectorError, 69 UnknownFunction, 70 }; 71 72 using StaticError = Error<StaticErrorType>; 73 using DynamicError = Error<DynamicErrorType>; 74 75 // These explicit instantiations have to come before the 76 // destructor definitions 77 template<> 78 Error<StaticErrorType>::~Error(); 79 template<> 80 Error<DynamicErrorType>::~Error(); 81 82 class StaticErrors : public UObject { 83 private: 84 friend class DynamicErrors; 85 86 LocalPointer<UVector> syntaxAndDataModelErrors; 87 bool dataModelError = false; 88 bool missingSelectorAnnotationError = false; 89 bool syntaxError = false; 90 91 public: 92 StaticErrors(UErrorCode&); 93 94 void setMissingSelectorAnnotation(UErrorCode&); 95 void setDuplicateOptionName(UErrorCode&); 96 void addSyntaxError(UErrorCode&); hasDataModelError()97 bool hasDataModelError() const { return dataModelError; } hasSyntaxError()98 bool hasSyntaxError() const { return syntaxError; } hasMissingSelectorAnnotationError()99 bool hasMissingSelectorAnnotationError() const { return missingSelectorAnnotationError; } 100 void addError(StaticError&&, UErrorCode&); 101 void checkErrors(UErrorCode&) const; 102 103 void clear(); 104 const StaticError& first() const; 105 StaticErrors(const StaticErrors&, UErrorCode&); 106 StaticErrors(StaticErrors&&) noexcept; 107 virtual ~StaticErrors(); 108 }; // class StaticErrors 109 110 class DynamicErrors : public UObject { 111 private: 112 const StaticErrors& staticErrors; 113 LocalPointer<UVector> resolutionAndFormattingErrors; 114 bool formattingError = false; 115 bool selectorError = false; 116 bool unknownFunctionError = false; 117 bool unresolvedVariableError = false; 118 119 public: 120 DynamicErrors(const StaticErrors&, UErrorCode&); 121 122 int32_t count() const; 123 void setSelectorError(const FunctionName&, UErrorCode&); 124 void setUnresolvedVariable(const VariableName&, UErrorCode&); 125 void setUnknownFunction(const FunctionName&, UErrorCode&); 126 void setFormattingError(const FunctionName&, UErrorCode&); 127 // Used when the name of the offending formatter is unknown 128 void setFormattingError(UErrorCode&); 129 void setOperandMismatchError(const FunctionName&, UErrorCode&); hasDataModelError()130 bool hasDataModelError() const { return staticErrors.hasDataModelError(); } hasFormattingError()131 bool hasFormattingError() const { return formattingError; } hasSelectorError()132 bool hasSelectorError() const { return selectorError; } hasSyntaxError()133 bool hasSyntaxError() const { return staticErrors.hasSyntaxError(); } hasUnknownFunctionError()134 bool hasUnknownFunctionError() const { return unknownFunctionError; } hasMissingSelectorAnnotationError()135 bool hasMissingSelectorAnnotationError() const { return staticErrors.hasMissingSelectorAnnotationError(); } hasUnresolvedVariableError()136 bool hasUnresolvedVariableError() const { return unresolvedVariableError; } 137 void addError(DynamicError&&, UErrorCode&); 138 void checkErrors(UErrorCode&) const; 139 bool hasError() const; 140 bool hasStaticError() const; 141 142 const DynamicError& first() const; 143 virtual ~DynamicErrors(); 144 }; // class DynamicErrors 145 146 } // namespace message2 147 148 U_NAMESPACE_END 149 150 #endif /* #if !UCONFIG_NO_MF2 */ 151 152 #endif /* #if !UCONFIG_NO_FORMATTING */ 153 154 #endif /* U_SHOW_CPLUSPLUS_API */ 155 156 #endif // MESSAGEFORMAT2_ERRORS_H 157 158 #endif // U_HIDE_DEPRECATED_API 159 // eof 160