| 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 |
OperandMismatchError, |
| 70 |
SelectorError, |
| 71 |
UnknownFunction, |
| 72 |
}; |
| 73 |
|
| 74 |
using StaticError = Error<StaticErrorType>; |
| 75 |
using DynamicError = Error<DynamicErrorType>; |
| 76 |
|
| 77 |
// These explicit instantiations have to come before the |
| 78 |
// destructor definitions |
| 79 |
template<> |
| 80 |
Error<StaticErrorType>::~Error(); |
| 81 |
template<> |
| 82 |
Error<DynamicErrorType>::~Error(); |
| 83 |
|
| 84 |
class StaticErrors : public UObject { |
| 85 |
private: |
| 86 |
friend class DynamicErrors; |
| 87 |
|
| 88 |
LocalPointer<UVector> syntaxAndDataModelErrors; |
| 89 |
bool dataModelError = false; |
| 90 |
bool missingSelectorAnnotationError = false; |
| 91 |
bool syntaxError = false; |
| 92 |
|
| 93 |
public: |
| 94 |
StaticErrors(UErrorCode&); |
| 95 |
|
| 96 |
void setMissingSelectorAnnotation(UErrorCode&); |
| 97 |
void setDuplicateOptionName(UErrorCode&); |
| 98 |
void addSyntaxError(UErrorCode&); |
| 99 |
bool hasDataModelError() const { return dataModelError; } |
| 100 |
bool hasSyntaxError() const { return syntaxError; } |
| 101 |
bool hasMissingSelectorAnnotationError() const { return missingSelectorAnnotationError; } |
| 102 |
void addError(StaticError&&, UErrorCode&); |
| 103 |
void checkErrors(UErrorCode&) const; |
| 104 |
|
| 105 |
void clear(); |
| 106 |
const StaticError& first() const; |
| 107 |
StaticErrors(const StaticErrors&, UErrorCode&); |
| 108 |
StaticErrors(StaticErrors&&) noexcept; |
| 109 |
virtual ~StaticErrors(); |
| 110 |
}; // class StaticErrors |
| 111 |
|
| 112 |
class DynamicErrors : public UObject { |
| 113 |
private: |
| 114 |
const StaticErrors& staticErrors; |
| 115 |
LocalPointer<UVector> resolutionAndFormattingErrors; |
| 116 |
bool formattingError = false; |
| 117 |
bool selectorError = false; |
| 118 |
bool unknownFunctionError = false; |
| 119 |
bool unresolvedVariableError = false; |
| 120 |
|
| 121 |
public: |
| 122 |
DynamicErrors(const StaticErrors&, UErrorCode&); |
| 123 |
|
| 124 |
int32_t count() const; |
| 125 |
void setSelectorError(const FunctionName&, UErrorCode&); |
| 126 |
void setUnresolvedVariable(const VariableName&, UErrorCode&); |
| 127 |
void setUnknownFunction(const FunctionName&, UErrorCode&); |
| 128 |
void setFormattingError(const FunctionName&, UErrorCode&); |
| 129 |
// Used when the name of the offending formatter is unknown |
| 130 |
void setFormattingError(UErrorCode&); |
| 131 |
void setOperandMismatchError(const FunctionName&, UErrorCode&); |
| 132 |
bool hasDataModelError() const { return staticErrors.hasDataModelError(); } |
| 133 |
bool hasFormattingError() const { return formattingError; } |
| 134 |
bool hasSelectorError() const { return selectorError; } |
| 135 |
bool hasSyntaxError() const { return staticErrors.hasSyntaxError(); } |
| 136 |
bool hasUnknownFunctionError() const { return unknownFunctionError; } |
| 137 |
bool hasMissingSelectorAnnotationError() const { return staticErrors.hasMissingSelectorAnnotationError(); } |
| 138 |
bool hasUnresolvedVariableError() const { return unresolvedVariableError; } |
| 139 |
void addError(DynamicError&&, UErrorCode&); |
| 140 |
void checkErrors(UErrorCode&) const; |
| 141 |
bool hasError() const; |
| 142 |
bool hasStaticError() const; |
| 143 |
|
| 144 |
const DynamicError& first() const; |
| 145 |
virtual ~DynamicErrors(); |
| 146 |
}; // class DynamicErrors |
| 147 |
|
| 148 |
} // namespace message2 |
| 149 |
|
| 150 |
U_NAMESPACE_END |
| 151 |
|
| 152 |
#endif /* #if !UCONFIG_NO_MF2 */ |
| 153 |
|
| 154 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |
| 155 |
|
| 156 |
#endif /* #if !UCONFIG_NO_NORMALIZATION */ |
| 157 |
|
| 158 |
#endif /* U_SHOW_CPLUSPLUS_API */ |
| 159 |
|
| 160 |
#endif // MESSAGEFORMAT2_ERRORS_H |
| 161 |
|
| 162 |
#endif // U_HIDE_DEPRECATED_API |
| 163 |
// eof |