LibJS+LibUnicode: Do not generate the PluralCategory enum

The PluralCategory enum is currently generated for plural rules. Instead
of generating it, this moves the enum to the public LibUnicode header.
While it was nice to auto-discover these values, they are well defined
by TR-35, and we will need their values from within the number format
code generator (which can't rely on the plural rules generator having
run yet). Further, number format will require additional values in the
enum that plural rules doesn't know about.
This commit is contained in:
Timothy Flynn 2022-07-08 07:52:54 -04:00 committed by Linus Groh
parent 9369610bf4
commit cc5c707649
Notes: sideshowbarker 2024-07-17 09:36:17 +09:00
5 changed files with 50 additions and 48 deletions

View file

@ -207,7 +207,6 @@ struct UnicodeLocaleData {
UniqueStringStorage<StringIndexType> unique_strings;
HashMap<String, Locale> locales;
Vector<String> categories;
};
static Relation parse_relation(StringView relation)
@ -342,9 +341,6 @@ static ErrorOr<void> parse_plural_rules(String core_supplemental_path, StringVie
auto category = key.substring_view(rule_prefix.length());
parse_condition(category, condition.as_string(), locale->rules_for_form(form));
if (!locale_data.categories.contains_slow(category))
locale_data.categories.append(category);
});
});
});
@ -385,7 +381,7 @@ static ErrorOr<void> parse_all_locales(String core_path, String locale_names_pat
return {};
}
static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile& file, UnicodeLocaleData& locale_data)
static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile& file, UnicodeLocaleData&)
{
StringBuilder builder;
SourceGenerator generator { builder };
@ -398,8 +394,6 @@ static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile&
namespace Unicode {
)~~~");
generate_enum(generator, format_identifier, "PluralCategory"sv, {}, locale_data.categories);
generator.append(R"~~~(
}
)~~~");
@ -418,8 +412,6 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
generator.append(R"~~~(
#include <AK/Array.h>
#include <AK/BinarySearch.h>
#include <AK/StringView.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/PluralRules.h>
#include <LibUnicode/UnicodeLocale.h>
@ -437,17 +429,6 @@ static PluralCategory default_category(PluralOperands)
)~~~");
auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values) {
HashValueMap<String> hashes;
hashes.ensure_capacity(values.size());
for (auto const& value : values)
hashes.set(value.hash(), format_identifier(enum_title, value));
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
generate_value_to_string(generator, "{}_to_string"sv, enum_title, enum_snake, format_identifier, values);
};
auto append_rules = [&](auto form, auto const& locale, auto const& rules) {
if (rules.is_empty())
return;
@ -518,8 +499,6 @@ static constexpr Array<PluralCategory, @size@> @name@ { { PluralCategory::Other)
generator.append("} };");
};
append_string_conversions("PluralCategory"sv, "plural_category"sv, locale_data.categories);
for (auto [locale, rules] : locale_data.locales) {
append_rules("cardinal"sv, locale, rules.cardinal_rules);
append_rules("ordinal"sv, locale, rules.ordinal_rules);

View file

@ -203,8 +203,8 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/PluralRules.h>
#include <LibUnicode/RelativeTimeFormat.h>
#include <LibUnicode/UnicodePluralRules.h>
#include <LibUnicode/UnicodeRelativeTimeFormat.h>
namespace Unicode {

View file

@ -103,7 +103,7 @@ Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules
// 4. If n is not a finite Number, then
if (!number.is_finite_number()) {
// a. Return "other".
return Unicode::plural_category_from_string("other"sv).value();
return Unicode::PluralCategory::Other;
}
// 5. Let locale be pluralRules.[[Locale]].

View file

@ -6,18 +6,8 @@
#include <LibUnicode/PluralRules.h>
#if ENABLE_UNICODE_DATA
# include <LibUnicode/UnicodePluralRules.h>
#endif
namespace Unicode {
#if !ENABLE_UNICODE_DATA
enum class PluralCategory : u8 {
Other,
};
#endif
PluralForm plural_form_from_string(StringView plural_form)
{
if (plural_form == "cardinal"sv)
@ -39,18 +29,6 @@ StringView plural_form_to_string(PluralForm plural_form)
}
}
Optional<PluralCategory> __attribute__((weak)) plural_category_from_string(StringView category)
{
VERIFY(category == "other"sv);
return PluralCategory::Other;
}
StringView __attribute__((weak)) plural_category_to_string(PluralCategory category)
{
VERIFY(category == PluralCategory::Other);
return "other"sv;
}
PluralCategory __attribute__((weak)) determine_plural_category(StringView, PluralForm, PluralOperands)
{
return PluralCategory::Other;

View file

@ -18,6 +18,15 @@ enum class PluralForm {
Ordinal,
};
enum class PluralCategory : u8 {
Other,
Zero,
One,
Two,
Few,
Many,
};
// https://unicode.org/reports/tr35/tr35-numbers.html#Plural_Operand_Meanings
struct PluralOperands {
static constexpr StringView symbol_to_variable_name(char symbol)
@ -57,8 +66,44 @@ struct PluralOperands {
PluralForm plural_form_from_string(StringView plural_form);
StringView plural_form_to_string(PluralForm plural_form);
Optional<PluralCategory> plural_category_from_string(StringView category);
StringView plural_category_to_string(PluralCategory category);
// NOTE: This must be defined inline to be callable from the code generators.
constexpr PluralCategory plural_category_from_string(StringView category)
{
if (category == "other"sv)
return PluralCategory::Other;
if (category == "zero"sv)
return PluralCategory::Zero;
if (category == "one"sv)
return PluralCategory::One;
if (category == "two"sv)
return PluralCategory::Two;
if (category == "few"sv)
return PluralCategory::Few;
if (category == "many"sv)
return PluralCategory::Many;
VERIFY_NOT_REACHED();
}
// NOTE: This must be defined inline to be callable from the code generators.
constexpr StringView plural_category_to_string(PluralCategory category)
{
switch (category) {
case PluralCategory::Other:
return "other"sv;
case PluralCategory::Zero:
return "zero"sv;
case PluralCategory::One:
return "one"sv;
case PluralCategory::Two:
return "two"sv;
case PluralCategory::Few:
return "few"sv;
case PluralCategory::Many:
return "many"sv;
}
VERIFY_NOT_REACHED();
}
PluralCategory determine_plural_category(StringView locale, PluralForm form, PluralOperands operands);
Span<PluralCategory const> available_plural_categories(StringView locale, PluralForm form);