1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "unicode/utypes.h"
#ifndef U_HIDE_DEPRECATED_API
#ifndef MESSAGEFORMAT2_EVALUATION_H
#define MESSAGEFORMAT2_EVALUATION_H
#if U_SHOW_CPLUSPLUS_API
/**
* \file
* \brief C++ API: Formats messages using the draft MessageFormat 2.0.
*/
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/messageformat2_arguments.h"
#include "unicode/messageformat2_data_model.h"
#include "unicode/messageformat2_function_registry.h"
#include "messageformat2_errors.h"
// Auxiliary data structures used during formatting a message
U_NAMESPACE_BEGIN
namespace message2 {
using namespace data_model;
// PrioritizedVariant
// For how this class is used, see the references to (integer, variant) tuples
// in https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md#pattern-selection
class PrioritizedVariant : public UObject {
public:
PrioritizedVariant() = default;
PrioritizedVariant(PrioritizedVariant&&) = default;
PrioritizedVariant& operator=(PrioritizedVariant&&) noexcept = default;
UBool operator<(const PrioritizedVariant&) const;
int32_t priority;
/* const */ SelectorKeys keys;
/* const */ Pattern pat;
PrioritizedVariant(uint32_t p,
const SelectorKeys& k,
const Pattern& pattern) noexcept : priority(p), keys(k), pat(pattern) {}
virtual ~PrioritizedVariant();
}; // class PrioritizedVariant
static inline int32_t comparePrioritizedVariants(UElement left, UElement right) {
const PrioritizedVariant& tuple1 = *(static_cast<const PrioritizedVariant*>(left.pointer));
const PrioritizedVariant& tuple2 = *(static_cast<const PrioritizedVariant*>(right.pointer));
if (tuple1 < tuple2) {
return -1;
}
if (tuple1.priority == tuple2.priority) {
return 0;
}
return 1;
}
// Encapsulates a value to be scrutinized by a `match` with its resolved
// options and the name of the selector
class ResolvedSelector : public UObject {
public:
ResolvedSelector() {}
ResolvedSelector(const FunctionName& fn,
Selector* selector,
FunctionOptions&& options,
FormattedPlaceholder&& value);
// Used either for errors, or when selector isn't yet known
explicit ResolvedSelector(FormattedPlaceholder&& value);
bool hasSelector() const { return selector.isValid(); }
const FormattedPlaceholder& argument() const { return value; }
FormattedPlaceholder&& takeArgument() { return std::move(value); }
const Selector* getSelector() {
U_ASSERT(selector.isValid());
return selector.getAlias();
}
FunctionOptions&& takeOptions() {
return std::move(options);
}
const FunctionName& getSelectorName() const { return selectorName; }
virtual ~ResolvedSelector();
ResolvedSelector& operator=(ResolvedSelector&&) noexcept;
ResolvedSelector(ResolvedSelector&&);
private:
FunctionName selectorName; // For error reporting
LocalPointer<Selector> selector;
FunctionOptions options;
FormattedPlaceholder value;
}; // class ResolvedSelector
// Closures and environments
// -------------------------
class Environment;
// A closure represents the right-hand side of a variable
// declaration, along with an environment giving values
// to its free variables
class Closure : public UMemory {
public:
const Expression& getExpr() const {
return expr;
}
const Environment& getEnv() const {
return env;
}
Closure(const Expression& expression, const Environment& environment) : expr(expression), env(environment) {}
Closure(Closure&&) = default;
virtual ~Closure();
private:
// An unevaluated expression
const Expression& expr;
// The environment mapping names used in this
// expression to other expressions
const Environment& env;
};
// An environment is represented as a linked chain of
// non-empty environments, terminating at an empty environment.
// It's searched using linear search.
class Environment : public UMemory {
public:
virtual bool has(const VariableName&) const = 0;
virtual const Closure& lookup(const VariableName&) const = 0;
static Environment* create(UErrorCode&);
static Environment* create(const VariableName&, Closure&&, Environment*, UErrorCode&);
virtual ~Environment();
};
class NonEmptyEnvironment;
class EmptyEnvironment : public Environment {
public:
EmptyEnvironment() = default;
virtual ~EmptyEnvironment();
private:
friend class Environment;
bool has(const VariableName&) const override;
const Closure& lookup(const VariableName&) const override;
static EmptyEnvironment* create(UErrorCode&);
static NonEmptyEnvironment* create(const VariableName&, Closure&&, Environment*, UErrorCode&);
};
class NonEmptyEnvironment : public Environment {
private:
friend class Environment;
bool has(const VariableName&) const override;
const Closure& lookup(const VariableName&) const override;
static NonEmptyEnvironment* create(const VariableName&, Closure&&, const Environment*, UErrorCode&);
virtual ~NonEmptyEnvironment();
private:
friend class Environment;
NonEmptyEnvironment(const VariableName& v, Closure&& c, Environment* e) : var(v), rhs(std::move(c)), parent(e) {}
// Maps VariableName onto Closure*
// Chain of linked environments
VariableName var;
Closure rhs;
const LocalPointer<Environment> parent;
};
// The context contains all the information needed to process
// an entire message: arguments, formatter cache, and error list
class MessageContext : public UMemory {
public:
MessageContext(const MessageArguments&, const StaticErrors&, UErrorCode&);
const Formattable* getGlobal(const VariableName&, UErrorCode&) const;
// If any errors were set, update `status` accordingly
void checkErrors(UErrorCode& status) const;
DynamicErrors& getErrors() { return errors; }
virtual ~MessageContext();
private:
const MessageArguments& arguments; // External message arguments
// Errors accumulated during parsing/formatting
DynamicErrors errors;
}; // class MessageContext
} // namespace message2
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // MESSAGEFORMAT2_EVALUATION_H
#endif // U_HIDE_DEPRECATED_API
// eof
|