| 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 MESSAGEFORMAT2_H |
| 7 | #define MESSAGEFORMAT2_H |
| 8 | |
| 9 | #if U_SHOW_CPLUSPLUS_API |
| 10 | |
| 11 | #if !UCONFIG_NO_NORMALIZATION |
| 12 | |
| 13 | #if !UCONFIG_NO_FORMATTING |
| 14 | |
| 15 | #if !UCONFIG_NO_MF2 |
| 16 | |
| 17 | /** |
| 18 | * \file |
| 19 | * \brief C++ API: Formats messages using the draft MessageFormat 2.0. |
| 20 | */ |
| 21 | |
| 22 | #include "unicode/messageformat2_arguments.h" |
| 23 | #include "unicode/messageformat2_data_model.h" |
| 24 | #include "unicode/messageformat2_function_registry.h" |
| 25 | #include "unicode/normalizer2.h" |
| 26 | #include "unicode/unistr.h" |
| 27 | |
| 28 | #ifndef U_HIDE_DEPRECATED_API |
| 29 | |
| 30 | U_NAMESPACE_BEGIN |
| 31 | |
| 32 | namespace message2 { |
| 33 | |
| 34 | class Environment; |
| 35 | class MessageContext; |
| 36 | class StaticErrors; |
| 37 | class InternalValue; |
| 38 | |
| 39 | /** |
| 40 | * <p>MessageFormatter is a Technical Preview API implementing MessageFormat 2.0. |
| 41 | * |
| 42 | * <p>See <a target="github" href="https://github.com/unicode-org/message-format-wg/blob/main/spec/syntax.md">the |
| 43 | * description of the syntax with examples and use cases</a> and the corresponding |
| 44 | * <a target="github" href="https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf">ABNF</a> grammar.</p> |
| 45 | * |
| 46 | * The MessageFormatter class is mutable and movable. It is not copyable. |
| 47 | * (It is mutable because if it has a custom function registry, the registry may include |
| 48 | * `FormatterFactory` objects implementing custom formatters, which are allowed to contain |
| 49 | * mutable state.) |
| 50 | * |
| 51 | * @internal ICU 75 technology preview |
| 52 | * @deprecated This API is for technology preview only. |
| 53 | */ |
| 54 | class U_I18N_API MessageFormatter : public UObject { |
| 55 | // Note: This class does not currently inherit from the existing |
| 56 | // `Format` class. |
| 57 | public: |
| 58 | /** |
| 59 | * Move assignment operator: |
| 60 | * The source MessageFormatter will be left in a valid but undefined state. |
| 61 | * |
| 62 | * @internal ICU 75 technology preview |
| 63 | * @deprecated This API is for technology preview only. |
| 64 | */ |
| 65 | MessageFormatter& operator=(MessageFormatter&&) noexcept; |
| 66 | /** |
| 67 | * Destructor. |
| 68 | * |
| 69 | * @internal ICU 75 technology preview |
| 70 | * @deprecated This API is for technology preview only. |
| 71 | */ |
| 72 | virtual ~MessageFormatter(); |
| 73 | |
| 74 | /** |
| 75 | * Formats the message to a string, using the data model that was previously set or parsed, |
| 76 | * and the given `arguments` object. |
| 77 | * |
| 78 | * @param arguments Reference to message arguments |
| 79 | * @param status Input/output error code used to indicate syntax errors, data model |
| 80 | * errors, resolution errors, formatting errors, selection errors, as well |
| 81 | * as other errors (such as memory allocation failures). Partial output |
| 82 | * is still provided in the presence of most error types. |
| 83 | * @return The string result of formatting the message with the given arguments. |
| 84 | * |
| 85 | * @internal ICU 75 technology preview |
| 86 | * @deprecated This API is for technology preview only. |
| 87 | */ |
| 88 | UnicodeString formatToString(const MessageArguments& arguments, UErrorCode &status); |
| 89 | |
| 90 | /** |
| 91 | * Not yet implemented; formats the message to a `FormattedMessage` object, |
| 92 | * using the data model that was previously set or parsed, |
| 93 | * and the given `arguments` object. |
| 94 | * |
| 95 | * @param arguments Reference to message arguments |
| 96 | * @param status Input/output error code used to indicate syntax errors, data model |
| 97 | * errors, resolution errors, formatting errors, selection errors, as well |
| 98 | * as other errors (such as memory allocation failures). Partial output |
| 99 | * is still provided in the presence of most error types. |
| 100 | * @return The `FormattedMessage` representing the formatted message. |
| 101 | * |
| 102 | * @internal ICU 75 technology preview |
| 103 | * @deprecated This API is for technology preview only. |
| 104 | */ |
| 105 | FormattedMessage format(const MessageArguments& arguments, UErrorCode &status) const { |
| 106 | (void) arguments; |
| 107 | if (U_SUCCESS(status)) { |
| 108 | status = U_UNSUPPORTED_ERROR; |
| 109 | } |
| 110 | return FormattedMessage(status); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Accesses the locale that this `MessageFormatter` object was created with. |
| 115 | * |
| 116 | * @return A reference to the locale. |
| 117 | * |
| 118 | * @internal ICU 75 technology preview |
| 119 | * @deprecated This API is for technology preview only. |
| 120 | */ |
| 121 | const Locale& getLocale() const { return locale; } |
| 122 | |
| 123 | /** |
| 124 | * Serializes the data model as a string in MessageFormat 2.0 syntax. |
| 125 | * |
| 126 | * @return result A string representation of the data model. |
| 127 | * The string is a valid MessageFormat 2.0 message. |
| 128 | * |
| 129 | * @internal ICU 75 technology preview |
| 130 | * @deprecated This API is for technology preview only. |
| 131 | */ |
| 132 | UnicodeString getPattern() const; |
| 133 | |
| 134 | /** |
| 135 | * Accesses the data model referred to by this |
| 136 | * `MessageFormatter` object. |
| 137 | * |
| 138 | * @return A reference to the data model. |
| 139 | * |
| 140 | * @internal ICU 75 technology preview |
| 141 | * @deprecated This API is for technology preview only. |
| 142 | */ |
| 143 | const MFDataModel& getDataModel() const; |
| 144 | |
| 145 | /** |
| 146 | * Used in conjunction with the |
| 147 | * MessageFormatter::Builder::setErrorHandlingBehavior() method. |
| 148 | * |
| 149 | * @internal ICU 76 technology preview |
| 150 | * @deprecated This API is for technology preview only. |
| 151 | */ |
| 152 | typedef enum UMFErrorHandlingBehavior { |
| 153 | /** |
| 154 | * Suppress errors and return best-effort output. |
| 155 | * |
| 156 | * @internal ICU 76 technology preview |
| 157 | * @deprecated This API is for technology preview only. |
| 158 | */ |
| 159 | U_MF_BEST_EFFORT = 0, |
| 160 | /** |
| 161 | * Signal all MessageFormat errors using the UErrorCode |
| 162 | * argument. |
| 163 | * |
| 164 | * @internal ICU 76 technology preview |
| 165 | * @deprecated This API is for technology preview only. |
| 166 | */ |
| 167 | U_MF_STRICT |
| 168 | } UMFErrorHandlingBehavior; |
| 169 | |
| 170 | /** |
| 171 | * The mutable Builder class allows each part of the MessageFormatter to be initialized |
| 172 | * separately; calling its `build()` method yields an immutable MessageFormatter. |
| 173 | * |
| 174 | * Not copyable or movable. |
| 175 | */ |
| 176 | class U_I18N_API Builder : public UObject { |
| 177 | private: |
| 178 | friend class MessageFormatter; |
| 179 | |
| 180 | // The pattern to be parsed to generate the formatted message |
| 181 | UnicodeString pattern; |
| 182 | bool hasPattern = false; |
| 183 | bool hasDataModel = false; |
| 184 | // The data model to be used to generate the formatted message |
| 185 | // Initialized either by `setDataModel()`, or by the parser |
| 186 | // through a call to `setPattern()` |
| 187 | MFDataModel dataModel; |
| 188 | // Normalized representation of the pattern; |
| 189 | // ignored if `setPattern()` wasn't called |
| 190 | UnicodeString normalizedInput; |
| 191 | // Errors (internal representation of parse errors) |
| 192 | // Ignored if `setPattern()` wasn't called |
| 193 | StaticErrors* errors; |
| 194 | Locale locale; |
| 195 | // Not owned |
| 196 | const MFFunctionRegistry* customMFFunctionRegistry; |
| 197 | // Error behavior; see comment in `MessageFormatter` class |
| 198 | bool signalErrors = false; |
| 199 | |
| 200 | void clearState(); |
| 201 | public: |
| 202 | /** |
| 203 | * Sets the locale to use for formatting. |
| 204 | * |
| 205 | * @param locale The desired locale. |
| 206 | * @return A reference to the builder. |
| 207 | * |
| 208 | * @internal ICU 75 technology preview |
| 209 | * @deprecated This API is for technology preview only. |
| 210 | */ |
| 211 | Builder& setLocale(const Locale& locale); |
| 212 | /** |
| 213 | * Sets the pattern (contents of the message) and parses it |
| 214 | * into a data model. If a data model was |
| 215 | * previously set, it is removed. |
| 216 | * |
| 217 | * @param pattern A string in MessageFormat 2.0 syntax. |
| 218 | * @param parseError Struct to receive information on the position |
| 219 | * of an error within the pattern. |
| 220 | * @param status Input/output error code. If the |
| 221 | * pattern cannot be parsed, set to failure code. |
| 222 | * @return A reference to the builder. |
| 223 | * |
| 224 | * @internal ICU 75 technology preview |
| 225 | * @deprecated This API is for technology preview only. |
| 226 | */ |
| 227 | Builder& setPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status); |
| 228 | /** |
| 229 | * Sets a custom function registry. |
| 230 | * |
| 231 | * @param functionRegistry Reference to the function registry to use. |
| 232 | * `functionRegistry` is not copied, |
| 233 | * and the caller must ensure its lifetime contains |
| 234 | * the lifetime of the `MessageFormatter` object built by this |
| 235 | * builder. |
| 236 | * @return A reference to the builder. |
| 237 | * |
| 238 | * @internal ICU 75 technology preview |
| 239 | * @deprecated This API is for technology preview only. |
| 240 | */ |
| 241 | Builder& setFunctionRegistry(const MFFunctionRegistry& functionRegistry); |
| 242 | /** |
| 243 | * Sets a data model. If a pattern was previously set, it is removed. |
| 244 | * |
| 245 | * @param dataModel Data model to format. Passed by move. |
| 246 | * @return A reference to the builder. |
| 247 | * |
| 248 | * @internal ICU 75 technology preview |
| 249 | * @deprecated This API is for technology preview only. |
| 250 | */ |
| 251 | Builder& setDataModel(MFDataModel&& dataModel); |
| 252 | /** |
| 253 | * Set the error handling behavior for this formatter. |
| 254 | * |
| 255 | * "Strict" error behavior means that that formatting methods |
| 256 | * will set their UErrorCode arguments to signal MessageFormat |
| 257 | * data model, resolution, and runtime errors. Syntax errors are |
| 258 | * always signaled. |
| 259 | * |
| 260 | * "Best effort" error behavior means that MessageFormat errors are |
| 261 | * suppressed: formatting methods will _not_ set their |
| 262 | * UErrorCode arguments to signal MessageFormat data model, |
| 263 | * resolution, or runtime errors. Best-effort output |
| 264 | * will be returned. Syntax errors are always signaled. |
| 265 | * This is the default behavior. |
| 266 | * |
| 267 | * @param type An enum with type UMFErrorHandlingBehavior; |
| 268 | * if type == `U_MF_STRICT`, then |
| 269 | * errors are handled strictly. |
| 270 | * If type == `U_MF_BEST_EFFORT`, then |
| 271 | * best-effort output is returned. |
| 272 | * |
| 273 | * The default is to suppress all MessageFormat errors |
| 274 | * and return best-effort output. |
| 275 | * |
| 276 | * @return A reference to the builder. |
| 277 | * |
| 278 | * @internal ICU 76 technology preview |
| 279 | * @deprecated This API is for technology preview only. |
| 280 | */ |
| 281 | Builder& setErrorHandlingBehavior(UMFErrorHandlingBehavior type); |
| 282 | /** |
| 283 | * Constructs a new immutable MessageFormatter using the pattern or data model |
| 284 | * that was previously set, and the locale (if it was previously set) |
| 285 | * or default locale (otherwise). |
| 286 | * |
| 287 | * The builder object (`this`) can still be used after calling `build()`. |
| 288 | * |
| 289 | * @param status Input/output error code. If neither the pattern |
| 290 | * nor the data model is set, set to failure code. |
| 291 | * @return The new MessageFormatter object |
| 292 | * |
| 293 | * @internal ICU 75 technology preview |
| 294 | * @deprecated This API is for technology preview only. |
| 295 | */ |
| 296 | MessageFormatter build(UErrorCode& status) const; |
| 297 | /** |
| 298 | * Default constructor. |
| 299 | * Returns a Builder with the default locale and with no |
| 300 | * data model or pattern set. Either `setPattern()` |
| 301 | * or `setDataModel()` has to be called before calling `build()`. |
| 302 | * |
| 303 | * @param status Input/output error code. |
| 304 | * |
| 305 | * @internal ICU 75 technology preview |
| 306 | * @deprecated This API is for technology preview only. |
| 307 | */ |
| 308 | Builder(UErrorCode& status); |
| 309 | /** |
| 310 | * Destructor. |
| 311 | * |
| 312 | * @internal ICU 75 technology preview |
| 313 | * @deprecated This API is for technology preview only. |
| 314 | */ |
| 315 | virtual ~Builder(); |
| 316 | }; // class MessageFormatter::Builder |
| 317 | |
| 318 | // TODO: Shouldn't be public; only used for testing |
| 319 | /** |
| 320 | * Returns a string consisting of the input with optional spaces removed. |
| 321 | * |
| 322 | * @return A normalized string representation of the input |
| 323 | * |
| 324 | * @internal ICU 75 technology preview |
| 325 | * @deprecated This API is for technology preview only. |
| 326 | */ |
| 327 | const UnicodeString& getNormalizedPattern() const { return normalizedInput; } |
| 328 | |
| 329 | private: |
| 330 | friend class Builder; |
| 331 | friend class Checker; |
| 332 | friend class MessageArguments; |
| 333 | friend class MessageContext; |
| 334 | |
| 335 | MessageFormatter(const MessageFormatter::Builder& builder, UErrorCode &status); |
| 336 | |
| 337 | MessageFormatter() = delete; // default constructor not implemented |
| 338 | |
| 339 | // Do not define default assignment operator |
| 340 | const MessageFormatter &operator=(const MessageFormatter &) = delete; |
| 341 | |
| 342 | // Selection methods |
| 343 | |
| 344 | // Takes a vector of FormattedPlaceholders |
| 345 | void resolveSelectors(MessageContext&, const Environment& env, UErrorCode&, UVector&) const; |
| 346 | // Takes a vector of vectors of strings (input) and a vector of PrioritizedVariants (output) |
| 347 | void filterVariants(const UVector&, UVector&, UErrorCode&) const; |
| 348 | // Takes a vector of vectors of strings (input) and a vector of PrioritizedVariants (input/output) |
| 349 | void sortVariants(const UVector&, UVector&, UErrorCode&) const; |
| 350 | // Takes a vector of strings (input) and a vector of strings (output) |
| 351 | void matchSelectorKeys(const UVector&, MessageContext&, InternalValue* rv, UVector&, UErrorCode&) const; |
| 352 | // Takes a vector of FormattedPlaceholders (input), |
| 353 | // and a vector of vectors of strings (output) |
| 354 | void resolvePreferences(MessageContext&, UVector&, UVector&, UErrorCode&) const; |
| 355 | |
| 356 | // Formatting methods |
| 357 | |
| 358 | // Used for normalizing variable names and keys for comparison |
| 359 | UnicodeString normalizeNFC(const UnicodeString&) const; |
| 360 | [[nodiscard]] FormattedPlaceholder formatLiteral(const data_model::Literal&) const; |
| 361 | void formatPattern(MessageContext&, const Environment&, const data_model::Pattern&, UErrorCode&, UnicodeString&) const; |
| 362 | // Evaluates a function call |
| 363 | // Dispatches on argument type |
| 364 | [[nodiscard]] InternalValue* evalFunctionCall(FormattedPlaceholder&& argument, |
| 365 | MessageContext& context, |
| 366 | UErrorCode& status) const; |
| 367 | // Dispatches on function name |
| 368 | [[nodiscard]] InternalValue* evalFunctionCall(const FunctionName& functionName, |
| 369 | InternalValue* argument, |
| 370 | FunctionOptions&& options, |
| 371 | MessageContext& context, |
| 372 | UErrorCode& status) const; |
| 373 | // Formats an expression that appears in a pattern or as the definition of a local variable |
| 374 | [[nodiscard]] InternalValue* formatExpression(const Environment&, |
| 375 | const data_model::Expression&, |
| 376 | MessageContext&, |
| 377 | UErrorCode&) const; |
| 378 | [[nodiscard]] FunctionOptions resolveOptions(const Environment& env, const OptionMap&, MessageContext&, UErrorCode&) const; |
| 379 | [[nodiscard]] InternalValue* formatOperand(const Environment&, const data_model::Operand&, MessageContext&, UErrorCode&) const; |
| 380 | [[nodiscard]] FormattedPlaceholder evalArgument(const data_model::VariableName&, MessageContext&, UErrorCode&) const; |
| 381 | void formatSelectors(MessageContext& context, const Environment& env, UErrorCode &status, UnicodeString& result) const; |
| 382 | |
| 383 | // Function registry methods |
| 384 | bool hasCustomMFFunctionRegistry() const { |
| 385 | return (customMFFunctionRegistry != nullptr); |
| 386 | } |
| 387 | |
| 388 | // Precondition: custom function registry exists |
| 389 | // Note: this is non-const because the values in the MFFunctionRegistry are mutable |
| 390 | // (a FormatterFactory can have mutable state) |
| 391 | const MFFunctionRegistry& getCustomMFFunctionRegistry() const; |
| 392 | |
| 393 | bool isCustomFormatter(const FunctionName&) const; |
| 394 | FormatterFactory* lookupFormatterFactory(const FunctionName&, UErrorCode& status) const; |
| 395 | bool isBuiltInSelector(const FunctionName&) const; |
| 396 | bool isBuiltInFormatter(const FunctionName&) const; |
| 397 | bool isCustomSelector(const FunctionName&) const; |
| 398 | const SelectorFactory* lookupSelectorFactory(MessageContext&, const FunctionName&, UErrorCode&) const; |
| 399 | bool isSelector(const FunctionName& fn) const { return isBuiltInSelector(fn) || isCustomSelector(fn); } |
| 400 | bool isFormatter(const FunctionName& fn) const { return isBuiltInFormatter(fn) || isCustomFormatter(fn); } |
| 401 | const Formatter* lookupFormatter(const FunctionName&, UErrorCode&) const; |
| 402 | |
| 403 | Selector* getSelector(MessageContext&, const FunctionName&, UErrorCode&) const; |
| 404 | Formatter* getFormatter(const FunctionName&, UErrorCode&) const; |
| 405 | bool getDefaultFormatterNameByType(const UnicodeString&, FunctionName&) const; |
| 406 | |
| 407 | // Checking for resolution errors |
| 408 | void checkDeclarations(MessageContext&, Environment*&, UErrorCode&) const; |
| 409 | void check(MessageContext&, const Environment&, const data_model::Expression&, UErrorCode&) const; |
| 410 | void check(MessageContext&, const Environment&, const data_model::Operand&, UErrorCode&) const; |
| 411 | void check(MessageContext&, const Environment&, const OptionMap&, UErrorCode&) const; |
| 412 | |
| 413 | void initErrors(UErrorCode&); |
| 414 | void clearErrors() const; |
| 415 | void cleanup() noexcept; |
| 416 | |
| 417 | // The locale this MessageFormatter was created with |
| 418 | /* const */ Locale locale; |
| 419 | |
| 420 | // Registry for built-in functions |
| 421 | MFFunctionRegistry standardMFFunctionRegistry; |
| 422 | // Registry for custom functions; may be null if no custom registry supplied |
| 423 | // Note: this is *not* owned by the MessageFormatter object |
| 424 | // The reason for this choice is to have a non-destructive MessageFormatter::Builder, |
| 425 | // while also not requiring the function registry to be deeply-copyable. Making the |
| 426 | // function registry copyable would impose a requirement on any implementations |
| 427 | // of the FormatterFactory and SelectorFactory interfaces to implement a custom |
| 428 | // clone() method, which is necessary to avoid sharing between copies of the |
| 429 | // function registry (and thus double-frees) |
| 430 | // Not deeply immutable (the values in the function registry are mutable, |
| 431 | // as a FormatterFactory can have mutable state |
| 432 | const MFFunctionRegistry* customMFFunctionRegistry; |
| 433 | |
| 434 | // Data model, representing the parsed message |
| 435 | MFDataModel dataModel; |
| 436 | |
| 437 | // Normalized version of the input string (optional whitespace removed) |
| 438 | UnicodeString normalizedInput; |
| 439 | |
| 440 | // Errors -- only used while parsing and checking for data model errors; then |
| 441 | // the MessageContext keeps track of errors |
| 442 | // Must be a raw pointer to avoid including the internal header file |
| 443 | // defining StaticErrors |
| 444 | // Owned by `this` |
| 445 | StaticErrors* errors = nullptr; |
| 446 | |
| 447 | // Error handling behavior. |
| 448 | // If true, then formatting methods set their UErrorCode arguments |
| 449 | // to signal MessageFormat errors, and no useful output is returned. |
| 450 | // If false, then MessageFormat errors are not signaled and the |
| 451 | // formatting methods return best-effort output. |
| 452 | // The default is false. |
| 453 | bool signalErrors = false; |
| 454 | |
| 455 | // Used for implementing normalizeNFC() |
| 456 | const Normalizer2* nfcNormalizer = nullptr; |
| 457 | |
| 458 | }; // class MessageFormatter |
| 459 | |
| 460 | } // namespace message2 |
| 461 | |
| 462 | U_NAMESPACE_END |
| 463 | |
| 464 | #endif // U_HIDE_DEPRECATED_API |
| 465 | |
| 466 | #endif /* #if !UCONFIG_NO_MF2 */ |
| 467 | |
| 468 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 469 | |
| 470 | #endif /* #if !UCONFIG_NO_NORMALIZATION */ |
| 471 | |
| 472 | #endif /* U_SHOW_CPLUSPLUS_API */ |
| 473 | |
| 474 | #endif // MESSAGEFORMAT2_H |
| 475 | |
| 476 | // eof |