GenerateUnicodeData.cpp 46 KB

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