| 1 |
. |
| 2 |
|
| 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 |
|
| 15 |
|
| 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 |
|
| 29 |
|
| 30 |
U_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
namespace message2 { |
| 33 |
|
| 34 |
using namespace data_model; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
SelectorKeys keys; |
| 48 |
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 |
}; |
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
class Environment; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 90 |
const Expression& expr; |
| 91 |
|
| 92 |
|
| 93 |
const Environment& env; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 137 |
|
| 138 |
VariableName var; |
| 139 |
Closure rhs; |
| 140 |
const LocalPointer<Environment> parent; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 157 |
void checkErrors(UErrorCode& status) const; |
| 158 |
DynamicErrors& getErrors() { return errors; } |
| 159 |
|
| 160 |
virtual ~MessageContext(); |
| 161 |
|
| 162 |
private: |
| 163 |
|
| 164 |
const MessageArguments& arguments; |
| 165 |
|
| 166 |
DynamicErrors errors; |
| 167 |
|
| 168 |
}; |
| 169 |
|
| 170 |
|
| 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 |
|
| 187 |
virtual ~InternalValue(); |
| 188 |
InternalValue(FormattedPlaceholder&&); |
| 189 |
|
| 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 |
|
| 196 |
FormattedPlaceholder takeArgument(UErrorCode& errorCode); |
| 197 |
InternalValue(InternalValue&& other) { *this = std::move(other); } |
| 198 |
InternalValue& operator=(InternalValue&& other) noexcept; |
| 199 |
private: |
| 200 |
|
| 201 |
std::variant<InternalValue*, FormattedPlaceholder> argument; |
| 202 |
FunctionOptions options; |
| 203 |
FunctionName name; |
| 204 |
const Selector* selector; |
| 205 |
const Formatter* formatter; |
| 206 |
}; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
U_NAMESPACE_END |
| 211 |
|
| 212 |
#endif |
| 213 |
|
| 214 |
#endif |
| 215 |
|
| 216 |
#endif |
| 217 |
|
| 218 |
#endif |
| 219 |
|
| 220 |
#endif |
| 221 |
|
| 222 |
#endif |
| 223 |
|