// Fail on syntax errors if (errors->hasSyntaxError()) {
errors->checkErrors(errorCode); // Check that the checkErrors() method set the error code
U_ASSERT(U_FAILURE(errorCode));
}
// Build the data model based on what was parsed
dataModel = tree.build(errorCode);
hasDataModel = true;
hasPattern = true;
pattern = pat;
return *this;
}
// Precondition: `reg` is non-null // Does not adopt `reg`
MessageFormatter::Builder& MessageFormatter::Builder::setFunctionRegistry(const MFFunctionRegistry& reg) {
customMFFunctionRegistry = ® return *this;
}
/* This build() method is non-destructive, which entails the risk that its borrowed MFFunctionRegistry and (if the setDataModel() method was called) MFDataModel pointers could become invalidated.
*/
MessageFormatter MessageFormatter::Builder::build(UErrorCode& errorCode) const { return MessageFormatter(*this, errorCode);
}
// Build data model // First, check that there is a data model // (which might have been set by setDataModel(), or to // the data model parsed from the pattern by setPattern())
if (!builder.hasDataModel) {
success = U_INVALID_STATE_ERROR; return;
}
// Note: we currently evaluate variables lazily, // without memoization. This call is still necessary // to check out-of-scope uses of local variables in // right-hand sides (unresolved variable errors can // only be checked when arguments are known)
// Check for resolution errors
Checker(dataModel, *errors).check(success);
}
UnicodeString MessageFormatter::getPattern() const { // Converts the current data model back to a string
UnicodeString result;
Serializer serializer(getDataModel(), result);
serializer.serialize(); return result;
}
const SelectorFactory* selectorFactory = lookupSelectorFactory(context, functionName, status);
NULL_ON_ERROR(status); if (selectorFactory == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR; return nullptr;
} // Create a specific instance of the selector auto result = selectorFactory->createSelector(getLocale(), status);
NULL_ON_ERROR(status); return result;
}
// First, look up the formatter factory for this function
FormatterFactory* formatterFactory = lookupFormatterFactory(functionName, status);
NULL_ON_ERROR(status);
U_ASSERT(formatterFactory != nullptr);
// Create a specific instance of the formatter
Formatter* formatter = formatterFactory->createFormatter(locale, status);
NULL_ON_ERROR(status); if (formatter == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR; return nullptr;
} return formatter;
}
// https://github.com/unicode-org/message-format-wg/issues/409 // Unknown function = unknown function error // Formatter used as selector = selector error // Selector used as formatter = formatting error const SelectorFactory* MessageFormatter::lookupSelectorFactory(MessageContext& context, const FunctionName& functionName, UErrorCode& status) const {
DynamicErrors& err = context.getErrors();
if (isBuiltInSelector(functionName)) { return standardMFFunctionRegistry.getSelector(functionName);
} if (isBuiltInFormatter(functionName)) {
err.setSelectorError(functionName, status); return nullptr;
} if (hasCustomMFFunctionRegistry()) { const MFFunctionRegistry& customMFFunctionRegistry = getCustomMFFunctionRegistry(); const SelectorFactory* selectorFactory = customMFFunctionRegistry.getSelector(functionName); if (selectorFactory != nullptr) { return selectorFactory;
} if (customMFFunctionRegistry.getFormatter(functionName) != nullptr) {
err.setSelectorError(functionName, status); return nullptr;
}
} // Either there is no custom function registry and the function // isn't built-in, or the function doesn't exist in either the built-in // or custom registry. // Unknown function error
err.setUnknownFunction(functionName, status); return nullptr;
}
if (isBuiltInFormatter(functionName)) { return standardMFFunctionRegistry.getFormatter(functionName);
} if (isBuiltInSelector(functionName)) {
status = U_MF_FORMATTING_ERROR; return nullptr;
} if (hasCustomMFFunctionRegistry()) { const MFFunctionRegistry& customMFFunctionRegistry = getCustomMFFunctionRegistry();
FormatterFactory* formatterFactory = customMFFunctionRegistry.getFormatter(functionName); if (formatterFactory != nullptr) { return formatterFactory;
} if (customMFFunctionRegistry.getSelector(functionName) != nullptr) {
status = U_MF_FORMATTING_ERROR; return nullptr;
}
} // Either there is no custom function registry and the function // isn't built-in, or the function doesn't exist in either the built-in // or custom registry. // Unknown function error
status = U_MF_UNKNOWN_FUNCTION_ERROR; return nullptr;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.