mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:48:12 +00:00
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:
parent
9369610bf4
commit
cc5c707649
5 changed files with 50 additions and 48 deletions
|
@ -207,7 +207,6 @@ struct UnicodeLocaleData {
|
||||||
UniqueStringStorage<StringIndexType> unique_strings;
|
UniqueStringStorage<StringIndexType> unique_strings;
|
||||||
|
|
||||||
HashMap<String, Locale> locales;
|
HashMap<String, Locale> locales;
|
||||||
Vector<String> categories;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static Relation parse_relation(StringView relation)
|
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());
|
auto category = key.substring_view(rule_prefix.length());
|
||||||
parse_condition(category, condition.as_string(), locale->rules_for_form(form));
|
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 {};
|
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;
|
StringBuilder builder;
|
||||||
SourceGenerator generator { builder };
|
SourceGenerator generator { builder };
|
||||||
|
@ -398,8 +394,6 @@ static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile&
|
||||||
namespace Unicode {
|
namespace Unicode {
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
||||||
generate_enum(generator, format_identifier, "PluralCategory"sv, {}, locale_data.categories);
|
|
||||||
|
|
||||||
generator.append(R"~~~(
|
generator.append(R"~~~(
|
||||||
}
|
}
|
||||||
)~~~");
|
)~~~");
|
||||||
|
@ -418,8 +412,6 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
|
||||||
|
|
||||||
generator.append(R"~~~(
|
generator.append(R"~~~(
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
#include <AK/BinarySearch.h>
|
|
||||||
#include <AK/StringView.h>
|
|
||||||
#include <LibUnicode/Locale.h>
|
#include <LibUnicode/Locale.h>
|
||||||
#include <LibUnicode/PluralRules.h>
|
#include <LibUnicode/PluralRules.h>
|
||||||
#include <LibUnicode/UnicodeLocale.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) {
|
auto append_rules = [&](auto form, auto const& locale, auto const& rules) {
|
||||||
if (rules.is_empty())
|
if (rules.is_empty())
|
||||||
return;
|
return;
|
||||||
|
@ -518,8 +499,6 @@ static constexpr Array<PluralCategory, @size@> @name@ { { PluralCategory::Other)
|
||||||
generator.append("} };");
|
generator.append("} };");
|
||||||
};
|
};
|
||||||
|
|
||||||
append_string_conversions("PluralCategory"sv, "plural_category"sv, locale_data.categories);
|
|
||||||
|
|
||||||
for (auto [locale, rules] : locale_data.locales) {
|
for (auto [locale, rules] : locale_data.locales) {
|
||||||
append_rules("cardinal"sv, locale, rules.cardinal_rules);
|
append_rules("cardinal"sv, locale, rules.cardinal_rules);
|
||||||
append_rules("ordinal"sv, locale, rules.ordinal_rules);
|
append_rules("ordinal"sv, locale, rules.ordinal_rules);
|
||||||
|
|
|
@ -203,8 +203,8 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibUnicode/Locale.h>
|
#include <LibUnicode/Locale.h>
|
||||||
|
#include <LibUnicode/PluralRules.h>
|
||||||
#include <LibUnicode/RelativeTimeFormat.h>
|
#include <LibUnicode/RelativeTimeFormat.h>
|
||||||
#include <LibUnicode/UnicodePluralRules.h>
|
|
||||||
#include <LibUnicode/UnicodeRelativeTimeFormat.h>
|
#include <LibUnicode/UnicodeRelativeTimeFormat.h>
|
||||||
|
|
||||||
namespace Unicode {
|
namespace Unicode {
|
||||||
|
|
|
@ -103,7 +103,7 @@ Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules
|
||||||
// 4. If n is not a finite Number, then
|
// 4. If n is not a finite Number, then
|
||||||
if (!number.is_finite_number()) {
|
if (!number.is_finite_number()) {
|
||||||
// a. Return "other".
|
// a. Return "other".
|
||||||
return Unicode::plural_category_from_string("other"sv).value();
|
return Unicode::PluralCategory::Other;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Let locale be pluralRules.[[Locale]].
|
// 5. Let locale be pluralRules.[[Locale]].
|
||||||
|
|
|
@ -6,18 +6,8 @@
|
||||||
|
|
||||||
#include <LibUnicode/PluralRules.h>
|
#include <LibUnicode/PluralRules.h>
|
||||||
|
|
||||||
#if ENABLE_UNICODE_DATA
|
|
||||||
# include <LibUnicode/UnicodePluralRules.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Unicode {
|
namespace Unicode {
|
||||||
|
|
||||||
#if !ENABLE_UNICODE_DATA
|
|
||||||
enum class PluralCategory : u8 {
|
|
||||||
Other,
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PluralForm plural_form_from_string(StringView plural_form)
|
PluralForm plural_form_from_string(StringView plural_form)
|
||||||
{
|
{
|
||||||
if (plural_form == "cardinal"sv)
|
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)
|
PluralCategory __attribute__((weak)) determine_plural_category(StringView, PluralForm, PluralOperands)
|
||||||
{
|
{
|
||||||
return PluralCategory::Other;
|
return PluralCategory::Other;
|
||||||
|
|
|
@ -18,6 +18,15 @@ enum class PluralForm {
|
||||||
Ordinal,
|
Ordinal,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class PluralCategory : u8 {
|
||||||
|
Other,
|
||||||
|
Zero,
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Few,
|
||||||
|
Many,
|
||||||
|
};
|
||||||
|
|
||||||
// https://unicode.org/reports/tr35/tr35-numbers.html#Plural_Operand_Meanings
|
// https://unicode.org/reports/tr35/tr35-numbers.html#Plural_Operand_Meanings
|
||||||
struct PluralOperands {
|
struct PluralOperands {
|
||||||
static constexpr StringView symbol_to_variable_name(char symbol)
|
static constexpr StringView symbol_to_variable_name(char symbol)
|
||||||
|
@ -57,8 +66,44 @@ struct PluralOperands {
|
||||||
PluralForm plural_form_from_string(StringView plural_form);
|
PluralForm plural_form_from_string(StringView plural_form);
|
||||||
StringView plural_form_to_string(PluralForm plural_form);
|
StringView plural_form_to_string(PluralForm plural_form);
|
||||||
|
|
||||||
Optional<PluralCategory> plural_category_from_string(StringView category);
|
// NOTE: This must be defined inline to be callable from the code generators.
|
||||||
StringView plural_category_to_string(PluralCategory category);
|
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);
|
PluralCategory determine_plural_category(StringView locale, PluralForm form, PluralOperands operands);
|
||||||
Span<PluralCategory const> available_plural_categories(StringView locale, PluralForm form);
|
Span<PluralCategory const> available_plural_categories(StringView locale, PluralForm form);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue