GenerateUnicodeNumberFormat.cpp 46 KB

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