| 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 | #if !UCONFIG_NO_NORMALIZATION |
| 7 | |
| 8 | #if !UCONFIG_NO_FORMATTING |
| 9 | |
| 10 | #if !UCONFIG_NO_MF2 |
| 11 | |
| 12 | #include "messageformat2_allocation.h" |
| 13 | #include "messageformat2_errors.h" |
| 14 | #include "messageformat2_macros.h" |
| 15 | #include "uvector.h" // U_ASSERT |
| 16 | |
| 17 | U_NAMESPACE_BEGIN |
| 18 | |
| 19 | namespace message2 { |
| 20 | |
| 21 | // Errors |
| 22 | // ----------- |
| 23 | |
| 24 | void DynamicErrors::setFormattingError(const FunctionName& formatterName, UErrorCode& status) { |
| 25 | addError(DynamicError(DynamicErrorType::FormattingError, formatterName), status); |
| 26 | } |
| 27 | |
| 28 | void DynamicErrors::setFormattingError(UErrorCode& status) { |
| 29 | addError(DynamicError(DynamicErrorType::FormattingError, UnicodeString("unknown formatter")), status); |
| 30 | } |
| 31 | |
| 32 | void DynamicErrors::setOperandMismatchError(const FunctionName& formatterName, UErrorCode& status) { |
| 33 | addError(DynamicError(DynamicErrorType::OperandMismatchError, formatterName), status); |
| 34 | } |
| 35 | |
| 36 | void StaticErrors::setDuplicateOptionName(UErrorCode& status) { |
| 37 | addError(StaticError(StaticErrorType::DuplicateOptionName), status); |
| 38 | } |
| 39 | |
| 40 | void StaticErrors::setMissingSelectorAnnotation(UErrorCode& status) { |
| 41 | addError(StaticError(StaticErrorType::MissingSelectorAnnotation), status); |
| 42 | } |
| 43 | |
| 44 | void DynamicErrors::setSelectorError(const FunctionName& selectorName, UErrorCode& status) { |
| 45 | addError(DynamicError(DynamicErrorType::SelectorError, selectorName), status); |
| 46 | } |
| 47 | |
| 48 | void DynamicErrors::setUnknownFunction(const FunctionName& functionName, UErrorCode& status) { |
| 49 | addError(DynamicError(DynamicErrorType::UnknownFunction, functionName), status); |
| 50 | } |
| 51 | |
| 52 | void DynamicErrors::setUnresolvedVariable(const VariableName& v, UErrorCode& status) { |
| 53 | addError(DynamicError(DynamicErrorType::UnresolvedVariable, v), status); |
| 54 | } |
| 55 | |
| 56 | DynamicErrors::DynamicErrors(const StaticErrors& e, UErrorCode& status) : staticErrors(e) { |
| 57 | resolutionAndFormattingErrors.adoptInstead(createUVector(status)); |
| 58 | } |
| 59 | |
| 60 | StaticErrors::StaticErrors(UErrorCode& status) { |
| 61 | syntaxAndDataModelErrors.adoptInstead(createUVector(status)); |
| 62 | } |
| 63 | |
| 64 | StaticErrors::StaticErrors(StaticErrors&& other) noexcept { |
| 65 | U_ASSERT(other.syntaxAndDataModelErrors.isValid()); |
| 66 | syntaxAndDataModelErrors.adoptInstead(other.syntaxAndDataModelErrors.orphan()); |
| 67 | dataModelError = other.dataModelError; |
| 68 | missingSelectorAnnotationError = other.missingSelectorAnnotationError; |
| 69 | syntaxError = other.syntaxError; |
| 70 | } |
| 71 | |
| 72 | StaticErrors::StaticErrors(const StaticErrors& other, UErrorCode& errorCode) { |
| 73 | CHECK_ERROR(errorCode); |
| 74 | |
| 75 | U_ASSERT(other.syntaxAndDataModelErrors.isValid()); |
| 76 | syntaxAndDataModelErrors.adoptInstead(createUVector(errorCode)); |
| 77 | CHECK_ERROR(errorCode); |
| 78 | for (int32_t i = 0; i < other.syntaxAndDataModelErrors->size(); i++) { |
| 79 | StaticError* e = static_cast<StaticError*>(other.syntaxAndDataModelErrors->elementAt(i)); |
| 80 | U_ASSERT(e != nullptr); |
| 81 | StaticError* copy = new StaticError(*e); |
| 82 | if (copy == nullptr) { |
| 83 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 84 | return; |
| 85 | } |
| 86 | syntaxAndDataModelErrors->adoptElement(copy, errorCode); |
| 87 | } |
| 88 | dataModelError = other.dataModelError; |
| 89 | missingSelectorAnnotationError = other.missingSelectorAnnotationError; |
| 90 | syntaxError = other.syntaxError; |
| 91 | } |
| 92 | |
| 93 | int32_t DynamicErrors::count() const { |
| 94 | U_ASSERT(resolutionAndFormattingErrors.isValid() && staticErrors.syntaxAndDataModelErrors.isValid()); |
| 95 | return resolutionAndFormattingErrors->size() + staticErrors.syntaxAndDataModelErrors->size(); |
| 96 | } |
| 97 | |
| 98 | bool DynamicErrors::hasError() const { |
| 99 | return count() > 0; |
| 100 | } |
| 101 | |
| 102 | bool DynamicErrors::hasStaticError() const { |
| 103 | U_ASSERT(staticErrors.syntaxAndDataModelErrors.isValid()); |
| 104 | return staticErrors.syntaxAndDataModelErrors->size() > 0; |
| 105 | } |
| 106 | |
| 107 | const DynamicError& DynamicErrors::first() const { |
| 108 | U_ASSERT(resolutionAndFormattingErrors->size() > 0); |
| 109 | return *static_cast<DynamicError*>(resolutionAndFormattingErrors->elementAt(0)); |
| 110 | } |
| 111 | |
| 112 | void DynamicErrors::checkErrors(UErrorCode& status) const { |
| 113 | if (status != U_ZERO_ERROR) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // Just handle the first error |
| 118 | // TODO: Eventually want to return all errors to caller |
| 119 | if (count() == 0) { |
| 120 | return; |
| 121 | } |
| 122 | staticErrors.checkErrors(status); |
| 123 | if (U_FAILURE(status)) { |
| 124 | return; |
| 125 | } |
| 126 | U_ASSERT(resolutionAndFormattingErrors->size() > 0); |
| 127 | switch (first().type) { |
| 128 | case DynamicErrorType::UnknownFunction: { |
| 129 | status = U_MF_UNKNOWN_FUNCTION_ERROR; |
| 130 | break; |
| 131 | } |
| 132 | case DynamicErrorType::UnresolvedVariable: { |
| 133 | status = U_MF_UNRESOLVED_VARIABLE_ERROR; |
| 134 | break; |
| 135 | } |
| 136 | case DynamicErrorType::FormattingError: { |
| 137 | status = U_MF_FORMATTING_ERROR; |
| 138 | break; |
| 139 | } |
| 140 | case DynamicErrorType::OperandMismatchError: { |
| 141 | status = U_MF_OPERAND_MISMATCH_ERROR; |
| 142 | break; |
| 143 | } |
| 144 | case DynamicErrorType::SelectorError: { |
| 145 | status = U_MF_SELECTOR_ERROR; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void StaticErrors::addSyntaxError(UErrorCode& status) { |
| 152 | addError(StaticError(StaticErrorType::SyntaxError), status); |
| 153 | } |
| 154 | |
| 155 | void StaticErrors::addError(StaticError&& e, UErrorCode& status) { |
| 156 | CHECK_ERROR(status); |
| 157 | |
| 158 | StaticErrorType type = e.type; |
| 159 | |
| 160 | void* errorP = static_cast<void*>(create<StaticError>(std::move(e), status)); |
| 161 | U_ASSERT(syntaxAndDataModelErrors.isValid()); |
| 162 | |
| 163 | switch (type) { |
| 164 | case StaticErrorType::SyntaxError: { |
| 165 | syntaxError = true; |
| 166 | break; |
| 167 | } |
| 168 | case StaticErrorType::DuplicateDeclarationError: { |
| 169 | dataModelError = true; |
| 170 | break; |
| 171 | } |
| 172 | case StaticErrorType::DuplicateOptionName: { |
| 173 | dataModelError = true; |
| 174 | break; |
| 175 | } |
| 176 | case StaticErrorType::VariantKeyMismatchError: { |
| 177 | dataModelError = true; |
| 178 | break; |
| 179 | } |
| 180 | case StaticErrorType::DuplicateVariant: { |
| 181 | dataModelError = true; |
| 182 | break; |
| 183 | } |
| 184 | case StaticErrorType::NonexhaustivePattern: { |
| 185 | dataModelError = true; |
| 186 | break; |
| 187 | } |
| 188 | case StaticErrorType::MissingSelectorAnnotation: { |
| 189 | missingSelectorAnnotationError = true; |
| 190 | dataModelError = true; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | syntaxAndDataModelErrors->adoptElement(errorP, status); |
| 195 | } |
| 196 | |
| 197 | void DynamicErrors::addError(DynamicError&& e, UErrorCode& status) { |
| 198 | CHECK_ERROR(status); |
| 199 | |
| 200 | DynamicErrorType type = e.type; |
| 201 | |
| 202 | void* errorP = static_cast<void*>(create<DynamicError>(std::move(e), status)); |
| 203 | U_ASSERT(resolutionAndFormattingErrors.isValid()); |
| 204 | |
| 205 | switch (type) { |
| 206 | case DynamicErrorType::UnresolvedVariable: { |
| 207 | unresolvedVariableError = true; |
| 208 | resolutionAndFormattingErrors->adoptElement(errorP, status); |
| 209 | break; |
| 210 | } |
| 211 | case DynamicErrorType::FormattingError: { |
| 212 | formattingError = true; |
| 213 | resolutionAndFormattingErrors->adoptElement(errorP, status); |
| 214 | break; |
| 215 | } |
| 216 | case DynamicErrorType::OperandMismatchError: { |
| 217 | formattingError = true; |
| 218 | resolutionAndFormattingErrors->adoptElement(errorP, status); |
| 219 | break; |
| 220 | } |
| 221 | case DynamicErrorType::SelectorError: { |
| 222 | selectorError = true; |
| 223 | resolutionAndFormattingErrors->adoptElement(errorP, status); |
| 224 | break; |
| 225 | } |
| 226 | case DynamicErrorType::UnknownFunction: { |
| 227 | unknownFunctionError = true; |
| 228 | resolutionAndFormattingErrors->adoptElement(errorP, status); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void StaticErrors::checkErrors(UErrorCode& status) const { |
| 235 | if (U_FAILURE(status)) { |
| 236 | return; |
| 237 | } |
| 238 | if (syntaxAndDataModelErrors->size() > 0) { |
| 239 | switch (first().type) { |
| 240 | case StaticErrorType::DuplicateDeclarationError: { |
| 241 | status = U_MF_DUPLICATE_DECLARATION_ERROR; |
| 242 | break; |
| 243 | } |
| 244 | case StaticErrorType::DuplicateOptionName: { |
| 245 | status = U_MF_DUPLICATE_OPTION_NAME_ERROR; |
| 246 | break; |
| 247 | } |
| 248 | case StaticErrorType::VariantKeyMismatchError: { |
| 249 | status = U_MF_VARIANT_KEY_MISMATCH_ERROR; |
| 250 | break; |
| 251 | } |
| 252 | case StaticErrorType::DuplicateVariant: { |
| 253 | status = U_MF_DUPLICATE_VARIANT_ERROR; |
| 254 | break; |
| 255 | } |
| 256 | case StaticErrorType::NonexhaustivePattern: { |
| 257 | status = U_MF_NONEXHAUSTIVE_PATTERN_ERROR; |
| 258 | break; |
| 259 | } |
| 260 | case StaticErrorType::MissingSelectorAnnotation: { |
| 261 | status = U_MF_MISSING_SELECTOR_ANNOTATION_ERROR; |
| 262 | break; |
| 263 | } |
| 264 | case StaticErrorType::SyntaxError: { |
| 265 | status = U_MF_SYNTAX_ERROR; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | const StaticError& StaticErrors::first() const { |
| 273 | U_ASSERT(syntaxAndDataModelErrors.isValid() && syntaxAndDataModelErrors->size() > 0); |
| 274 | return *static_cast<StaticError*>(syntaxAndDataModelErrors->elementAt(0)); |
| 275 | } |
| 276 | |
| 277 | StaticErrors::~StaticErrors() {} |
| 278 | DynamicErrors::~DynamicErrors() {} |
| 279 | |
| 280 | template<typename ErrorType> |
| 281 | Error<ErrorType>::~Error() {} |
| 282 | |
| 283 | template<> |
| 284 | Error<StaticErrorType>::~Error() {} |
| 285 | template<> |
| 286 | Error<DynamicErrorType>::~Error() {} |
| 287 | |
| 288 | } // namespace message2 |
| 289 | |
| 290 | U_NAMESPACE_END |
| 291 | |
| 292 | #endif /* #if !UCONFIG_NO_MF2 */ |
| 293 | |
| 294 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 295 | |
| 296 | #endif /* #if !UCONFIG_NO_NORMALIZATION */ |