tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

messageformat2_errors.cpp (11108B)


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