xref: /aosp_15_r20/external/icu/libicu/cts_headers/messageformat2_errors.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
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         MissingSelectorAnnotation,
58         NonexhaustivePattern,
59         SyntaxError,
60         UnsupportedStatementError,
61         VariantKeyMismatchError
62     };
63 
64     enum DynamicErrorType {
65         UnresolvedVariable,
66         FormattingError,
67         OperandMismatchError,
68         ReservedError,
69         SelectorError,
70         UnknownFunction,
71     };
72 
73     using StaticError = Error<StaticErrorType>;
74     using DynamicError = Error<DynamicErrorType>;
75 
76     // These explicit instantiations have to come before the
77     // destructor definitions
78     template<>
79     Error<StaticErrorType>::~Error();
80     template<>
81     Error<DynamicErrorType>::~Error();
82 
83     class StaticErrors : public UObject {
84     private:
85         friend class DynamicErrors;
86 
87         LocalPointer<UVector> syntaxAndDataModelErrors;
88         bool dataModelError = false;
89         bool missingSelectorAnnotationError = false;
90         bool syntaxError = false;
91 
92     public:
93         StaticErrors(UErrorCode&);
94 
95         void setMissingSelectorAnnotation(UErrorCode&);
96         void setDuplicateOptionName(UErrorCode&);
97         void addSyntaxError(UErrorCode&);
hasDataModelError()98         bool hasDataModelError() const { return dataModelError; }
hasSyntaxError()99         bool hasSyntaxError() const { return syntaxError; }
hasMissingSelectorAnnotationError()100         bool hasMissingSelectorAnnotationError() const { return missingSelectorAnnotationError; }
101         void addError(StaticError&&, UErrorCode&);
102         void checkErrors(UErrorCode&);
103 
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 setReservedError(UErrorCode&);
125         void setUnresolvedVariable(const VariableName&, UErrorCode&);
126         void setUnknownFunction(const FunctionName&, UErrorCode&);
127         void setFormattingError(const FunctionName&, UErrorCode&);
128         // Used when the name of the offending formatter is unknown
129         void setFormattingError(UErrorCode&);
130         void setOperandMismatchError(const FunctionName&, UErrorCode&);
hasDataModelError()131         bool hasDataModelError() const { return staticErrors.hasDataModelError(); }
hasFormattingError()132         bool hasFormattingError() const { return formattingError; }
hasSelectorError()133         bool hasSelectorError() const { return selectorError; }
hasSyntaxError()134         bool hasSyntaxError() const { return staticErrors.hasSyntaxError(); }
hasUnknownFunctionError()135         bool hasUnknownFunctionError() const { return unknownFunctionError; }
hasMissingSelectorAnnotationError()136         bool hasMissingSelectorAnnotationError() const { return staticErrors.hasMissingSelectorAnnotationError(); }
hasUnresolvedVariableError()137         bool hasUnresolvedVariableError() const { return unresolvedVariableError; }
138         void addError(DynamicError&&, UErrorCode&);
139         void checkErrors(UErrorCode&) const;
140         bool hasError() const;
141         bool hasStaticError() const;
142 
143         const DynamicError& first() const;
144         virtual ~DynamicErrors();
145     }; // class DynamicErrors
146 
147 } // namespace message2
148 
149 U_NAMESPACE_END
150 
151 #endif /* #if !UCONFIG_NO_MF2 */
152 
153 #endif /* #if !UCONFIG_NO_FORMATTING */
154 
155 #endif /* U_SHOW_CPLUSPLUS_API */
156 
157 #endif // MESSAGEFORMAT2_ERRORS_H
158 
159 #endif // U_HIDE_DEPRECATED_API
160 // eof
161