GenerateUnicodeNumberFormat.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GeneratorUtil.h"
  7. #include <AK/AllOf.h>
  8. #include <AK/Array.h>
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/Find.h>
  11. #include <AK/Format.h>
  12. #include <AK/HashFunctions.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/JsonObject.h>
  15. #include <AK/JsonParser.h>
  16. #include <AK/JsonValue.h>
  17. #include <AK/LexicalPath.h>
  18. #include <AK/QuickSort.h>
  19. #include <AK/SourceGenerator.h>
  20. #include <AK/String.h>
  21. #include <AK/StringBuilder.h>
  22. #include <AK/Traits.h>
  23. #include <AK/Utf8View.h>
  24. #include <LibCore/ArgsParser.h>
  25. #include <LibCore/DirIterator.h>
  26. #include <LibCore/File.h>
  27. #include <LibCore/Stream.h>
  28. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  29. #include <LibUnicode/Locale.h>
  30. #include <LibUnicode/NumberFormat.h>
  31. #include <math.h>
  32. using StringIndexType = u16;
  33. constexpr auto s_string_index_type = "u16"sv;
  34. using NumberFormatIndexType = u16;
  35. constexpr auto s_number_format_index_type = "u16"sv;
  36. using NumberFormatListIndexType = u16;
  37. constexpr auto s_number_format_list_index_type = "u16"sv;
  38. using NumericSymbolListIndexType = u8;
  39. constexpr auto s_numeric_symbol_list_index_type = "u8"sv;
  40. using NumberSystemIndexType = u8;
  41. constexpr auto s_number_system_index_type = "u8"sv;
  42. using UnitIndexType = u16;
  43. constexpr auto s_unit_index_type = "u16"sv;
  44. enum class NumberFormatType {
  45. Standard,
  46. Compact,
  47. };
  48. struct NumberFormat : public Unicode::NumberFormat {
  49. using Base = Unicode::NumberFormat;
  50. static Base::Plurality plurality_from_string(StringView plurality)
  51. {
  52. if (plurality == "other"sv)
  53. return Base::Plurality::Other;
  54. if (plurality == "1"sv)
  55. return Base::Plurality::Single;
  56. if (plurality == "zero"sv)
  57. return Base::Plurality::Zero;
  58. if (plurality == "one"sv)
  59. return Base::Plurality::One;
  60. if (plurality == "two"sv)
  61. return Base::Plurality::Two;
  62. if (plurality == "few"sv)
  63. return Base::Plurality::Few;
  64. if (plurality == "many"sv)
  65. return Base::Plurality::Many;
  66. VERIFY_NOT_REACHED();
  67. }
  68. unsigned hash() const
  69. {
  70. auto hash = pair_int_hash(magnitude, exponent);
  71. hash = pair_int_hash(hash, static_cast<u8>(plurality));
  72. hash = pair_int_hash(hash, zero_format_index);
  73. hash = pair_int_hash(hash, positive_format_index);
  74. hash = pair_int_hash(hash, negative_format_index);
  75. for (auto index : identifier_indices)
  76. hash = pair_int_hash(hash, index);
  77. return hash;
  78. }
  79. bool operator==(NumberFormat const& other) const
  80. {
  81. return (magnitude == other.magnitude)
  82. && (exponent == other.exponent)
  83. && (plurality == other.plurality)
  84. && (zero_format_index == other.zero_format_index)
  85. && (positive_format_index == other.positive_format_index)
  86. && (negative_format_index == other.negative_format_index)
  87. && (identifier_indices == other.identifier_indices);
  88. }
  89. StringIndexType zero_format_index { 0 };
  90. StringIndexType positive_format_index { 0 };
  91. StringIndexType negative_format_index { 0 };
  92. Vector<StringIndexType> identifier_indices {};
  93. };
  94. template<>
  95. struct AK::Formatter<NumberFormat> : Formatter<FormatString> {
  96. ErrorOr<void> format(FormatBuilder& builder, NumberFormat const& format)
  97. {
  98. StringBuilder identifier_indices;
  99. identifier_indices.join(", "sv, format.identifier_indices);
  100. return Formatter<FormatString>::format(builder,
  101. "{{ {}, {}, {}, {}, {}, {}, {{ {} }} }}",
  102. format.magnitude,
  103. format.exponent,
  104. static_cast<u8>(format.plurality),
  105. format.zero_format_index,
  106. format.positive_format_index,
  107. format.negative_format_index,
  108. identifier_indices.build());
  109. }
  110. };
  111. template<>
  112. struct AK::Traits<NumberFormat> : public GenericTraits<NumberFormat> {
  113. static unsigned hash(NumberFormat const& f) { return f.hash(); }
  114. };
  115. using NumberFormatList = Vector<NumberFormatIndexType>;
  116. using NumericSymbolList = Vector<StringIndexType>;
  117. struct NumberSystem {
  118. unsigned hash() const
  119. {
  120. auto hash = int_hash(symbols);
  121. hash = pair_int_hash(hash, primary_grouping_size);
  122. hash = pair_int_hash(hash, secondary_grouping_size);
  123. hash = pair_int_hash(hash, decimal_format);
  124. hash = pair_int_hash(hash, decimal_long_formats);
  125. hash = pair_int_hash(hash, decimal_short_formats);
  126. hash = pair_int_hash(hash, currency_format);
  127. hash = pair_int_hash(hash, accounting_format);
  128. hash = pair_int_hash(hash, currency_unit_formats);
  129. hash = pair_int_hash(hash, currency_short_formats);
  130. hash = pair_int_hash(hash, percent_format);
  131. hash = pair_int_hash(hash, scientific_format);
  132. return hash;
  133. }
  134. bool operator==(NumberSystem const& other) const
  135. {
  136. return (symbols == other.symbols)
  137. && (primary_grouping_size == other.primary_grouping_size)
  138. && (secondary_grouping_size == other.secondary_grouping_size)
  139. && (decimal_format == other.decimal_format)
  140. && (decimal_long_formats == other.decimal_long_formats)
  141. && (decimal_short_formats == other.decimal_short_formats)
  142. && (currency_format == other.currency_format)
  143. && (accounting_format == other.accounting_format)
  144. && (currency_unit_formats == other.currency_unit_formats)
  145. && (currency_short_formats == other.currency_short_formats)
  146. && (percent_format == other.percent_format)
  147. && (scientific_format == other.scientific_format);
  148. }
  149. NumericSymbolListIndexType symbols { 0 };
  150. u8 primary_grouping_size { 0 };
  151. u8 secondary_grouping_size { 0 };
  152. NumberFormatIndexType decimal_format { 0 };
  153. NumberFormatListIndexType decimal_long_formats { 0 };
  154. NumberFormatListIndexType decimal_short_formats { 0 };
  155. NumberFormatIndexType currency_format { 0 };
  156. NumberFormatIndexType accounting_format { 0 };
  157. NumberFormatListIndexType currency_unit_formats { 0 };
  158. NumberFormatListIndexType currency_short_formats { 0 };
  159. NumberFormatIndexType percent_format { 0 };
  160. NumberFormatIndexType scientific_format { 0 };
  161. };
  162. template<>
  163. struct AK::Formatter<NumberSystem> : Formatter<FormatString> {
  164. ErrorOr<void> format(FormatBuilder& builder, NumberSystem const& system)
  165. {
  166. return Formatter<FormatString>::format(builder,
  167. "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}",
  168. system.symbols,
  169. system.primary_grouping_size,
  170. system.secondary_grouping_size,
  171. system.decimal_format,
  172. system.decimal_long_formats,
  173. system.decimal_short_formats,
  174. system.currency_format,
  175. system.accounting_format,
  176. system.currency_unit_formats,
  177. system.currency_short_formats,
  178. system.percent_format,
  179. system.scientific_format);
  180. }
  181. };
  182. template<>
  183. struct AK::Traits<NumberSystem> : public GenericTraits<NumberSystem> {
  184. static unsigned hash(NumberSystem const& s) { return s.hash(); }
  185. };
  186. struct Unit {
  187. unsigned hash() const
  188. {
  189. auto hash = int_hash(unit);
  190. hash = pair_int_hash(hash, long_formats);
  191. hash = pair_int_hash(hash, short_formats);
  192. hash = pair_int_hash(hash, narrow_formats);
  193. return hash;
  194. }
  195. bool operator==(Unit const& other) const
  196. {
  197. return (unit == other.unit)
  198. && (long_formats == other.long_formats)
  199. && (short_formats == other.short_formats)
  200. && (narrow_formats == other.narrow_formats);
  201. }
  202. StringIndexType unit { 0 };
  203. NumberFormatListIndexType long_formats { 0 };
  204. NumberFormatListIndexType short_formats { 0 };
  205. NumberFormatListIndexType narrow_formats { 0 };
  206. };
  207. template<>
  208. struct AK::Formatter<Unit> : Formatter<FormatString> {
  209. ErrorOr<void> format(FormatBuilder& builder, Unit const& system)
  210. {
  211. return Formatter<FormatString>::format(builder,
  212. "{{ {}, {}, {}, {} }}",
  213. system.unit,
  214. system.long_formats,
  215. system.short_formats,
  216. system.narrow_formats);
  217. }
  218. };
  219. template<>
  220. struct AK::Traits<Unit> : public GenericTraits<Unit> {
  221. static unsigned hash(Unit const& u) { return u.hash(); }
  222. };
  223. struct Locale {
  224. Vector<NumberSystemIndexType> number_systems;
  225. HashMap<String, UnitIndexType> units {};
  226. u8 minimum_grouping_digits { 0 };
  227. };
  228. struct UnicodeLocaleData {
  229. UniqueStringStorage<StringIndexType> unique_strings;
  230. UniqueStorage<NumberFormat, NumberFormatIndexType> unique_formats;
  231. UniqueStorage<NumberFormatList, NumberFormatListIndexType> unique_format_lists;
  232. UniqueStorage<NumericSymbolList, NumericSymbolListIndexType> unique_symbols;
  233. UniqueStorage<NumberSystem, NumberSystemIndexType> unique_systems;
  234. UniqueStorage<Unit, UnitIndexType> unique_units;
  235. HashMap<String, Array<u32, 10>> number_system_digits;
  236. Vector<String> number_systems;
  237. HashMap<String, Locale> locales;
  238. size_t max_identifier_count { 0 };
  239. };
  240. static ErrorOr<void> parse_number_system_digits(String core_supplemental_path, UnicodeLocaleData& locale_data)
  241. {
  242. LexicalPath number_systems_path(move(core_supplemental_path));
  243. number_systems_path = number_systems_path.append("numberingSystems.json"sv);
  244. auto number_systems = TRY(read_json_file(number_systems_path.string()));
  245. auto const& supplemental_object = number_systems.as_object().get("supplemental"sv);
  246. auto const& number_systems_object = supplemental_object.as_object().get("numberingSystems"sv);
  247. number_systems_object.as_object().for_each_member([&](auto const& number_system, auto const& digits_object) {
  248. auto type = digits_object.as_object().get("_type"sv).as_string();
  249. if (type != "numeric"sv)
  250. return;
  251. auto digits = digits_object.as_object().get("_digits"sv).as_string();
  252. Utf8View utf8_digits { digits };
  253. VERIFY(utf8_digits.length() == 10);
  254. auto& number_system_digits = locale_data.number_system_digits.ensure(number_system);
  255. size_t index = 0;
  256. for (u32 digit : utf8_digits)
  257. number_system_digits[index++] = digit;
  258. if (!locale_data.number_systems.contains_slow(number_system))
  259. locale_data.number_systems.append(number_system);
  260. });
  261. return {};
  262. }
  263. static String parse_identifiers(String pattern, StringView replacement, UnicodeLocaleData& locale_data, NumberFormat& format)
  264. {
  265. static constexpr Utf8View whitespace { "\u0020\u00a0\u200f"sv };
  266. while (true) {
  267. Utf8View utf8_pattern { pattern };
  268. Optional<size_t> start_index;
  269. Optional<size_t> end_index;
  270. bool inside_replacement = false;
  271. for (auto it = utf8_pattern.begin(); it != utf8_pattern.end(); ++it) {
  272. if (*it == '{') {
  273. if (start_index.has_value()) {
  274. end_index = utf8_pattern.byte_offset_of(it);
  275. break;
  276. }
  277. inside_replacement = true;
  278. } else if (*it == '}') {
  279. inside_replacement = false;
  280. } else if (!inside_replacement && !start_index.has_value() && !whitespace.contains(*it)) {
  281. start_index = utf8_pattern.byte_offset_of(it);
  282. }
  283. }
  284. if (!start_index.has_value())
  285. return pattern;
  286. end_index = end_index.value_or(pattern.length());
  287. utf8_pattern = utf8_pattern.substring_view(*start_index, *end_index - *start_index);
  288. utf8_pattern = utf8_pattern.trim(whitespace);
  289. auto identifier = utf8_pattern.as_string().replace("'.'"sv, "."sv, ReplaceMode::FirstOnly);
  290. auto identifier_index = locale_data.unique_strings.ensure(move(identifier));
  291. size_t replacement_index = 0;
  292. if (auto index = format.identifier_indices.find_first_index(identifier_index); index.has_value()) {
  293. replacement_index = *index;
  294. } else {
  295. replacement_index = format.identifier_indices.size();
  296. format.identifier_indices.append(identifier_index);
  297. locale_data.max_identifier_count = max(locale_data.max_identifier_count, format.identifier_indices.size());
  298. }
  299. pattern = String::formatted("{}{{{}:{}}}{}",
  300. *start_index > 0 ? pattern.substring_view(0, *start_index) : ""sv,
  301. replacement,
  302. replacement_index,
  303. pattern.substring_view(*start_index + utf8_pattern.byte_length()));
  304. }
  305. }
  306. static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& locale_data, NumberFormatType type, NumberFormat& format, NumberSystem* number_system_for_groupings = nullptr)
  307. {
  308. // https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns
  309. // https://cldr.unicode.org/translation/number-currency-formats/number-and-currency-patterns
  310. VERIFY((patterns.size() == 1) || (patterns.size() == 2));
  311. auto replace_patterns = [&](String pattern) {
  312. static HashMap<StringView, StringView> replacements = {
  313. { "{0}"sv, "{number}"sv },
  314. { "{1}"sv, "{currency}"sv },
  315. { "%"sv, "{percentSign}"sv },
  316. { "+"sv, "{plusSign}"sv },
  317. { "-"sv, "{minusSign}"sv },
  318. { "¤"sv, "{currency}"sv }, // U+00A4 Currency Sign
  319. { "E"sv, "{scientificSeparator}"sv },
  320. };
  321. for (auto const& replacement : replacements)
  322. pattern = pattern.replace(replacement.key, replacement.value, ReplaceMode::All);
  323. if (auto start_number_index = pattern.find_any_of("#0"sv, String::SearchDirection::Forward); start_number_index.has_value()) {
  324. auto end_number_index = *start_number_index + 1;
  325. for (; end_number_index < pattern.length(); ++end_number_index) {
  326. auto ch = pattern[end_number_index];
  327. if ((ch != '#') && (ch != '0') && (ch != ',') && (ch != '.'))
  328. break;
  329. }
  330. if (number_system_for_groupings) {
  331. auto number_pattern = pattern.substring_view(*start_number_index, end_number_index - *start_number_index);
  332. auto group_separators = number_pattern.find_all(","sv);
  333. VERIFY((group_separators.size() == 1) || (group_separators.size() == 2));
  334. auto decimal = number_pattern.find('.');
  335. VERIFY(decimal.has_value());
  336. if (group_separators.size() == 1) {
  337. number_system_for_groupings->primary_grouping_size = *decimal - group_separators[0] - 1;
  338. number_system_for_groupings->secondary_grouping_size = number_system_for_groupings->primary_grouping_size;
  339. } else {
  340. number_system_for_groupings->primary_grouping_size = *decimal - group_separators[1] - 1;
  341. number_system_for_groupings->secondary_grouping_size = group_separators[1] - group_separators[0] - 1;
  342. }
  343. }
  344. pattern = String::formatted("{}{{number}}{}",
  345. *start_number_index > 0 ? pattern.substring_view(0, *start_number_index) : ""sv,
  346. pattern.substring_view(end_number_index));
  347. // This is specifically handled here rather than in the replacements HashMap above so
  348. // that we do not errantly replace zeroes in number patterns.
  349. if (pattern.contains(*replacements.get("E"sv)))
  350. pattern = pattern.replace("0"sv, "{scientificExponent}"sv, ReplaceMode::FirstOnly);
  351. }
  352. if (type == NumberFormatType::Compact)
  353. return parse_identifiers(move(pattern), "compactIdentifier"sv, locale_data, format);
  354. return pattern;
  355. };
  356. auto zero_format = replace_patterns(move(patterns[0]));
  357. format.positive_format_index = locale_data.unique_strings.ensure(String::formatted("{{plusSign}}{}", zero_format));
  358. if (patterns.size() == 2) {
  359. auto negative_format = replace_patterns(move(patterns[1]));
  360. format.negative_format_index = locale_data.unique_strings.ensure(move(negative_format));
  361. } else {
  362. format.negative_format_index = locale_data.unique_strings.ensure(String::formatted("{{minusSign}}{}", zero_format));
  363. }
  364. format.zero_format_index = locale_data.unique_strings.ensure(move(zero_format));
  365. }
  366. static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& locale_data, NumberFormatType type, NumberFormatIndexType& format_index, NumberSystem* number_system_for_groupings = nullptr)
  367. {
  368. NumberFormat format {};
  369. parse_number_pattern(move(patterns), locale_data, type, format, number_system_for_groupings);
  370. format_index = locale_data.unique_formats.ensure(move(format));
  371. }
  372. static ErrorOr<void> parse_number_systems(String locale_numbers_path, UnicodeLocaleData& locale_data, Locale& locale)
  373. {
  374. LexicalPath numbers_path(move(locale_numbers_path));
  375. numbers_path = numbers_path.append("numbers.json"sv);
  376. auto numbers = TRY(read_json_file(numbers_path.string()));
  377. auto const& main_object = numbers.as_object().get("main"sv);
  378. auto const& locale_object = main_object.as_object().get(numbers_path.parent().basename());
  379. auto const& locale_numbers_object = locale_object.as_object().get("numbers"sv);
  380. auto const& minimum_grouping_digits = locale_numbers_object.as_object().get("minimumGroupingDigits"sv);
  381. Vector<Optional<NumberSystem>> number_systems;
  382. number_systems.resize(locale_data.number_systems.size());
  383. auto ensure_number_system = [&](auto const& system) -> NumberSystem& {
  384. auto system_index = locale_data.number_systems.find_first_index(system).value();
  385. VERIFY(system_index < number_systems.size());
  386. auto& number_system = number_systems.at(system_index);
  387. if (!number_system.has_value())
  388. number_system = NumberSystem {};
  389. return number_system.value();
  390. };
  391. auto parse_number_format = [&](auto const& format_object) {
  392. Vector<NumberFormatIndexType> result;
  393. result.ensure_capacity(format_object.size());
  394. format_object.for_each_member([&](auto const& key, JsonValue const& value) {
  395. auto split_key = key.split_view('-');
  396. if (split_key.size() != 3)
  397. return;
  398. auto patterns = value.as_string().split(';');
  399. NumberFormat format {};
  400. if (auto type = split_key[0].template to_uint<u64>(); type.has_value()) {
  401. VERIFY(*type % 10 == 0);
  402. format.magnitude = static_cast<u8>(log10(*type));
  403. if (patterns[0] != "0"sv) {
  404. auto number_of_zeroes_in_pattern = patterns[0].count("0"sv);
  405. VERIFY(format.magnitude >= number_of_zeroes_in_pattern);
  406. format.exponent = format.magnitude + 1 - number_of_zeroes_in_pattern;
  407. }
  408. } else {
  409. VERIFY(split_key[0] == "unitPattern"sv);
  410. }
  411. format.plurality = NumberFormat::plurality_from_string(split_key[2]);
  412. parse_number_pattern(move(patterns), locale_data, NumberFormatType::Compact, format);
  413. auto format_index = locale_data.unique_formats.ensure(move(format));
  414. result.append(format_index);
  415. });
  416. return locale_data.unique_format_lists.ensure(move(result));
  417. };
  418. auto numeric_symbol_from_string = [&](StringView numeric_symbol) -> Optional<Unicode::NumericSymbol> {
  419. if (numeric_symbol == "decimal"sv)
  420. return Unicode::NumericSymbol::Decimal;
  421. if (numeric_symbol == "exponential"sv)
  422. return Unicode::NumericSymbol::Exponential;
  423. if (numeric_symbol == "group"sv)
  424. return Unicode::NumericSymbol::Group;
  425. if (numeric_symbol == "infinity"sv)
  426. return Unicode::NumericSymbol::Infinity;
  427. if (numeric_symbol == "minusSign"sv)
  428. return Unicode::NumericSymbol::MinusSign;
  429. if (numeric_symbol == "nan"sv)
  430. return Unicode::NumericSymbol::NaN;
  431. if (numeric_symbol == "percentSign"sv)
  432. return Unicode::NumericSymbol::PercentSign;
  433. if (numeric_symbol == "plusSign"sv)
  434. return Unicode::NumericSymbol::PlusSign;
  435. if (numeric_symbol == "timeSeparator"sv)
  436. return Unicode::NumericSymbol::TimeSeparator;
  437. return {};
  438. };
  439. locale_numbers_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
  440. constexpr auto symbols_prefix = "symbols-numberSystem-"sv;
  441. constexpr auto decimal_formats_prefix = "decimalFormats-numberSystem-"sv;
  442. constexpr auto currency_formats_prefix = "currencyFormats-numberSystem-"sv;
  443. constexpr auto percent_formats_prefix = "percentFormats-numberSystem-"sv;
  444. constexpr auto scientific_formats_prefix = "scientificFormats-numberSystem-"sv;
  445. if (key.starts_with(symbols_prefix)) {
  446. auto system = key.substring(symbols_prefix.length());
  447. auto& number_system = ensure_number_system(system);
  448. NumericSymbolList symbols;
  449. value.as_object().for_each_member([&](auto const& symbol, JsonValue const& localization) {
  450. auto numeric_symbol = numeric_symbol_from_string(symbol);
  451. if (!numeric_symbol.has_value())
  452. return;
  453. if (to_underlying(*numeric_symbol) >= symbols.size())
  454. symbols.resize(to_underlying(*numeric_symbol) + 1);
  455. auto symbol_index = locale_data.unique_strings.ensure(localization.as_string());
  456. symbols[to_underlying(*numeric_symbol)] = symbol_index;
  457. });
  458. number_system.symbols = locale_data.unique_symbols.ensure(move(symbols));
  459. } else if (key.starts_with(decimal_formats_prefix)) {
  460. auto system = key.substring(decimal_formats_prefix.length());
  461. auto& number_system = ensure_number_system(system);
  462. auto format_object = value.as_object().get("standard"sv);
  463. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.decimal_format, &number_system);
  464. auto const& long_format = value.as_object().get("long"sv).as_object().get("decimalFormat"sv);
  465. number_system.decimal_long_formats = parse_number_format(long_format.as_object());
  466. auto const& short_format = value.as_object().get("short"sv).as_object().get("decimalFormat"sv);
  467. number_system.decimal_short_formats = parse_number_format(short_format.as_object());
  468. } else if (key.starts_with(currency_formats_prefix)) {
  469. auto system = key.substring(currency_formats_prefix.length());
  470. auto& number_system = ensure_number_system(system);
  471. auto format_object = value.as_object().get("standard"sv);
  472. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.currency_format);
  473. format_object = value.as_object().get("accounting"sv);
  474. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.accounting_format);
  475. number_system.currency_unit_formats = parse_number_format(value.as_object());
  476. if (value.as_object().has("short"sv)) {
  477. auto const& short_format = value.as_object().get("short"sv).as_object().get("standard"sv);
  478. number_system.currency_short_formats = parse_number_format(short_format.as_object());
  479. }
  480. } else if (key.starts_with(percent_formats_prefix)) {
  481. auto system = key.substring(percent_formats_prefix.length());
  482. auto& number_system = ensure_number_system(system);
  483. auto format_object = value.as_object().get("standard"sv);
  484. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.percent_format);
  485. } else if (key.starts_with(scientific_formats_prefix)) {
  486. auto system = key.substring(scientific_formats_prefix.length());
  487. auto& number_system = ensure_number_system(system);
  488. auto format_object = value.as_object().get("standard"sv);
  489. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.scientific_format);
  490. }
  491. });
  492. locale.number_systems.ensure_capacity(number_systems.size());
  493. for (auto& number_system : number_systems) {
  494. NumberSystemIndexType system_index = 0;
  495. if (number_system.has_value())
  496. system_index = locale_data.unique_systems.ensure(number_system.release_value());
  497. locale.number_systems.append(system_index);
  498. }
  499. locale.minimum_grouping_digits = minimum_grouping_digits.as_string().template to_uint<u8>().value();
  500. return {};
  501. }
  502. static ErrorOr<void> parse_units(String locale_units_path, UnicodeLocaleData& locale_data, Locale& locale)
  503. {
  504. LexicalPath units_path(move(locale_units_path));
  505. units_path = units_path.append("units.json"sv);
  506. auto locale_units = TRY(read_json_file(units_path.string()));
  507. auto const& main_object = locale_units.as_object().get("main"sv);
  508. auto const& locale_object = main_object.as_object().get(units_path.parent().basename());
  509. auto const& locale_units_object = locale_object.as_object().get("units"sv);
  510. auto const& long_object = locale_units_object.as_object().get("long"sv);
  511. auto const& short_object = locale_units_object.as_object().get("short"sv);
  512. auto const& narrow_object = locale_units_object.as_object().get("narrow"sv);
  513. HashMap<String, Unit> units;
  514. auto ensure_unit = [&](auto const& unit) -> Unit& {
  515. return units.ensure(unit, [&]() {
  516. auto unit_index = locale_data.unique_strings.ensure(unit);
  517. return Unit { .unit = unit_index };
  518. });
  519. };
  520. auto is_sanctioned_unit = [](StringView unit_name) {
  521. // LibUnicode generally tries to avoid being directly dependent on ECMA-402, but this rather significantly reduces the amount
  522. // of data generated here, and ECMA-402 is currently the only consumer of this data.
  523. constexpr auto sanctioned_units = JS::Intl::sanctioned_single_unit_identifiers();
  524. if (find(sanctioned_units.begin(), sanctioned_units.end(), unit_name) != sanctioned_units.end())
  525. return true;
  526. static constexpr auto extra_sanctioned_units = JS::Intl::extra_sanctioned_single_unit_identifiers();
  527. return find(extra_sanctioned_units.begin(), extra_sanctioned_units.end(), unit_name) != extra_sanctioned_units.end();
  528. };
  529. auto parse_units_object = [&](auto const& units_object, Unicode::Style style) {
  530. constexpr auto unit_pattern_prefix = "unitPattern-count-"sv;
  531. constexpr auto combined_unit_separator = "-per-"sv;
  532. units_object.for_each_member([&](auto const& key, JsonValue const& value) {
  533. auto end_of_category = key.find('-');
  534. if (!end_of_category.has_value())
  535. return;
  536. auto unit_name = key.substring(*end_of_category + 1);
  537. if (!is_sanctioned_unit(unit_name)) {
  538. auto indices = unit_name.find_all(combined_unit_separator);
  539. if (indices.size() != 1)
  540. return;
  541. auto numerator = unit_name.substring_view(0, indices[0]);
  542. auto denominator = unit_name.substring_view(indices[0] + combined_unit_separator.length());
  543. if (!is_sanctioned_unit(numerator) || !is_sanctioned_unit(denominator))
  544. return;
  545. }
  546. auto& unit = ensure_unit(unit_name);
  547. NumberFormatList formats;
  548. value.as_object().for_each_member([&](auto const& unit_key, JsonValue const& pattern_value) {
  549. if (!unit_key.starts_with(unit_pattern_prefix))
  550. return;
  551. NumberFormat format {};
  552. auto plurality = unit_key.substring_view(unit_pattern_prefix.length());
  553. format.plurality = NumberFormat::plurality_from_string(plurality);
  554. auto zero_format = pattern_value.as_string().replace("{0}"sv, "{number}"sv, ReplaceMode::FirstOnly);
  555. zero_format = parse_identifiers(zero_format, "unitIdentifier"sv, locale_data, format);
  556. format.positive_format_index = locale_data.unique_strings.ensure(zero_format.replace("{number}"sv, "{plusSign}{number}"sv, ReplaceMode::FirstOnly));
  557. format.negative_format_index = locale_data.unique_strings.ensure(zero_format.replace("{number}"sv, "{minusSign}{number}"sv, ReplaceMode::FirstOnly));
  558. format.zero_format_index = locale_data.unique_strings.ensure(move(zero_format));
  559. formats.append(locale_data.unique_formats.ensure(move(format)));
  560. });
  561. auto number_format_list_index = locale_data.unique_format_lists.ensure(move(formats));
  562. switch (style) {
  563. case Unicode::Style::Long:
  564. unit.long_formats = number_format_list_index;
  565. break;
  566. case Unicode::Style::Short:
  567. unit.short_formats = number_format_list_index;
  568. break;
  569. case Unicode::Style::Narrow:
  570. unit.narrow_formats = number_format_list_index;
  571. break;
  572. default:
  573. VERIFY_NOT_REACHED();
  574. }
  575. });
  576. };
  577. parse_units_object(long_object.as_object(), Unicode::Style::Long);
  578. parse_units_object(short_object.as_object(), Unicode::Style::Short);
  579. parse_units_object(narrow_object.as_object(), Unicode::Style::Narrow);
  580. for (auto& unit : units) {
  581. auto unit_index = locale_data.unique_units.ensure(move(unit.value));
  582. locale.units.set(unit.key, unit_index);
  583. }
  584. return {};
  585. }
  586. static ErrorOr<void> parse_all_locales(String core_path, String numbers_path, String units_path, UnicodeLocaleData& locale_data)
  587. {
  588. auto numbers_iterator = TRY(path_to_dir_iterator(move(numbers_path)));
  589. auto units_iterator = TRY(path_to_dir_iterator(move(units_path)));
  590. LexicalPath core_supplemental_path(move(core_path));
  591. core_supplemental_path = core_supplemental_path.append("supplemental"sv);
  592. VERIFY(Core::File::is_directory(core_supplemental_path.string()));
  593. TRY(parse_number_system_digits(core_supplemental_path.string(), locale_data));
  594. auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
  595. auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(locale_data.unique_strings, LexicalPath::basename(path)));
  596. StringBuilder builder;
  597. builder.append(locale_data.unique_strings.get(parsed_locale.language));
  598. if (auto script = locale_data.unique_strings.get(parsed_locale.script); !script.is_empty())
  599. builder.appendff("-{}", script);
  600. if (auto region = locale_data.unique_strings.get(parsed_locale.region); !region.is_empty())
  601. builder.appendff("-{}", region);
  602. return builder.build();
  603. };
  604. while (numbers_iterator.has_next()) {
  605. auto numbers_path = TRY(next_path_from_dir_iterator(numbers_iterator));
  606. auto language = TRY(remove_variants_from_path(numbers_path));
  607. auto& locale = locale_data.locales.ensure(language);
  608. TRY(parse_number_systems(numbers_path, locale_data, locale));
  609. }
  610. while (units_iterator.has_next()) {
  611. auto units_path = TRY(next_path_from_dir_iterator(units_iterator));
  612. auto language = TRY(remove_variants_from_path(units_path));
  613. auto& locale = locale_data.locales.ensure(language);
  614. TRY(parse_units(units_path, locale_data, locale));
  615. }
  616. return {};
  617. }
  618. static String format_identifier(StringView, String identifier)
  619. {
  620. return identifier.to_titlecase();
  621. }
  622. static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile& file, UnicodeLocaleData& locale_data)
  623. {
  624. StringBuilder builder;
  625. SourceGenerator generator { builder };
  626. generator.append(R"~~~(
  627. #include <AK/Types.h>
  628. #pragma once
  629. namespace Unicode {
  630. )~~~");
  631. generate_enum(generator, format_identifier, "NumberSystem"sv, {}, locale_data.number_systems);
  632. generator.append(R"~~~(
  633. }
  634. )~~~");
  635. TRY(file.write(generator.as_string_view().bytes()));
  636. return {};
  637. }
  638. static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::BufferedFile& file, UnicodeLocaleData& locale_data)
  639. {
  640. StringBuilder builder;
  641. SourceGenerator generator { builder };
  642. generator.set("string_index_type"sv, s_string_index_type);
  643. generator.set("number_format_index_type"sv, s_number_format_index_type);
  644. generator.set("number_format_list_index_type"sv, s_number_format_list_index_type);
  645. generator.set("numeric_symbol_list_index_type"sv, s_numeric_symbol_list_index_type);
  646. generator.set("identifier_count", String::number(locale_data.max_identifier_count));
  647. generator.append(R"~~~(
  648. #include <AK/Array.h>
  649. #include <AK/BinarySearch.h>
  650. #include <AK/Optional.h>
  651. #include <AK/Span.h>
  652. #include <AK/StringView.h>
  653. #include <AK/Vector.h>
  654. #include <LibUnicode/Locale.h>
  655. #include <LibUnicode/NumberFormat.h>
  656. #include <LibUnicode/UnicodeLocale.h>
  657. #include <LibUnicode/UnicodeNumberFormat.h>
  658. namespace Unicode {
  659. )~~~");
  660. locale_data.unique_strings.generate(generator);
  661. generator.append(R"~~~(
  662. struct NumberFormatImpl {
  663. NumberFormat to_unicode_number_format() const {
  664. NumberFormat number_format {};
  665. number_format.magnitude = magnitude;
  666. number_format.exponent = exponent;
  667. number_format.plurality = static_cast<NumberFormat::Plurality>(plurality);
  668. number_format.zero_format = s_string_list[zero_format];
  669. number_format.positive_format = s_string_list[positive_format];
  670. number_format.negative_format = s_string_list[negative_format];
  671. number_format.identifiers.ensure_capacity(identifiers.size());
  672. for (@string_index_type@ identifier : identifiers)
  673. number_format.identifiers.append(s_string_list[identifier]);
  674. return number_format;
  675. }
  676. u8 magnitude { 0 };
  677. u8 exponent { 0 };
  678. u8 plurality { 0 };
  679. @string_index_type@ zero_format { 0 };
  680. @string_index_type@ positive_format { 0 };
  681. @string_index_type@ negative_format { 0 };
  682. Array<@string_index_type@, @identifier_count@> identifiers {};
  683. };
  684. struct NumberSystemData {
  685. @numeric_symbol_list_index_type@ symbols { 0 };
  686. u8 primary_grouping_size { 0 };
  687. u8 secondary_grouping_size { 0 };
  688. @number_format_index_type@ decimal_format { 0 };
  689. @number_format_list_index_type@ decimal_long_formats { 0 };
  690. @number_format_list_index_type@ decimal_short_formats { 0 };
  691. @number_format_index_type@ currency_format { 0 };
  692. @number_format_index_type@ accounting_format { 0 };
  693. @number_format_list_index_type@ currency_unit_formats { 0 };
  694. @number_format_list_index_type@ currency_short_formats { 0 };
  695. @number_format_index_type@ percent_format { 0 };
  696. @number_format_index_type@ scientific_format { 0 };
  697. };
  698. struct Unit {
  699. @string_index_type@ unit { 0 };
  700. @number_format_list_index_type@ long_formats { 0 };
  701. @number_format_list_index_type@ short_formats { 0 };
  702. @number_format_list_index_type@ narrow_formats { 0 };
  703. };
  704. )~~~");
  705. locale_data.unique_formats.generate(generator, "NumberFormatImpl"sv, "s_number_formats"sv, 10);
  706. locale_data.unique_format_lists.generate(generator, s_number_format_index_type, "s_number_format_lists"sv);
  707. locale_data.unique_symbols.generate(generator, s_string_index_type, "s_numeric_symbol_lists"sv);
  708. locale_data.unique_systems.generate(generator, "NumberSystemData"sv, "s_number_systems"sv, 10);
  709. locale_data.unique_units.generate(generator, "Unit"sv, "s_units"sv, 10);
  710. auto locales = locale_data.locales.keys();
  711. quick_sort(locales);
  712. generator.set("size", String::number(locales.size()));
  713. generator.append(R"~~~(
  714. static constexpr Array<u8, @size@> s_minimum_grouping_digits { { )~~~");
  715. bool first = true;
  716. for (auto const& locale : locales) {
  717. generator.append(first ? " " : ", ");
  718. generator.append(String::number(locale_data.locales.find(locale)->value.minimum_grouping_digits));
  719. first = false;
  720. }
  721. generator.append(" } };\n");
  722. auto append_map = [&](String name, auto type, auto const& map) {
  723. generator.set("name", name);
  724. generator.set("type", type);
  725. generator.set("size", String::number(map.size()));
  726. generator.append(R"~~~(
  727. static constexpr Array<@type@, @size@> @name@ { {)~~~");
  728. bool first = true;
  729. for (auto const& item : map) {
  730. generator.append(first ? " " : ", ");
  731. if constexpr (requires { item.value; })
  732. generator.append(String::number(item.value));
  733. else
  734. generator.append(String::number(item));
  735. first = false;
  736. }
  737. generator.append(" } };");
  738. };
  739. generate_mapping(generator, locale_data.number_system_digits, "u32"sv, "s_number_systems_digits"sv, "s_number_systems_digits_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, "u32"sv, value); });
  740. generate_mapping(generator, locale_data.locales, s_number_system_index_type, "s_locale_number_systems"sv, "s_number_systems_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, s_number_system_index_type, value.number_systems); });
  741. generate_mapping(generator, locale_data.locales, s_unit_index_type, "s_locale_units"sv, "s_units_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, s_unit_index_type, value.units); });
  742. generator.append(R"~~~(
  743. static Optional<NumberSystem> keyword_to_number_system(KeywordNumbers keyword)
  744. {
  745. switch (keyword) {)~~~");
  746. for (auto const& number_system : locale_data.number_systems) {
  747. generator.set("name"sv, format_identifier({}, number_system));
  748. generator.append(R"~~~(
  749. case KeywordNumbers::@name@:
  750. return NumberSystem::@name@;)~~~");
  751. }
  752. generator.append(R"~~~(
  753. default:
  754. return {};
  755. }
  756. }
  757. Optional<Span<u32 const>> get_digits_for_number_system(StringView system)
  758. {
  759. auto number_system_keyword = keyword_nu_from_string(system);
  760. if (!number_system_keyword.has_value())
  761. return {};
  762. auto number_system_value = keyword_to_number_system(*number_system_keyword);
  763. if (!number_system_value.has_value())
  764. return {};
  765. auto number_system_index = to_underlying(*number_system_value);
  766. return s_number_systems_digits[number_system_index];
  767. }
  768. static NumberSystemData const* find_number_system(StringView locale, StringView system)
  769. {
  770. auto locale_value = locale_from_string(locale);
  771. if (!locale_value.has_value())
  772. return nullptr;
  773. auto number_system_keyword = keyword_nu_from_string(system);
  774. if (!number_system_keyword.has_value())
  775. return {};
  776. auto number_system_value = keyword_to_number_system(*number_system_keyword);
  777. if (!number_system_value.has_value())
  778. return {};
  779. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  780. auto number_system_index = to_underlying(*number_system_value);
  781. auto const& number_systems = s_locale_number_systems.at(locale_index);
  782. number_system_index = number_systems.at(number_system_index);
  783. if (number_system_index == 0)
  784. return nullptr;
  785. return &s_number_systems.at(number_system_index);
  786. }
  787. Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol)
  788. {
  789. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  790. auto symbols = s_numeric_symbol_lists.at(number_system->symbols);
  791. auto symbol_index = to_underlying(symbol);
  792. if (symbol_index >= symbols.size())
  793. return {};
  794. return s_string_list[symbols[symbol_index]];
  795. }
  796. return {};
  797. }
  798. Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system)
  799. {
  800. auto locale_value = locale_from_string(locale);
  801. if (!locale_value.has_value())
  802. return {};
  803. u8 minimum_grouping_digits = s_minimum_grouping_digits[to_underlying(*locale_value) - 1];
  804. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr)
  805. return NumberGroupings { minimum_grouping_digits, number_system->primary_grouping_size, number_system->secondary_grouping_size };
  806. return {};
  807. }
  808. Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type)
  809. {
  810. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  811. @number_format_index_type@ format_index = 0;
  812. switch (type) {
  813. case StandardNumberFormatType::Decimal:
  814. format_index = number_system->decimal_format;
  815. break;
  816. case StandardNumberFormatType::Currency:
  817. format_index = number_system->currency_format;
  818. break;
  819. case StandardNumberFormatType::Accounting:
  820. format_index = number_system->accounting_format;
  821. break;
  822. case StandardNumberFormatType::Percent:
  823. format_index = number_system->percent_format;
  824. break;
  825. case StandardNumberFormatType::Scientific:
  826. format_index = number_system->scientific_format;
  827. break;
  828. }
  829. return s_number_formats[format_index].to_unicode_number_format();
  830. }
  831. return {};
  832. }
  833. Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type)
  834. {
  835. Vector<NumberFormat> formats;
  836. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  837. @number_format_list_index_type@ number_format_list_index { 0 };
  838. switch (type) {
  839. case CompactNumberFormatType::DecimalLong:
  840. number_format_list_index = number_system->decimal_long_formats;
  841. break;
  842. case CompactNumberFormatType::DecimalShort:
  843. number_format_list_index = number_system->decimal_short_formats;
  844. break;
  845. case CompactNumberFormatType::CurrencyUnit:
  846. number_format_list_index = number_system->currency_unit_formats;
  847. break;
  848. case CompactNumberFormatType::CurrencyShort:
  849. number_format_list_index = number_system->currency_short_formats;
  850. break;
  851. }
  852. auto number_formats = s_number_format_lists.at(number_format_list_index);
  853. formats.ensure_capacity(number_formats.size());
  854. for (auto number_format : number_formats)
  855. formats.append(s_number_formats[number_format].to_unicode_number_format());
  856. }
  857. return formats;
  858. }
  859. static Unit const* find_units(StringView locale, StringView unit)
  860. {
  861. auto locale_value = locale_from_string(locale);
  862. if (!locale_value.has_value())
  863. return nullptr;
  864. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  865. auto const& locale_units = s_locale_units.at(locale_index);
  866. for (auto unit_index : locale_units) {
  867. auto const& units = s_units.at(unit_index);
  868. if (unit == s_string_list[units.unit])
  869. return &units;
  870. };
  871. return nullptr;
  872. }
  873. Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style)
  874. {
  875. Vector<NumberFormat> formats;
  876. if (auto const* units = find_units(locale, unit); units != nullptr) {
  877. @number_format_list_index_type@ number_format_list_index { 0 };
  878. switch (style) {
  879. case Style::Long:
  880. number_format_list_index = units->long_formats;
  881. break;
  882. case Style::Short:
  883. number_format_list_index = units->short_formats;
  884. break;
  885. case Style::Narrow:
  886. number_format_list_index = units->narrow_formats;
  887. break;
  888. default:
  889. VERIFY_NOT_REACHED();
  890. }
  891. auto number_formats = s_number_format_lists.at(number_format_list_index);
  892. formats.ensure_capacity(number_formats.size());
  893. for (auto number_format : number_formats)
  894. formats.append(s_number_formats[number_format].to_unicode_number_format());
  895. }
  896. return formats;
  897. }
  898. }
  899. )~~~");
  900. TRY(file.write(generator.as_string_view().bytes()));
  901. return {};
  902. }
  903. ErrorOr<int> serenity_main(Main::Arguments arguments)
  904. {
  905. StringView generated_header_path;
  906. StringView generated_implementation_path;
  907. StringView core_path;
  908. StringView numbers_path;
  909. StringView units_path;
  910. Core::ArgsParser args_parser;
  911. args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path");
  912. args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  913. args_parser.add_option(core_path, "Path to cldr-core directory", "core-path", 'r', "core-path");
  914. args_parser.add_option(numbers_path, "Path to cldr-numbers directory", "numbers-path", 'n', "numbers-path");
  915. args_parser.add_option(units_path, "Path to cldr-units directory", "units-path", 'u', "units-path");
  916. args_parser.parse(arguments);
  917. auto generated_header_file = TRY(open_file(generated_header_path, Core::Stream::OpenMode::Write));
  918. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::Stream::OpenMode::Write));
  919. UnicodeLocaleData locale_data;
  920. TRY(parse_all_locales(core_path, numbers_path, units_path, locale_data));
  921. TRY(generate_unicode_locale_header(*generated_header_file, locale_data));
  922. TRY(generate_unicode_locale_implementation(*generated_implementation_file, locale_data));
  923. return 0;
  924. }