GenerateUnicodeData.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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/HashMap.h>
  12. #include <AK/Optional.h>
  13. #include <AK/QuickSort.h>
  14. #include <AK/SourceGenerator.h>
  15. #include <AK/String.h>
  16. #include <AK/StringUtils.h>
  17. #include <AK/Types.h>
  18. #include <AK/Vector.h>
  19. #include <LibCore/ArgsParser.h>
  20. #include <LibCore/File.h>
  21. // Some code points are excluded from UnicodeData.txt, and instead are part of a "range" of code
  22. // points, as indicated by the "name" field. For example:
  23. // 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;
  24. // 4DBF;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;;
  25. struct CodePointRange {
  26. u32 first;
  27. u32 last;
  28. };
  29. // SpecialCasing source: https://www.unicode.org/Public/13.0.0/ucd/SpecialCasing.txt
  30. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#SpecialCasing.txt
  31. struct SpecialCasing {
  32. u32 index { 0 };
  33. u32 code_point { 0 };
  34. Vector<u32> lowercase_mapping;
  35. Vector<u32> uppercase_mapping;
  36. Vector<u32> titlecase_mapping;
  37. String locale;
  38. String condition;
  39. };
  40. // PropList source: https://www.unicode.org/Public/13.0.0/ucd/PropList.txt
  41. // Property descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#PropList.txt
  42. using PropList = HashMap<String, Vector<CodePointRange>>;
  43. // Normalization source: https://www.unicode.org/Public/13.0.0/ucd/DerivedNormalizationProps.txt
  44. // Normalization descriptions: https://www.unicode.org/reports/tr44/#DerivedNormalizationProps.txt
  45. enum class QuickCheck {
  46. Yes,
  47. No,
  48. Maybe,
  49. };
  50. struct Normalization {
  51. CodePointRange code_point_range;
  52. Vector<u32> value;
  53. QuickCheck quick_check { QuickCheck::Yes };
  54. };
  55. using NormalizationProps = HashMap<String, Vector<Normalization>>;
  56. struct CodePointName {
  57. CodePointRange code_point_range;
  58. StringView name;
  59. };
  60. // UnicodeData source: https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt
  61. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#UnicodeData.txt
  62. // https://www.unicode.org/reports/tr44/#General_Category_Values
  63. struct CodePointData {
  64. u32 code_point { 0 };
  65. String name;
  66. u8 canonical_combining_class { 0 };
  67. String bidi_class;
  68. String decomposition_type;
  69. Optional<i8> numeric_value_decimal;
  70. Optional<i8> numeric_value_digit;
  71. Optional<i8> numeric_value_numeric;
  72. bool bidi_mirrored { false };
  73. String unicode_1_name;
  74. String iso_comment;
  75. Optional<u32> simple_uppercase_mapping;
  76. Optional<u32> simple_lowercase_mapping;
  77. Optional<u32> simple_titlecase_mapping;
  78. Vector<u32> special_casing_indices;
  79. };
  80. struct UnicodeData {
  81. u32 code_points_with_non_zero_combining_class { 0 };
  82. u32 simple_uppercase_mapping_size { 0 };
  83. u32 simple_lowercase_mapping_size { 0 };
  84. Vector<SpecialCasing> special_casing;
  85. u32 code_points_with_special_casing { 0 };
  86. u32 largest_casing_transform_size { 0 };
  87. u32 largest_special_casing_size { 0 };
  88. Vector<String> conditions;
  89. Vector<CodePointData> code_point_data;
  90. HashMap<u32, String> code_point_display_name_aliases;
  91. Vector<CodePointName> code_point_display_names;
  92. PropList general_categories;
  93. Vector<Alias> general_category_aliases;
  94. // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in
  95. // any UCD file. Assigned code point ranges are derived as this generator is executed.
  96. // https://unicode.org/reports/tr18/#General_Category_Property
  97. PropList prop_list {
  98. { "Any"sv, { { 0, 0x10ffff } } },
  99. { "Assigned"sv, {} },
  100. { "ASCII"sv, { { 0, 0x7f } } },
  101. };
  102. Vector<Alias> prop_aliases;
  103. PropList script_list {
  104. { "Unknown"sv, {} },
  105. };
  106. Vector<Alias> script_aliases;
  107. PropList script_extensions;
  108. // FIXME: We are not yet doing anything with this data. It will be needed for String.prototype.normalize.
  109. NormalizationProps normalization_props;
  110. };
  111. static Vector<u32> parse_code_point_list(StringView list)
  112. {
  113. Vector<u32> code_points;
  114. auto segments = list.split_view(' ');
  115. for (auto const& code_point : segments)
  116. code_points.append(AK::StringUtils::convert_to_uint_from_hex<u32>(code_point).value());
  117. return code_points;
  118. }
  119. static CodePointRange parse_code_point_range(StringView list)
  120. {
  121. CodePointRange code_point_range {};
  122. if (list.contains(".."sv)) {
  123. auto segments = list.split_view(".."sv);
  124. VERIFY(segments.size() == 2);
  125. auto begin = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  126. auto end = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[1]).value();
  127. code_point_range = { begin, end };
  128. } else {
  129. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(list).value();
  130. code_point_range = { code_point, code_point };
  131. }
  132. return code_point_range;
  133. }
  134. static void parse_special_casing(Core::File& file, UnicodeData& unicode_data)
  135. {
  136. while (file.can_read_line()) {
  137. auto line = file.read_line();
  138. if (line.is_empty() || line.starts_with('#'))
  139. continue;
  140. if (auto index = line.find('#'); index.has_value())
  141. line = line.substring(0, *index);
  142. auto segments = line.split(';', true);
  143. VERIFY(segments.size() == 5 || segments.size() == 6);
  144. SpecialCasing casing {};
  145. casing.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  146. casing.lowercase_mapping = parse_code_point_list(segments[1]);
  147. casing.titlecase_mapping = parse_code_point_list(segments[2]);
  148. casing.uppercase_mapping = parse_code_point_list(segments[3]);
  149. if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) {
  150. auto conditions = condition.split(' ', true);
  151. VERIFY(conditions.size() == 1 || conditions.size() == 2);
  152. if (conditions.size() == 2) {
  153. casing.locale = move(conditions[0]);
  154. casing.condition = move(conditions[1]);
  155. } else if (all_of(conditions[0], is_ascii_lower_alpha)) {
  156. casing.locale = move(conditions[0]);
  157. } else {
  158. casing.condition = move(conditions[0]);
  159. }
  160. if (!casing.locale.is_empty())
  161. casing.locale = String::formatted("{:c}{}", to_ascii_uppercase(casing.locale[0]), casing.locale.substring_view(1));
  162. casing.condition = casing.condition.replace("_", "", true);
  163. if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition))
  164. unicode_data.conditions.append(casing.condition);
  165. }
  166. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.lowercase_mapping.size());
  167. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.titlecase_mapping.size());
  168. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.uppercase_mapping.size());
  169. unicode_data.special_casing.append(move(casing));
  170. }
  171. quick_sort(unicode_data.special_casing, [](auto const& lhs, auto const& rhs) {
  172. if (lhs.code_point != rhs.code_point)
  173. return lhs.code_point < rhs.code_point;
  174. if (lhs.locale.is_empty() && !rhs.locale.is_empty())
  175. return false;
  176. if (!lhs.locale.is_empty() && rhs.locale.is_empty())
  177. return true;
  178. return lhs.locale < rhs.locale;
  179. });
  180. for (u32 i = 0; i < unicode_data.special_casing.size(); ++i)
  181. unicode_data.special_casing[i].index = i;
  182. }
  183. static void parse_prop_list(Core::File& file, PropList& prop_list, bool multi_value_property = false)
  184. {
  185. while (file.can_read_line()) {
  186. auto line = file.read_line();
  187. if (line.is_empty() || line.starts_with('#'))
  188. continue;
  189. if (auto index = line.find('#'); index.has_value())
  190. line = line.substring(0, *index);
  191. auto segments = line.split_view(';', true);
  192. VERIFY(segments.size() == 2);
  193. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  194. Vector<StringView> properties;
  195. if (multi_value_property)
  196. properties = segments[1].trim_whitespace().split_view(' ');
  197. else
  198. properties = { segments[1].trim_whitespace() };
  199. for (auto const& property : properties) {
  200. auto& code_points = prop_list.ensure(property.trim_whitespace());
  201. code_points.append(code_point_range);
  202. }
  203. }
  204. }
  205. static void parse_alias_list(Core::File& file, PropList const& prop_list, Vector<Alias>& prop_aliases)
  206. {
  207. String current_property;
  208. auto append_alias = [&](auto alias, auto property) {
  209. // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip.
  210. if (alias == property)
  211. return;
  212. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  213. if (!prop_list.contains(property))
  214. return;
  215. prop_aliases.append({ property, alias });
  216. };
  217. while (file.can_read_line()) {
  218. auto line = file.read_line();
  219. if (line.is_empty() || line.starts_with('#')) {
  220. if (line.ends_with("Properties"sv))
  221. current_property = line.substring(2);
  222. continue;
  223. }
  224. // Note: For now, we only care about Binary Property aliases for Unicode property escapes.
  225. if (current_property != "Binary Properties"sv)
  226. continue;
  227. auto segments = line.split_view(';', true);
  228. VERIFY((segments.size() == 2) || (segments.size() == 3));
  229. auto alias = segments[0].trim_whitespace();
  230. auto property = segments[1].trim_whitespace();
  231. append_alias(alias, property);
  232. if (segments.size() == 3) {
  233. alias = segments[2].trim_whitespace();
  234. append_alias(alias, property);
  235. }
  236. }
  237. }
  238. static void parse_name_aliases(Core::File& file, UnicodeData& unicode_data)
  239. {
  240. while (file.can_read_line()) {
  241. auto line = file.read_line();
  242. if (line.is_empty() || line.starts_with('#'))
  243. continue;
  244. auto segments = line.split_view(';', true);
  245. VERIFY(segments.size() == 3);
  246. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0].trim_whitespace());
  247. auto alias = segments[1].trim_whitespace();
  248. auto reason = segments[2].trim_whitespace();
  249. if (!reason.is_one_of("correction"sv, "control"sv))
  250. continue;
  251. if (!unicode_data.code_point_display_name_aliases.contains(*code_point))
  252. unicode_data.code_point_display_name_aliases.set(*code_point, alias);
  253. }
  254. }
  255. static void parse_value_alias_list(Core::File& file, StringView desired_category, Vector<String> const& value_list, Vector<Alias>& prop_aliases, bool primary_value_is_first = true)
  256. {
  257. VERIFY(file.seek(0));
  258. auto append_alias = [&](auto alias, auto value) {
  259. // Note: The value alias file contains lines such as "Ahom = Ahom", which we should just skip.
  260. if (alias == value)
  261. return;
  262. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  263. if (!value_list.contains_slow(value))
  264. return;
  265. prop_aliases.append({ value, alias });
  266. };
  267. while (file.can_read_line()) {
  268. auto line = file.read_line();
  269. if (line.is_empty() || line.starts_with('#'))
  270. continue;
  271. if (auto index = line.find('#'); index.has_value())
  272. line = line.substring(0, *index);
  273. auto segments = line.split_view(';', true);
  274. auto category = segments[0].trim_whitespace();
  275. if (category != desired_category)
  276. continue;
  277. VERIFY((segments.size() == 3) || (segments.size() == 4));
  278. auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
  279. auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
  280. append_alias(alias, value);
  281. if (segments.size() == 4) {
  282. alias = segments[3].trim_whitespace();
  283. append_alias(alias, value);
  284. }
  285. }
  286. }
  287. static void parse_normalization_props(Core::File& file, UnicodeData& unicode_data)
  288. {
  289. while (file.can_read_line()) {
  290. auto line = file.read_line();
  291. if (line.is_empty() || line.starts_with('#'))
  292. continue;
  293. if (auto index = line.find('#'); index.has_value())
  294. line = line.substring(0, *index);
  295. auto segments = line.split_view(';', true);
  296. VERIFY((segments.size() == 2) || (segments.size() == 3));
  297. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  298. auto property = segments[1].trim_whitespace().to_string();
  299. Vector<u32> value;
  300. QuickCheck quick_check = QuickCheck::Yes;
  301. if (segments.size() == 3) {
  302. auto value_or_quick_check = segments[2].trim_whitespace();
  303. if ((value_or_quick_check == "N"sv))
  304. quick_check = QuickCheck::No;
  305. else if ((value_or_quick_check == "M"sv))
  306. quick_check = QuickCheck::Maybe;
  307. else
  308. value = parse_code_point_list(value_or_quick_check);
  309. }
  310. auto& normalizations = unicode_data.normalization_props.ensure(property);
  311. normalizations.append({ code_point_range, move(value), quick_check });
  312. auto& prop_list = unicode_data.prop_list.ensure(property);
  313. prop_list.append(move(code_point_range));
  314. }
  315. }
  316. static void add_canonical_code_point_name(CodePointRange range, StringView name, UnicodeData& unicode_data)
  317. {
  318. // https://www.unicode.org/versions/Unicode14.0.0/ch04.pdf#G142981
  319. // FIXME: Implement the NR1 rules for Hangul syllables.
  320. // These code point ranges are the NR2 set of name replacements defined by Table 4-8.
  321. constexpr Array<CodePointName, 15> s_ideographic_replacements { {
  322. { { 0x3400, 0x4DBF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  323. { { 0x4E00, 0x9FFC }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  324. { { 0xF900, 0xFA6D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  325. { { 0xFA70, 0xFAD9 }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  326. { { 0x17000, 0x187F7 }, "TANGUT IDEOGRAPH-{:X}"sv },
  327. { { 0x18B00, 0x18CD5 }, "KHITAN SMALL SCRIPT CHARACTER-{:X}"sv },
  328. { { 0x18D00, 0x18D08 }, "TANGUT IDEOGRAPH-{:X}"sv },
  329. { { 0x1B170, 0x1B2FB }, "NUSHU CHARACTER-{:X}"sv },
  330. { { 0x20000, 0x2A6DD }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  331. { { 0x2A700, 0x2B734 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  332. { { 0x2B740, 0x2B81D }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  333. { { 0x2B820, 0x2CEA1 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  334. { { 0x2CEB0, 0x2EBE0 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  335. { { 0x2F800, 0x2FA1D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  336. { { 0x30000, 0x3134A }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  337. } };
  338. auto it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  339. [&](auto const& replacement) {
  340. return replacement.code_point_range.first == range.first;
  341. });
  342. if (it != s_ideographic_replacements.end()) {
  343. unicode_data.code_point_display_names.append(*it);
  344. return;
  345. }
  346. it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  347. [&](auto const& replacement) {
  348. return (replacement.code_point_range.first <= range.first) && (range.first <= replacement.code_point_range.last);
  349. });
  350. if (it != s_ideographic_replacements.end()) {
  351. // Drop code points that will have been captured by a range defined by the ideographic replacements.
  352. return;
  353. }
  354. if (auto alias = unicode_data.code_point_display_name_aliases.get(range.first); alias.has_value()) {
  355. // NR4 states that control code points have a null string as their name. Our implementation
  356. // uses the control code's alias as its display name.
  357. unicode_data.code_point_display_names.append({ range, *alias });
  358. return;
  359. }
  360. unicode_data.code_point_display_names.append({ range, name });
  361. }
  362. static void parse_unicode_data(Core::File& file, UnicodeData& unicode_data)
  363. {
  364. Optional<u32> code_point_range_start;
  365. auto& assigned_code_points = unicode_data.prop_list.find("Assigned"sv)->value;
  366. Optional<u32> assigned_code_point_range_start = 0;
  367. u32 previous_code_point = 0;
  368. while (file.can_read_line()) {
  369. auto line = file.read_line();
  370. if (line.is_empty())
  371. continue;
  372. auto segments = line.split(';', true);
  373. VERIFY(segments.size() == 15);
  374. CodePointData data {};
  375. data.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  376. data.name = move(segments[1]);
  377. data.canonical_combining_class = AK::StringUtils::convert_to_uint<u8>(segments[3]).value();
  378. data.bidi_class = move(segments[4]);
  379. data.decomposition_type = move(segments[5]);
  380. data.numeric_value_decimal = AK::StringUtils::convert_to_int<i8>(segments[6]);
  381. data.numeric_value_digit = AK::StringUtils::convert_to_int<i8>(segments[7]);
  382. data.numeric_value_numeric = AK::StringUtils::convert_to_int<i8>(segments[8]);
  383. data.bidi_mirrored = segments[9] == "Y"sv;
  384. data.unicode_1_name = move(segments[10]);
  385. data.iso_comment = move(segments[11]);
  386. data.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[12]);
  387. data.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[13]);
  388. data.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[14]);
  389. if (!assigned_code_point_range_start.has_value())
  390. assigned_code_point_range_start = data.code_point;
  391. if (data.name.starts_with("<"sv) && data.name.ends_with(", First>")) {
  392. VERIFY(!code_point_range_start.has_value() && assigned_code_point_range_start.has_value());
  393. code_point_range_start = data.code_point;
  394. data.name = data.name.substring(1, data.name.length() - 9);
  395. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  396. assigned_code_point_range_start.clear();
  397. } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>")) {
  398. VERIFY(code_point_range_start.has_value());
  399. CodePointRange code_point_range { *code_point_range_start, data.code_point };
  400. assigned_code_points.append(code_point_range);
  401. data.name = data.name.substring(1, data.name.length() - 8);
  402. code_point_range_start.clear();
  403. add_canonical_code_point_name(code_point_range, data.name, unicode_data);
  404. } else {
  405. add_canonical_code_point_name({ data.code_point, data.code_point }, data.name, unicode_data);
  406. if ((data.code_point > 0) && (data.code_point - previous_code_point) != 1) {
  407. VERIFY(assigned_code_point_range_start.has_value());
  408. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  409. assigned_code_point_range_start = data.code_point;
  410. }
  411. }
  412. bool has_special_casing { false };
  413. for (auto const& casing : unicode_data.special_casing) {
  414. if (casing.code_point == data.code_point) {
  415. data.special_casing_indices.append(casing.index);
  416. has_special_casing = true;
  417. }
  418. }
  419. unicode_data.code_points_with_non_zero_combining_class += data.canonical_combining_class != 0;
  420. unicode_data.simple_uppercase_mapping_size += data.simple_uppercase_mapping.has_value();
  421. unicode_data.simple_lowercase_mapping_size += data.simple_lowercase_mapping.has_value();
  422. unicode_data.code_points_with_special_casing += has_special_casing;
  423. unicode_data.largest_special_casing_size = max(unicode_data.largest_special_casing_size, data.special_casing_indices.size());
  424. previous_code_point = data.code_point;
  425. unicode_data.code_point_data.append(move(data));
  426. }
  427. }
  428. static void generate_unicode_data_header(Core::File& file, UnicodeData& unicode_data)
  429. {
  430. StringBuilder builder;
  431. SourceGenerator generator { builder };
  432. generator.set("casing_transform_size", String::number(unicode_data.largest_casing_transform_size));
  433. auto generate_enum = [&](StringView name, StringView default_, Vector<String> values, Vector<Alias> aliases = {}) {
  434. quick_sort(values);
  435. quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; });
  436. generator.set("name", name);
  437. generator.set("underlying", String::formatted("{}UnderlyingType", name));
  438. generator.append(R"~~~(
  439. using @underlying@ = u8;
  440. enum class @name@ : @underlying@ {)~~~");
  441. if (!default_.is_empty()) {
  442. generator.set("default", default_);
  443. generator.append(R"~~~(
  444. @default@,)~~~");
  445. }
  446. for (auto const& value : values) {
  447. generator.set("value", value);
  448. generator.append(R"~~~(
  449. @value@,)~~~");
  450. }
  451. for (auto const& alias : aliases) {
  452. generator.set("alias", alias.alias);
  453. generator.set("value", alias.name);
  454. generator.append(R"~~~(
  455. @alias@ = @value@,)~~~");
  456. }
  457. generator.append(R"~~~(
  458. };
  459. )~~~");
  460. };
  461. generator.append(R"~~~(
  462. #pragma once
  463. #include <AK/Optional.h>
  464. #include <AK/Span.h>
  465. #include <AK/Types.h>
  466. #include <LibUnicode/Forward.h>
  467. #include <LibUnicode/UnicodeLocale.h>
  468. namespace Unicode {
  469. )~~~");
  470. generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions));
  471. generate_enum("GeneralCategory"sv, {}, unicode_data.general_categories.keys(), unicode_data.general_category_aliases);
  472. generate_enum("Property"sv, {}, unicode_data.prop_list.keys(), unicode_data.prop_aliases);
  473. generate_enum("Script"sv, {}, unicode_data.script_list.keys(), unicode_data.script_aliases);
  474. generator.append(R"~~~(
  475. struct SpecialCasing {
  476. u32 code_point { 0 };
  477. u32 lowercase_mapping[@casing_transform_size@];
  478. u32 lowercase_mapping_size { 0 };
  479. u32 uppercase_mapping[@casing_transform_size@];
  480. u32 uppercase_mapping_size { 0 };
  481. u32 titlecase_mapping[@casing_transform_size@];
  482. u32 titlecase_mapping_size { 0 };
  483. Locale locale { Locale::None };
  484. Condition condition { Condition::None };
  485. };
  486. namespace Detail {
  487. Optional<String> code_point_display_name(u32 code_point);
  488. u32 canonical_combining_class(u32 code_point);
  489. u32 simple_uppercase_mapping(u32 code_point);
  490. u32 simple_lowercase_mapping(u32 code_point);
  491. Span<SpecialCasing const* const> special_case_mapping(u32 code_point);
  492. bool code_point_has_general_category(u32 code_point, GeneralCategory general_category);
  493. Optional<GeneralCategory> general_category_from_string(StringView general_category);
  494. bool code_point_has_property(u32 code_point, Property property);
  495. Optional<Property> property_from_string(StringView property);
  496. bool code_point_has_script(u32 code_point, Script script);
  497. bool code_point_has_script_extension(u32 code_point, Script script);
  498. Optional<Script> script_from_string(StringView script);
  499. }
  500. }
  501. )~~~");
  502. VERIFY(file.write(generator.as_string_view()));
  503. }
  504. static void generate_unicode_data_implementation(Core::File& file, UnicodeData const& unicode_data)
  505. {
  506. StringBuilder builder;
  507. SourceGenerator generator { builder };
  508. generator.set("largest_special_casing_size", String::number(unicode_data.largest_special_casing_size));
  509. generator.set("special_casing_size", String::number(unicode_data.special_casing.size()));
  510. generator.append(R"~~~(
  511. #include <AK/Array.h>
  512. #include <AK/BinarySearch.h>
  513. #include <AK/CharacterTypes.h>
  514. #include <AK/String.h>
  515. #include <AK/StringView.h>
  516. #include <LibUnicode/UnicodeData.h>
  517. namespace Unicode {
  518. )~~~");
  519. auto append_list_and_size = [&](auto const& list, StringView format) {
  520. if (list.is_empty()) {
  521. generator.append(", {}, 0");
  522. return;
  523. }
  524. bool first = true;
  525. generator.append(", {");
  526. for (auto const& item : list) {
  527. generator.append(first ? " " : ", ");
  528. generator.append(String::formatted(format, item));
  529. first = false;
  530. }
  531. generator.append(String::formatted(" }}, {}", list.size()));
  532. };
  533. generator.append(R"~~~(
  534. static constexpr Array<SpecialCasing, @special_casing_size@> s_special_casing { {)~~~");
  535. for (auto const& casing : unicode_data.special_casing) {
  536. generator.set("code_point", String::formatted("{:#x}", casing.code_point));
  537. generator.append(R"~~~(
  538. { @code_point@)~~~");
  539. constexpr auto format = "0x{:x}"sv;
  540. append_list_and_size(casing.lowercase_mapping, format);
  541. append_list_and_size(casing.uppercase_mapping, format);
  542. append_list_and_size(casing.titlecase_mapping, format);
  543. generator.set("locale", casing.locale.is_empty() ? "None" : casing.locale);
  544. generator.append(", Locale::@locale@");
  545. generator.set("condition", casing.condition.is_empty() ? "None" : casing.condition);
  546. generator.append(", Condition::@condition@");
  547. generator.append(" },");
  548. }
  549. generator.append(R"~~~(
  550. } };
  551. struct CodePointMapping {
  552. u32 code_point { 0 };
  553. u32 mapping { 0 };
  554. };
  555. struct SpecialCaseMapping {
  556. u32 code_point { 0 };
  557. Array<SpecialCasing const*, @largest_special_casing_size@> special_casing {};
  558. u32 special_casing_size { 0 };
  559. };
  560. template<typename MappingType>
  561. struct CodePointComparator {
  562. constexpr int operator()(u32 code_point, MappingType const& mapping)
  563. {
  564. return code_point - mapping.code_point;
  565. }
  566. };
  567. )~~~");
  568. auto append_code_point_mappings = [&](StringView name, StringView mapping_type, u32 size, auto mapping_getter) {
  569. generator.set("name", name);
  570. generator.set("mapping_type", mapping_type);
  571. generator.set("size", String::number(size));
  572. generator.append(R"~~~(
  573. static constexpr Array<@mapping_type@, @size@> s_@name@_mappings { {
  574. )~~~");
  575. constexpr size_t max_mappings_per_row = 20;
  576. size_t mappings_in_current_row = 0;
  577. for (auto const& data : unicode_data.code_point_data) {
  578. auto mapping = mapping_getter(data);
  579. if constexpr (IsSame<decltype(mapping), Optional<u32>>) {
  580. if (!mapping.has_value())
  581. continue;
  582. } else {
  583. if (mapping.is_empty())
  584. continue;
  585. }
  586. if (mappings_in_current_row++ > 0)
  587. generator.append(" ");
  588. generator.set("code_point", String::formatted("{:#x}", data.code_point));
  589. generator.append("{ @code_point@");
  590. if constexpr (IsSame<decltype(mapping), Optional<u32>>) {
  591. generator.set("mapping", String::formatted("{:#x}", *mapping));
  592. generator.append(", @mapping@ },");
  593. } else {
  594. append_list_and_size(data.special_casing_indices, "&s_special_casing[{}]"sv);
  595. generator.append(" },");
  596. }
  597. if (mappings_in_current_row == max_mappings_per_row) {
  598. mappings_in_current_row = 0;
  599. generator.append("\n ");
  600. }
  601. }
  602. generator.append(R"~~~(
  603. } };
  604. )~~~");
  605. };
  606. append_code_point_mappings("combining_class"sv, "CodePointMapping"sv, unicode_data.code_points_with_non_zero_combining_class,
  607. [](auto const& data) -> Optional<u32> {
  608. if (data.canonical_combining_class == 0)
  609. return {};
  610. return data.canonical_combining_class;
  611. });
  612. append_code_point_mappings("uppercase"sv, "CodePointMapping"sv, unicode_data.simple_uppercase_mapping_size, [](auto const& data) { return data.simple_uppercase_mapping; });
  613. append_code_point_mappings("lowercase"sv, "CodePointMapping"sv, unicode_data.simple_lowercase_mapping_size, [](auto const& data) { return data.simple_lowercase_mapping; });
  614. append_code_point_mappings("special_case"sv, "SpecialCaseMapping"sv, unicode_data.code_points_with_special_casing, [](auto const& data) { return data.special_casing_indices; });
  615. generator.append(R"~~~(
  616. struct CodePointRange {
  617. u32 first { 0 };
  618. u32 last { 0 };
  619. };
  620. struct CodePointRangeComparator {
  621. constexpr int operator()(u32 code_point, CodePointRange const& range)
  622. {
  623. return (code_point > range.last) - (code_point < range.first);
  624. }
  625. };
  626. )~~~");
  627. auto append_code_point_range_list = [&](String name, Vector<CodePointRange> const& ranges) {
  628. generator.set("name", name);
  629. generator.set("size", String::number(ranges.size()));
  630. generator.append(R"~~~(
  631. static constexpr Array<CodePointRange, @size@> @name@ { {
  632. )~~~");
  633. constexpr size_t max_ranges_per_row = 20;
  634. size_t ranges_in_current_row = 0;
  635. for (auto const& range : ranges) {
  636. if (ranges_in_current_row++ > 0)
  637. generator.append(" ");
  638. generator.set("first", String::formatted("{:#x}", range.first));
  639. generator.set("last", String::formatted("{:#x}", range.last));
  640. generator.append("{ @first@, @last@ },");
  641. if (ranges_in_current_row == max_ranges_per_row) {
  642. ranges_in_current_row = 0;
  643. generator.append("\n ");
  644. }
  645. }
  646. generator.append(R"~~~(
  647. } };
  648. )~~~");
  649. };
  650. auto append_prop_list = [&](StringView collection_name, StringView property_format, PropList const& property_list) {
  651. for (auto const& property : property_list) {
  652. auto name = String::formatted(property_format, property.key);
  653. append_code_point_range_list(move(name), property.value);
  654. }
  655. auto property_names = property_list.keys();
  656. quick_sort(property_names);
  657. generator.set("name", collection_name);
  658. generator.set("size", String::number(property_names.size()));
  659. generator.append(R"~~~(
  660. static constexpr Array<Span<CodePointRange const>, @size@> @name@ { {)~~~");
  661. for (auto const& property_name : property_names) {
  662. generator.set("name", String::formatted(property_format, property_name));
  663. generator.append(R"~~~(
  664. @name@.span(),)~~~");
  665. }
  666. generator.append(R"~~~(
  667. } };
  668. )~~~");
  669. };
  670. append_prop_list("s_general_categories"sv, "s_general_category_{}"sv, unicode_data.general_categories);
  671. append_prop_list("s_properties"sv, "s_property_{}"sv, unicode_data.prop_list);
  672. append_prop_list("s_scripts"sv, "s_script_{}"sv, unicode_data.script_list);
  673. append_prop_list("s_script_extensions"sv, "s_script_extension_{}"sv, unicode_data.script_extensions);
  674. generator.append(R"~~~(
  675. struct CodePointName {
  676. CodePointRange code_point_range {};
  677. StringView display_name;
  678. };
  679. struct CodePointNameComparator : public CodePointRangeComparator {
  680. constexpr int operator()(u32 code_point, CodePointName const& name)
  681. {
  682. return CodePointRangeComparator::operator()(code_point, name.code_point_range);
  683. }
  684. };
  685. )~~~");
  686. generator.set("code_point_display_names_size", String::number(unicode_data.code_point_display_names.size()));
  687. generator.append(R"~~~(
  688. static constexpr Array<CodePointName, @code_point_display_names_size@> s_code_point_display_names { {
  689. )~~~");
  690. for (auto const& code_point_name : unicode_data.code_point_display_names) {
  691. generator.set("first", String::formatted("{:#x}", code_point_name.code_point_range.first));
  692. generator.set("last", String::formatted("{:#x}", code_point_name.code_point_range.last));
  693. generator.set("name", code_point_name.name);
  694. generator.append(R"~~~( { { @first@, @last@ }, "@name@"sv },
  695. )~~~");
  696. }
  697. generator.append(R"~~~(} };
  698. )~~~");
  699. generator.append(R"~~~(
  700. namespace Detail {
  701. Optional<String> code_point_display_name(u32 code_point)
  702. {
  703. if (auto const* entry = binary_search(s_code_point_display_names, code_point, nullptr, CodePointNameComparator {})) {
  704. if (entry->display_name.ends_with("{:X}"sv))
  705. return String::formatted(entry->display_name, code_point);
  706. return entry->display_name;
  707. }
  708. return {};
  709. }
  710. )~~~");
  711. auto append_code_point_mapping_search = [&](StringView method, StringView mappings, StringView fallback) {
  712. generator.set("method", method);
  713. generator.set("mappings", mappings);
  714. generator.set("fallback", fallback);
  715. generator.append(R"~~~(
  716. u32 @method@(u32 code_point)
  717. {
  718. auto const* mapping = binary_search(@mappings@, code_point, nullptr, CodePointComparator<CodePointMapping> {});
  719. return mapping ? mapping->mapping : @fallback@;
  720. }
  721. )~~~");
  722. };
  723. append_code_point_mapping_search("canonical_combining_class"sv, "s_combining_class_mappings"sv, "0"sv);
  724. append_code_point_mapping_search("simple_uppercase_mapping"sv, "s_uppercase_mappings"sv, "code_point"sv);
  725. append_code_point_mapping_search("simple_lowercase_mapping"sv, "s_lowercase_mappings"sv, "code_point"sv);
  726. generator.append(R"~~~(
  727. Span<SpecialCasing const* const> special_case_mapping(u32 code_point)
  728. {
  729. auto const* mapping = binary_search(s_special_case_mappings, code_point, nullptr, CodePointComparator<SpecialCaseMapping> {});
  730. if (mapping == nullptr)
  731. return {};
  732. return mapping->special_casing.span().slice(0, mapping->special_casing_size);
  733. }
  734. )~~~");
  735. auto append_prop_search = [&](StringView enum_title, StringView enum_snake, StringView collection_name) {
  736. generator.set("enum_title", enum_title);
  737. generator.set("enum_snake", enum_snake);
  738. generator.set("collection_name", collection_name);
  739. generator.append(R"~~~(
  740. bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
  741. {
  742. auto index = static_cast<@enum_title@UnderlyingType>(@enum_snake@);
  743. auto const& ranges = @collection_name@.at(index);
  744. auto const* range = binary_search(ranges, code_point, nullptr, CodePointRangeComparator {});
  745. return range != nullptr;
  746. }
  747. )~~~");
  748. };
  749. auto append_from_string = [&](StringView enum_title, StringView enum_snake, PropList const& prop_list, Vector<Alias> const& aliases) {
  750. HashValueMap<StringView> hashes;
  751. hashes.ensure_capacity(prop_list.size() + aliases.size());
  752. for (auto const& prop : prop_list)
  753. hashes.set(prop.key.hash(), prop.key);
  754. for (auto const& alias : aliases)
  755. hashes.set(alias.alias.hash(), alias.alias);
  756. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
  757. };
  758. append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv);
  759. append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases);
  760. append_prop_search("Property"sv, "property"sv, "s_properties"sv);
  761. append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases);
  762. append_prop_search("Script"sv, "script"sv, "s_scripts"sv);
  763. append_prop_search("Script"sv, "script_extension"sv, "s_script_extensions"sv);
  764. append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases);
  765. generator.append(R"~~~(
  766. }
  767. }
  768. )~~~");
  769. VERIFY(file.write(generator.as_string_view()));
  770. }
  771. static Vector<u32> flatten_code_point_ranges(Vector<CodePointRange> const& code_points)
  772. {
  773. Vector<u32> flattened;
  774. for (auto const& range : code_points) {
  775. flattened.grow_capacity(range.last - range.first);
  776. for (u32 code_point = range.first; code_point <= range.last; ++code_point)
  777. flattened.append(code_point);
  778. }
  779. return flattened;
  780. }
  781. static Vector<CodePointRange> form_code_point_ranges(Vector<u32> code_points)
  782. {
  783. Vector<CodePointRange> ranges;
  784. u32 range_start = code_points[0];
  785. u32 range_end = range_start;
  786. for (size_t i = 1; i < code_points.size(); ++i) {
  787. u32 code_point = code_points[i];
  788. if ((code_point - range_end) == 1) {
  789. range_end = code_point;
  790. } else {
  791. ranges.append({ range_start, range_end });
  792. range_start = code_point;
  793. range_end = code_point;
  794. }
  795. }
  796. ranges.append({ range_start, range_end });
  797. return ranges;
  798. }
  799. static void sort_and_merge_code_point_ranges(Vector<CodePointRange>& code_points)
  800. {
  801. quick_sort(code_points, [](auto const& range1, auto const& range2) {
  802. return range1.first < range2.first;
  803. });
  804. for (size_t i = 0; i < code_points.size() - 1;) {
  805. if (code_points[i].last >= code_points[i + 1].first) {
  806. code_points[i].last = max(code_points[i].last, code_points[i + 1].last);
  807. code_points.remove(i + 1);
  808. } else {
  809. ++i;
  810. }
  811. }
  812. auto all_code_points = flatten_code_point_ranges(code_points);
  813. code_points = form_code_point_ranges(all_code_points);
  814. }
  815. static void populate_general_category_unions(PropList& general_categories)
  816. {
  817. // The Unicode standard defines General Category values which are not in any UCD file. These
  818. // values are simply unions of other values.
  819. // https://www.unicode.org/reports/tr44/#GC_Values_Table
  820. auto populate_union = [&](auto alias, auto categories) {
  821. auto& code_points = general_categories.ensure(alias);
  822. for (auto const& category : categories)
  823. code_points.extend(general_categories.find(category)->value);
  824. sort_and_merge_code_point_ranges(code_points);
  825. };
  826. populate_union("LC"sv, Array { "Ll"sv, "Lu"sv, "Lt"sv });
  827. populate_union("L"sv, Array { "Lu"sv, "Ll"sv, "Lt"sv, "Lm"sv, "Lo"sv });
  828. populate_union("M"sv, Array { "Mn"sv, "Mc"sv, "Me"sv });
  829. populate_union("N"sv, Array { "Nd"sv, "Nl"sv, "No"sv });
  830. populate_union("P"sv, Array { "Pc"sv, "Pd"sv, "Ps"sv, "Pe"sv, "Pi"sv, "Pf"sv, "Po"sv });
  831. populate_union("S"sv, Array { "Sm"sv, "Sc"sv, "Sk"sv, "So"sv });
  832. populate_union("Z"sv, Array { "Zs"sv, "Zl"sv, "Zp"sv });
  833. populate_union("C"sv, Array { "Cc"sv, "Cf"sv, "Cs"sv, "Co"sv, "Cn"sv });
  834. }
  835. static void normalize_script_extensions(PropList& script_extensions, PropList const& script_list, Vector<Alias> const& script_aliases)
  836. {
  837. // The ScriptExtensions UCD file lays out its code point ranges rather uniquely compared to
  838. // other files. The Script listed on each line may either be a full Script string or an aliased
  839. // abbreviation. Further, the extensions may or may not include the base Script list. Normalize
  840. // the extensions here to be keyed by the full Script name and always include the base list.
  841. auto extensions = move(script_extensions);
  842. script_extensions = script_list;
  843. for (auto const& extension : extensions) {
  844. auto it = find_if(script_aliases.begin(), script_aliases.end(), [&](auto const& alias) { return extension.key == alias.alias; });
  845. auto const& key = (it == script_aliases.end()) ? extension.key : it->name;
  846. auto& code_points = script_extensions.find(key)->value;
  847. code_points.extend(extension.value);
  848. sort_and_merge_code_point_ranges(code_points);
  849. }
  850. // Lastly, the Common and Inherited script extensions are special. They must not contain any
  851. // code points which appear in other script extensions. The ScriptExtensions UCD file does not
  852. // list these extensions, therefore this peculiarity must be handled programmatically.
  853. // https://www.unicode.org/reports/tr24/#Assignment_ScriptX_Values
  854. auto code_point_has_other_extension = [&](StringView key, u32 code_point) {
  855. for (auto const& extension : extensions) {
  856. if (extension.key == key)
  857. continue;
  858. if (any_of(extension.value, [&](auto const& r) { return (r.first <= code_point) && (code_point <= r.last); }))
  859. return true;
  860. }
  861. return false;
  862. };
  863. auto get_code_points_without_other_extensions = [&](StringView key) {
  864. auto code_points = flatten_code_point_ranges(script_list.find(key)->value);
  865. code_points.remove_all_matching([&](u32 c) { return code_point_has_other_extension(key, c); });
  866. return code_points;
  867. };
  868. auto common_code_points = get_code_points_without_other_extensions("Common"sv);
  869. script_extensions.set("Common"sv, form_code_point_ranges(common_code_points));
  870. auto inherited_code_points = get_code_points_without_other_extensions("Inherited"sv);
  871. script_extensions.set("Inherited"sv, form_code_point_ranges(inherited_code_points));
  872. }
  873. ErrorOr<int> serenity_main(Main::Arguments arguments)
  874. {
  875. StringView generated_header_path;
  876. StringView generated_implementation_path;
  877. StringView unicode_data_path;
  878. StringView special_casing_path;
  879. StringView derived_general_category_path;
  880. StringView prop_list_path;
  881. StringView derived_core_prop_path;
  882. StringView derived_binary_prop_path;
  883. StringView prop_alias_path;
  884. StringView prop_value_alias_path;
  885. StringView name_alias_path;
  886. StringView scripts_path;
  887. StringView script_extensions_path;
  888. StringView emoji_data_path;
  889. StringView normalization_path;
  890. Core::ArgsParser args_parser;
  891. args_parser.add_option(generated_header_path, "Path to the Unicode Data header file to generate", "generated-header-path", 'h', "generated-header-path");
  892. args_parser.add_option(generated_implementation_path, "Path to the Unicode Data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  893. args_parser.add_option(unicode_data_path, "Path to UnicodeData.txt file", "unicode-data-path", 'u', "unicode-data-path");
  894. args_parser.add_option(special_casing_path, "Path to SpecialCasing.txt file", "special-casing-path", 's', "special-casing-path");
  895. args_parser.add_option(derived_general_category_path, "Path to DerivedGeneralCategory.txt file", "derived-general-category-path", 'g', "derived-general-category-path");
  896. args_parser.add_option(prop_list_path, "Path to PropList.txt file", "prop-list-path", 'p', "prop-list-path");
  897. args_parser.add_option(derived_core_prop_path, "Path to DerivedCoreProperties.txt file", "derived-core-prop-path", 'd', "derived-core-prop-path");
  898. args_parser.add_option(derived_binary_prop_path, "Path to DerivedBinaryProperties.txt file", "derived-binary-prop-path", 'b', "derived-binary-prop-path");
  899. args_parser.add_option(prop_alias_path, "Path to PropertyAliases.txt file", "prop-alias-path", 'a', "prop-alias-path");
  900. args_parser.add_option(prop_value_alias_path, "Path to PropertyValueAliases.txt file", "prop-value-alias-path", 'v', "prop-value-alias-path");
  901. args_parser.add_option(name_alias_path, "Path to NameAliases.txt file", "name-alias-path", 'm', "name-alias-path");
  902. args_parser.add_option(scripts_path, "Path to Scripts.txt file", "scripts-path", 'r', "scripts-path");
  903. args_parser.add_option(script_extensions_path, "Path to ScriptExtensions.txt file", "script-extensions-path", 'x', "script-extensions-path");
  904. args_parser.add_option(emoji_data_path, "Path to emoji-data.txt file", "emoji-data-path", 'e', "emoji-data-path");
  905. args_parser.add_option(normalization_path, "Path to DerivedNormalizationProps.txt file", "normalization-path", 'n', "normalization-path");
  906. args_parser.parse(arguments);
  907. auto open_file = [&](StringView path, Core::OpenMode mode = Core::OpenMode::ReadOnly) -> ErrorOr<NonnullRefPtr<Core::File>> {
  908. if (path.is_empty()) {
  909. args_parser.print_usage(stderr, arguments.argv[0]);
  910. return Error::from_string_literal("Must provide all command line options"sv);
  911. }
  912. return Core::File::open(path, mode);
  913. };
  914. auto generated_header_file = TRY(open_file(generated_header_path, Core::OpenMode::ReadWrite));
  915. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::OpenMode::ReadWrite));
  916. auto unicode_data_file = TRY(open_file(unicode_data_path));
  917. auto derived_general_category_file = TRY(open_file(derived_general_category_path));
  918. auto special_casing_file = TRY(open_file(special_casing_path));
  919. auto prop_list_file = TRY(open_file(prop_list_path));
  920. auto derived_core_prop_file = TRY(open_file(derived_core_prop_path));
  921. auto derived_binary_prop_file = TRY(open_file(derived_binary_prop_path));
  922. auto prop_alias_file = TRY(open_file(prop_alias_path));
  923. auto prop_value_alias_file = TRY(open_file(prop_value_alias_path));
  924. auto name_alias_file = TRY(open_file(name_alias_path));
  925. auto scripts_file = TRY(open_file(scripts_path));
  926. auto script_extensions_file = TRY(open_file(script_extensions_path));
  927. auto emoji_data_file = TRY(open_file(emoji_data_path));
  928. auto normalization_file = TRY(open_file(normalization_path));
  929. UnicodeData unicode_data {};
  930. parse_special_casing(special_casing_file, unicode_data);
  931. parse_prop_list(derived_general_category_file, unicode_data.general_categories);
  932. parse_prop_list(prop_list_file, unicode_data.prop_list);
  933. parse_prop_list(derived_core_prop_file, unicode_data.prop_list);
  934. parse_prop_list(derived_binary_prop_file, unicode_data.prop_list);
  935. parse_prop_list(emoji_data_file, unicode_data.prop_list);
  936. parse_normalization_props(normalization_file, unicode_data);
  937. parse_alias_list(prop_alias_file, unicode_data.prop_list, unicode_data.prop_aliases);
  938. parse_prop_list(scripts_file, unicode_data.script_list);
  939. parse_prop_list(script_extensions_file, unicode_data.script_extensions, true);
  940. parse_name_aliases(name_alias_file, unicode_data);
  941. populate_general_category_unions(unicode_data.general_categories);
  942. parse_unicode_data(unicode_data_file, unicode_data);
  943. parse_value_alias_list(prop_value_alias_file, "gc"sv, unicode_data.general_categories.keys(), unicode_data.general_category_aliases);
  944. parse_value_alias_list(prop_value_alias_file, "sc"sv, unicode_data.script_list.keys(), unicode_data.script_aliases, false);
  945. normalize_script_extensions(unicode_data.script_extensions, unicode_data.script_list, unicode_data.script_aliases);
  946. generate_unicode_data_header(generated_header_file, unicode_data);
  947. generate_unicode_data_implementation(generated_implementation_file, unicode_data);
  948. return 0;
  949. }