Debian Codegraph @

intl/icu/source/i18n/messageformat2_evaluation.h

raw
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_EVALUATION_H
9 #define MESSAGEFORMAT2_EVALUATION_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 #if !UCONFIG_NO_NORMALIZATION
18
19 #if !UCONFIG_NO_FORMATTING
20
21 #if !UCONFIG_NO_MF2
22
23 #include "unicode/messageformat2_arguments.h"
24 #include "unicode/messageformat2_data_model.h"
25 #include "unicode/messageformat2_function_registry.h"
26 #include "messageformat2_errors.h"
27
28 // Auxiliary data structures used during formatting a message
29
30 U_NAMESPACE_BEGIN
31
32 namespace message2 {
33
34 using namespace data_model;
35
36 // PrioritizedVariant
37
38 // For how this class is used, see the references to (integer, variant) tuples
39 // in https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md#pattern-selection
40 class PrioritizedVariant : public UObject {
41 public:
42 PrioritizedVariant() = default;
43 PrioritizedVariant(PrioritizedVariant&&) = default;
44 PrioritizedVariant& operator=(PrioritizedVariant&&) noexcept = default;
45 UBool operator<(const PrioritizedVariant&) const;
46 int32_t priority;
47 /* const */ SelectorKeys keys;
48 /* const */ Pattern pat;
49 PrioritizedVariant(uint32_t p,
50 const SelectorKeys& k,
51 const Pattern& pattern) noexcept : priority(p), keys(k), pat(pattern) {}
52 virtual ~PrioritizedVariant();
53 }; // class PrioritizedVariant
54
55 static inline int32_t comparePrioritizedVariants(UElement left, UElement right) {
56 const PrioritizedVariant& tuple1 = *(static_cast<const PrioritizedVariant*>(left.pointer));
57 const PrioritizedVariant& tuple2 = *(static_cast<const PrioritizedVariant*>(right.pointer));
58 if (tuple1 < tuple2) {
59 return -1;
60 }
61 if (tuple1.priority == tuple2.priority) {
62 return 0;
63 }
64 return 1;
65 }
66
67 // Closures and environments
68 // -------------------------
69
70 class Environment;
71
72 // A closure represents the right-hand side of a variable
73 // declaration, along with an environment giving values
74 // to its free variables
75 class Closure : public UMemory {
76 public:
77 const Expression& getExpr() const {
78 return expr;
79 }
80 const Environment& getEnv() const {
81 return env;
82 }
83 Closure(const Expression& expression, const Environment& environment) : expr(expression), env(environment) {}
84 Closure(Closure&&) = default;
85
86 virtual ~Closure();
87 private:
88
89 // An unevaluated expression
90 const Expression& expr;
91 // The environment mapping names used in this
92 // expression to other expressions
93 const Environment& env;
94 };
95
96 // An environment is represented as a linked chain of
97 // non-empty environments, terminating at an empty environment.
98 // It's searched using linear search.
99 class Environment : public UMemory {
100 public:
101 virtual bool has(const VariableName&) const = 0;
102 virtual const Closure& lookup(const VariableName&) const = 0;
103 static Environment* create(UErrorCode&);
104 static Environment* create(const VariableName&, Closure&&, Environment*, UErrorCode&);
105 virtual ~Environment();
106 };
107
108 class NonEmptyEnvironment;
109 class EmptyEnvironment : public Environment {
110 public:
111 EmptyEnvironment() = default;
112 virtual ~EmptyEnvironment();
113
114 private:
115 friend class Environment;
116
117 bool has(const VariableName&) const override;
118 const Closure& lookup(const VariableName&) const override;
119 static EmptyEnvironment* create(UErrorCode&);
120 static NonEmptyEnvironment* create(const VariableName&, Closure&&, Environment*, UErrorCode&);
121 };
122
123 class NonEmptyEnvironment : public Environment {
124 private:
125 friend class Environment;
126
127 bool has(const VariableName&) const override;
128 const Closure& lookup(const VariableName&) const override;
129 static NonEmptyEnvironment* create(const VariableName&, Closure&&, const Environment*, UErrorCode&);
130 virtual ~NonEmptyEnvironment();
131 private:
132 friend class Environment;
133
134 NonEmptyEnvironment(const VariableName& v, Closure&& c, Environment* e) : var(v), rhs(std::move(c)), parent(e) {}
135
136 // Maps VariableName onto Closure*
137 // Chain of linked environments
138 VariableName var;
139 Closure rhs;
140 const LocalPointer<Environment> parent;
141 };
142
143 // The context contains all the information needed to process
144 // an entire message: arguments, formatter cache, and error list
145
146 class MessageFormatter;
147
148 class MessageContext : public UMemory {
149 public:
150 MessageContext(const MessageArguments&, const StaticErrors&, UErrorCode&);
151
152 const Formattable* getGlobal(const MessageFormatter&,
153 const VariableName&,
154 UErrorCode&) const;
155
156 // If any errors were set, update `status` accordingly
157 void checkErrors(UErrorCode& status) const;
158 DynamicErrors& getErrors() { return errors; }
159
160 virtual ~MessageContext();
161
162 private:
163
164 const MessageArguments& arguments; // External message arguments
165 // Errors accumulated during parsing/formatting
166 DynamicErrors errors;
167
168 }; // class MessageContext
169
170 // InternalValue
171 // ----------------
172
173 class InternalValue : public UObject {
174 public:
175 const FunctionName& getFunctionName() const { return name; }
176 bool canSelect() const { return selector != nullptr; }
177 const Selector* getSelector(UErrorCode&) const;
178 FormattedPlaceholder forceFormatting(DynamicErrors& errs,
179 UErrorCode& errorCode);
180 void forceSelection(DynamicErrors& errs,
181 const UnicodeString* keys,
182 int32_t keysLen,
183 UnicodeString* prefs,
184 int32_t& prefsLen,
185 UErrorCode& errorCode);
186 // Needs to be deep-copyable and movable
187 virtual ~InternalValue();
188 InternalValue(FormattedPlaceholder&&);
189 // Formatter and selector may be null
190 InternalValue(InternalValue*, FunctionOptions&&, const FunctionName&, const Formatter*,
191 const Selector*);
192 const UnicodeString& getFallback() const;
193 bool isFallback() const;
194 bool hasNullOperand() const;
195 // Can't be used anymore after calling this
196 FormattedPlaceholder takeArgument(UErrorCode& errorCode);
197 InternalValue(InternalValue&& other) { *this = std::move(other); }
198 InternalValue& operator=(InternalValue&& other) noexcept;
199 private:
200 // InternalValue is owned (if present)
201 std::variant<InternalValue*, FormattedPlaceholder> argument;
202 FunctionOptions options;
203 FunctionName name;
204 const Selector* selector; // May be null
205 const Formatter* formatter; // May be null, but one or the other should be non-null unless argument is a FormattedPlaceholder
206 }; // class InternalValue
207
208 } // namespace message2
209
210 U_NAMESPACE_END
211
212 #endif /* #if !UCONFIG_NO_MF2 */
213
214 #endif /* #if !UCONFIG_NO_FORMATTING */
215
216 #endif /* #if !UCONFIG_NO_NORMALIZATION */
217
218 #endif /* U_SHOW_CPLUSPLUS_API */
219
220 #endif // MESSAGEFORMAT2_EVALUATION_H
221
222 #endif // U_HIDE_DEPRECATED_API
223 // eof