GenerateUnicodeData.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GeneratorUtil.h"
  7. #include <AK/AllOf.h>
  8. #include <AK/Array.h>
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/Error.h>
  12. #include <AK/Find.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/Optional.h>
  15. #include <AK/QuickSort.h>
  16. #include <AK/SourceGenerator.h>
  17. #include <AK/StringUtils.h>
  18. #include <AK/Types.h>
  19. #include <AK/Vector.h>
  20. #include <LibCore/ArgsParser.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. // https://www.unicode.org/reports/tr44/#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. DeprecatedString locale;
  37. DeprecatedString condition;
  38. };
  39. // https://www.unicode.org/reports/tr44/#CaseFolding.txt
  40. struct CaseFolding {
  41. u32 code_point { 0 };
  42. StringView status { "Common"sv };
  43. Vector<u32> mapping { 0 };
  44. };
  45. // https://www.unicode.org/reports/tr44/#Character_Decomposition_Mappings
  46. struct CodePointDecomposition {
  47. // `tag` is a string since it's used for codegen as an enum value.
  48. DeprecatedString tag { "Canonical"sv };
  49. size_t decomposition_index { 0 };
  50. size_t decomposition_size { 0 };
  51. };
  52. // https://www.unicode.org/reports/tr44/#PropList.txt
  53. using PropList = HashMap<DeprecatedString, Vector<CodePointRange>>;
  54. // https://www.unicode.org/reports/tr44/#DerivedNormalizationProps.txt
  55. enum class QuickCheck {
  56. Yes,
  57. No,
  58. Maybe,
  59. };
  60. struct Normalization {
  61. CodePointRange code_point_range;
  62. Vector<u32> value;
  63. QuickCheck quick_check { QuickCheck::Yes };
  64. };
  65. using NormalizationProps = HashMap<DeprecatedString, Vector<Normalization>>;
  66. struct CodePointName {
  67. CodePointRange code_point_range;
  68. size_t name { 0 };
  69. };
  70. // https://www.unicode.org/reports/tr44/#UnicodeData.txt
  71. struct CodePointData {
  72. u32 code_point { 0 };
  73. DeprecatedString name;
  74. Optional<size_t> abbreviation;
  75. u8 canonical_combining_class { 0 };
  76. DeprecatedString bidi_class;
  77. Optional<CodePointDecomposition> decomposition_mapping;
  78. Optional<i8> numeric_value_decimal;
  79. Optional<i8> numeric_value_digit;
  80. Optional<i8> numeric_value_numeric;
  81. bool bidi_mirrored { false };
  82. DeprecatedString unicode_1_name;
  83. DeprecatedString iso_comment;
  84. Optional<u32> simple_uppercase_mapping;
  85. Optional<u32> simple_lowercase_mapping;
  86. Optional<u32> simple_titlecase_mapping;
  87. Vector<u32> special_casing_indices;
  88. Vector<u32> case_folding_indices;
  89. };
  90. struct BlockName {
  91. CodePointRange code_point_range;
  92. size_t name { 0 };
  93. };
  94. struct UnicodeData {
  95. UniqueStringStorage unique_strings;
  96. u32 code_points_with_non_zero_combining_class { 0 };
  97. u32 code_points_with_decomposition_mapping { 0 };
  98. Vector<u32> decomposition_mappings;
  99. Vector<DeprecatedString> compatibility_tags;
  100. u32 simple_uppercase_mapping_size { 0 };
  101. u32 simple_lowercase_mapping_size { 0 };
  102. u32 simple_titlecase_mapping_size { 0 };
  103. Vector<SpecialCasing> special_casing;
  104. u32 code_points_with_special_casing { 0 };
  105. u32 largest_special_casing_mapping_size { 0 };
  106. u32 largest_special_casing_size { 0 };
  107. Vector<DeprecatedString> conditions;
  108. Vector<DeprecatedString> locales;
  109. Vector<CaseFolding> case_folding;
  110. u32 code_points_with_case_folding { 0 };
  111. u32 largest_case_folding_mapping_size { 0 };
  112. u32 largest_case_folding_size { 0 };
  113. Vector<StringView> statuses;
  114. Vector<CodePointData> code_point_data;
  115. HashMap<u32, size_t> code_point_abbreviations;
  116. HashMap<u32, size_t> code_point_display_name_aliases;
  117. Vector<CodePointName> code_point_display_names;
  118. // https://www.unicode.org/reports/tr44/#General_Category_Values
  119. PropList general_categories;
  120. Vector<Alias> general_category_aliases;
  121. // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in
  122. // any UCD file. Assigned code point ranges are derived as this generator is executed.
  123. // https://unicode.org/reports/tr18/#General_Category_Property
  124. PropList prop_list {
  125. { "Any"sv, { { 0, 0x10ffff } } },
  126. { "Assigned"sv, {} },
  127. { "ASCII"sv, { { 0, 0x7f } } },
  128. };
  129. Vector<Alias> prop_aliases;
  130. PropList script_list {
  131. { "Unknown"sv, {} },
  132. };
  133. Vector<Alias> script_aliases;
  134. PropList script_extensions;
  135. PropList block_list {
  136. { "No_Block"sv, {} },
  137. };
  138. Vector<Alias> block_aliases;
  139. Vector<BlockName> block_display_names;
  140. // FIXME: We are not yet doing anything with this data. It will be needed for String.prototype.normalize.
  141. NormalizationProps normalization_props;
  142. PropList grapheme_break_props;
  143. PropList word_break_props;
  144. PropList sentence_break_props;
  145. };
  146. static DeprecatedString sanitize_entry(DeprecatedString const& entry)
  147. {
  148. auto sanitized = entry.replace("-"sv, "_"sv, ReplaceMode::All);
  149. sanitized = sanitized.replace(" "sv, "_"sv, ReplaceMode::All);
  150. StringBuilder builder;
  151. bool next_is_upper = true;
  152. for (auto ch : sanitized) {
  153. if (next_is_upper)
  154. builder.append_code_point(to_ascii_uppercase(ch));
  155. else
  156. builder.append_code_point(ch);
  157. next_is_upper = ch == '_';
  158. }
  159. return builder.to_deprecated_string();
  160. }
  161. static Vector<u32> parse_code_point_list(StringView list)
  162. {
  163. Vector<u32> code_points;
  164. auto segments = list.split_view(' ');
  165. for (auto const& code_point : segments)
  166. code_points.append(AK::StringUtils::convert_to_uint_from_hex<u32>(code_point).value());
  167. return code_points;
  168. }
  169. static CodePointRange parse_code_point_range(StringView list)
  170. {
  171. CodePointRange code_point_range {};
  172. if (list.contains(".."sv)) {
  173. auto segments = list.split_view(".."sv);
  174. VERIFY(segments.size() == 2);
  175. auto begin = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  176. auto end = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[1]).value();
  177. code_point_range = { begin, end };
  178. } else {
  179. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(list).value();
  180. code_point_range = { code_point, code_point };
  181. }
  182. return code_point_range;
  183. }
  184. static ErrorOr<void> parse_special_casing(Core::BufferedFile& file, UnicodeData& unicode_data)
  185. {
  186. Array<u8, 1024> buffer;
  187. while (TRY(file.can_read_line())) {
  188. auto line = TRY(file.read_line(buffer));
  189. if (line.is_empty() || line.starts_with('#'))
  190. continue;
  191. if (auto index = line.find('#'); index.has_value())
  192. line = line.substring_view(0, *index);
  193. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  194. VERIFY(segments.size() == 5 || segments.size() == 6);
  195. SpecialCasing casing {};
  196. casing.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  197. casing.lowercase_mapping = parse_code_point_list(segments[1]);
  198. casing.titlecase_mapping = parse_code_point_list(segments[2]);
  199. casing.uppercase_mapping = parse_code_point_list(segments[3]);
  200. if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) {
  201. auto conditions = condition.split_view(' ', SplitBehavior::KeepEmpty);
  202. VERIFY(conditions.size() == 1 || conditions.size() == 2);
  203. if (conditions.size() == 2) {
  204. casing.locale = conditions[0];
  205. casing.condition = conditions[1];
  206. } else if (all_of(conditions[0], is_ascii_lower_alpha)) {
  207. casing.locale = conditions[0];
  208. } else {
  209. casing.condition = conditions[0];
  210. }
  211. if (!casing.locale.is_empty()) {
  212. casing.locale = DeprecatedString::formatted("{:c}{}", to_ascii_uppercase(casing.locale[0]), casing.locale.substring_view(1));
  213. if (!unicode_data.locales.contains_slow(casing.locale))
  214. unicode_data.locales.append(casing.locale);
  215. }
  216. casing.condition = casing.condition.replace("_"sv, ""sv, ReplaceMode::All);
  217. if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition))
  218. unicode_data.conditions.append(casing.condition);
  219. }
  220. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.lowercase_mapping.size());
  221. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.titlecase_mapping.size());
  222. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.uppercase_mapping.size());
  223. unicode_data.special_casing.append(move(casing));
  224. }
  225. quick_sort(unicode_data.special_casing, [](auto const& lhs, auto const& rhs) {
  226. if (lhs.code_point != rhs.code_point)
  227. return lhs.code_point < rhs.code_point;
  228. if (lhs.locale.is_empty() && !rhs.locale.is_empty())
  229. return false;
  230. if (!lhs.locale.is_empty() && rhs.locale.is_empty())
  231. return true;
  232. return lhs.locale < rhs.locale;
  233. });
  234. for (u32 i = 0; i < unicode_data.special_casing.size(); ++i)
  235. unicode_data.special_casing[i].index = i;
  236. return {};
  237. }
  238. static ErrorOr<void> parse_case_folding(Core::BufferedFile& file, UnicodeData& unicode_data)
  239. {
  240. Array<u8, 1024> buffer;
  241. while (TRY(file.can_read_line())) {
  242. auto line = TRY(file.read_line(buffer));
  243. if (line.is_empty() || line.starts_with('#'))
  244. continue;
  245. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  246. VERIFY(segments.size() == 4);
  247. CaseFolding folding {};
  248. folding.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  249. folding.mapping = parse_code_point_list(segments[2]);
  250. switch (segments[1].trim_whitespace()[0]) {
  251. case 'C':
  252. folding.status = "Common"sv;
  253. break;
  254. case 'F':
  255. folding.status = "Full"sv;
  256. break;
  257. case 'S':
  258. folding.status = "Simple"sv;
  259. break;
  260. case 'T':
  261. folding.status = "Special"sv;
  262. break;
  263. }
  264. unicode_data.largest_case_folding_mapping_size = max(unicode_data.largest_case_folding_mapping_size, folding.mapping.size());
  265. if (!unicode_data.statuses.contains_slow(folding.status))
  266. unicode_data.statuses.append(folding.status);
  267. unicode_data.case_folding.append(move(folding));
  268. }
  269. quick_sort(unicode_data.case_folding, [](auto const& lhs, auto const& rhs) {
  270. if (lhs.code_point != rhs.code_point)
  271. return lhs.code_point < rhs.code_point;
  272. return lhs.status < rhs.status;
  273. });
  274. return {};
  275. }
  276. static ErrorOr<void> parse_prop_list(Core::BufferedFile& file, PropList& prop_list, bool multi_value_property = false, bool sanitize_property = false)
  277. {
  278. Array<u8, 1024> buffer;
  279. while (TRY(file.can_read_line())) {
  280. auto line = TRY(file.read_line(buffer));
  281. if (line.is_empty() || line.starts_with('#'))
  282. continue;
  283. if (auto index = line.find('#'); index.has_value())
  284. line = line.substring_view(0, *index);
  285. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  286. VERIFY(segments.size() == 2);
  287. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  288. Vector<StringView> properties;
  289. if (multi_value_property)
  290. properties = segments[1].trim_whitespace().split_view(' ');
  291. else
  292. properties = { segments[1].trim_whitespace() };
  293. for (auto& property : properties) {
  294. auto& code_points = prop_list.ensure(sanitize_property ? sanitize_entry(property).trim_whitespace().view() : property.trim_whitespace());
  295. code_points.append(code_point_range);
  296. }
  297. }
  298. return {};
  299. }
  300. static ErrorOr<void> parse_alias_list(Core::BufferedFile& file, PropList const& prop_list, Vector<Alias>& prop_aliases)
  301. {
  302. DeprecatedString current_property;
  303. Array<u8, 1024> buffer;
  304. auto append_alias = [&](auto alias, auto property) {
  305. // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip.
  306. if (alias == property)
  307. return;
  308. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  309. if (!prop_list.contains(property))
  310. return;
  311. prop_aliases.append({ property, alias });
  312. };
  313. while (TRY(file.can_read_line())) {
  314. auto line = TRY(file.read_line(buffer));
  315. if (line.is_empty() || line.starts_with('#')) {
  316. if (line.ends_with("Properties"sv))
  317. current_property = line.substring_view(2);
  318. continue;
  319. }
  320. // Note: For now, we only care about Binary Property aliases for Unicode property escapes.
  321. if (current_property != "Binary Properties"sv)
  322. continue;
  323. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  324. VERIFY((segments.size() == 2) || (segments.size() == 3));
  325. auto alias = segments[0].trim_whitespace();
  326. auto property = segments[1].trim_whitespace();
  327. append_alias(alias, property);
  328. if (segments.size() == 3) {
  329. alias = segments[2].trim_whitespace();
  330. append_alias(alias, property);
  331. }
  332. }
  333. return {};
  334. }
  335. static ErrorOr<void> parse_name_aliases(Core::BufferedFile& file, UnicodeData& unicode_data)
  336. {
  337. Array<u8, 1024> buffer;
  338. while (TRY(file.can_read_line())) {
  339. auto line = TRY(file.read_line(buffer));
  340. if (line.is_empty() || line.starts_with('#'))
  341. continue;
  342. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  343. VERIFY(segments.size() == 3);
  344. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0].trim_whitespace());
  345. auto alias = segments[1].trim_whitespace();
  346. auto reason = segments[2].trim_whitespace();
  347. if (reason == "abbreviation"sv) {
  348. auto index = unicode_data.unique_strings.ensure(alias);
  349. unicode_data.code_point_abbreviations.set(*code_point, index);
  350. } else if (reason.is_one_of("correction"sv, "control"sv)) {
  351. if (!unicode_data.code_point_display_name_aliases.contains(*code_point)) {
  352. auto index = unicode_data.unique_strings.ensure(alias);
  353. unicode_data.code_point_display_name_aliases.set(*code_point, index);
  354. }
  355. }
  356. }
  357. return {};
  358. }
  359. static ErrorOr<void> parse_value_alias_list(Core::BufferedFile& file, StringView desired_category, Vector<DeprecatedString> const& value_list, Vector<Alias>& prop_aliases, bool primary_value_is_first = true, bool sanitize_alias = false)
  360. {
  361. TRY(file.seek(0, SeekMode::SetPosition));
  362. Array<u8, 1024> buffer;
  363. auto append_alias = [&](auto alias, auto value) {
  364. // Note: The value alias file contains lines such as "Ahom = Ahom", which we should just skip.
  365. if (alias == value)
  366. return;
  367. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  368. if (!value_list.contains_slow(value))
  369. return;
  370. prop_aliases.append({ value, alias });
  371. };
  372. while (TRY(file.can_read_line())) {
  373. auto line = TRY(file.read_line(buffer));
  374. if (line.is_empty() || line.starts_with('#'))
  375. continue;
  376. if (auto index = line.find('#'); index.has_value())
  377. line = line.substring_view(0, *index);
  378. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  379. auto category = segments[0].trim_whitespace();
  380. if (category != desired_category)
  381. continue;
  382. VERIFY((segments.size() == 3) || (segments.size() == 4));
  383. auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
  384. auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
  385. append_alias(sanitize_alias ? sanitize_entry(alias).view() : alias, value);
  386. if (segments.size() == 4) {
  387. alias = segments[3].trim_whitespace();
  388. append_alias(sanitize_alias ? sanitize_entry(alias).view() : alias, value);
  389. }
  390. }
  391. return {};
  392. }
  393. static ErrorOr<void> parse_normalization_props(Core::BufferedFile& file, UnicodeData& unicode_data)
  394. {
  395. Array<u8, 1024> buffer;
  396. while (TRY(file.can_read_line())) {
  397. auto line = TRY(file.read_line(buffer));
  398. if (line.is_empty() || line.starts_with('#'))
  399. continue;
  400. if (auto index = line.find('#'); index.has_value())
  401. line = line.substring_view(0, *index);
  402. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  403. VERIFY((segments.size() == 2) || (segments.size() == 3));
  404. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  405. auto property = segments[1].trim_whitespace().to_deprecated_string();
  406. Vector<u32> value;
  407. QuickCheck quick_check = QuickCheck::Yes;
  408. if (segments.size() == 3) {
  409. auto value_or_quick_check = segments[2].trim_whitespace();
  410. if ((value_or_quick_check == "N"sv))
  411. quick_check = QuickCheck::No;
  412. else if ((value_or_quick_check == "M"sv))
  413. quick_check = QuickCheck::Maybe;
  414. else
  415. value = parse_code_point_list(value_or_quick_check);
  416. }
  417. auto& normalizations = unicode_data.normalization_props.ensure(property);
  418. normalizations.append({ code_point_range, move(value), quick_check });
  419. auto& prop_list = unicode_data.prop_list.ensure(property);
  420. prop_list.append(move(code_point_range));
  421. }
  422. return {};
  423. }
  424. static void add_canonical_code_point_name(CodePointRange range, StringView name, UnicodeData& unicode_data)
  425. {
  426. // https://www.unicode.org/versions/Unicode15.0.0/ch04.pdf#G142981
  427. // FIXME: Implement the NR1 rules for Hangul syllables.
  428. struct CodePointNameFormat {
  429. CodePointRange code_point_range;
  430. StringView name;
  431. };
  432. // These code point ranges are the NR2 set of name replacements defined by Table 4-8.
  433. constexpr Array<CodePointNameFormat, 16> s_ideographic_replacements { {
  434. { { 0x3400, 0x4DBF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  435. { { 0x4E00, 0x9FFF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  436. { { 0xF900, 0xFA6D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  437. { { 0xFA70, 0xFAD9 }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  438. { { 0x17000, 0x187F7 }, "TANGUT IDEOGRAPH-{:X}"sv },
  439. { { 0x18B00, 0x18CD5 }, "KHITAN SMALL SCRIPT CHARACTER-{:X}"sv },
  440. { { 0x18D00, 0x18D08 }, "TANGUT IDEOGRAPH-{:X}"sv },
  441. { { 0x1B170, 0x1B2FB }, "NUSHU CHARACTER-{:X}"sv },
  442. { { 0x20000, 0x2A6DF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  443. { { 0x2A700, 0x2B739 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  444. { { 0x2B740, 0x2B81D }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  445. { { 0x2B820, 0x2CEA1 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  446. { { 0x2CEB0, 0x2EBE0 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  447. { { 0x2F800, 0x2FA1D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  448. { { 0x30000, 0x3134A }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  449. { { 0x31350, 0x323AF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  450. } };
  451. auto it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  452. [&](auto const& replacement) {
  453. return replacement.code_point_range.first == range.first;
  454. });
  455. if (it != s_ideographic_replacements.end()) {
  456. auto index = unicode_data.unique_strings.ensure(it->name);
  457. unicode_data.code_point_display_names.append({ it->code_point_range, index });
  458. return;
  459. }
  460. it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  461. [&](auto const& replacement) {
  462. return (replacement.code_point_range.first <= range.first) && (range.first <= replacement.code_point_range.last);
  463. });
  464. if (it != s_ideographic_replacements.end()) {
  465. // Drop code points that will have been captured by a range defined by the ideographic replacements.
  466. return;
  467. }
  468. if (auto alias = unicode_data.code_point_display_name_aliases.get(range.first); alias.has_value()) {
  469. // NR4 states that control code points have a null string as their name. Our implementation
  470. // uses the control code's alias as its display name.
  471. unicode_data.code_point_display_names.append({ range, *alias });
  472. return;
  473. }
  474. auto index = unicode_data.unique_strings.ensure(name);
  475. unicode_data.code_point_display_names.append({ range, index });
  476. }
  477. static Optional<CodePointDecomposition> parse_decomposition_mapping(StringView string, UnicodeData& unicode_data)
  478. {
  479. if (string.is_empty())
  480. return {};
  481. CodePointDecomposition mapping;
  482. auto parts = string.split_view(' ');
  483. VERIFY(parts.size() > 0);
  484. if (parts.first().starts_with('<')) {
  485. auto const tag = parts.take_first().trim("<>"sv);
  486. mapping.tag = DeprecatedString::formatted("{:c}{}", to_ascii_uppercase(tag[0]), tag.substring_view(1));
  487. if (!unicode_data.compatibility_tags.contains_slow(mapping.tag))
  488. unicode_data.compatibility_tags.append(mapping.tag);
  489. }
  490. mapping.decomposition_index = unicode_data.decomposition_mappings.size();
  491. mapping.decomposition_size = parts.size();
  492. for (auto part : parts) {
  493. unicode_data.decomposition_mappings.append(AK::StringUtils::convert_to_uint_from_hex<u32>(part).value());
  494. }
  495. return mapping;
  496. }
  497. static ErrorOr<void> parse_block_display_names(Core::BufferedFile& file, UnicodeData& unicode_data)
  498. {
  499. Array<u8, 1024> buffer;
  500. while (TRY(file.can_read_line())) {
  501. auto line = TRY(file.read_line(buffer));
  502. if (line.is_empty() || line.starts_with('#'))
  503. continue;
  504. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  505. VERIFY(segments.size() == 2);
  506. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  507. auto display_name = segments[1].trim_whitespace();
  508. auto index = unicode_data.unique_strings.ensure(display_name);
  509. unicode_data.block_display_names.append({ code_point_range, index });
  510. }
  511. TRY(file.seek(0, SeekMode::SetPosition));
  512. return {};
  513. }
  514. static ErrorOr<void> parse_unicode_data(Core::BufferedFile& file, UnicodeData& unicode_data)
  515. {
  516. Optional<u32> code_point_range_start;
  517. auto& assigned_code_points = unicode_data.prop_list.find("Assigned"sv)->value;
  518. Optional<u32> assigned_code_point_range_start = 0;
  519. u32 previous_code_point = 0;
  520. Array<u8, 1024> buffer;
  521. while (TRY(file.can_read_line())) {
  522. auto line = TRY(file.read_line(buffer));
  523. if (line.is_empty())
  524. continue;
  525. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  526. VERIFY(segments.size() == 15);
  527. CodePointData data {};
  528. data.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  529. data.name = segments[1];
  530. data.canonical_combining_class = AK::StringUtils::convert_to_uint<u8>(segments[3]).value();
  531. data.bidi_class = segments[4];
  532. data.decomposition_mapping = parse_decomposition_mapping(segments[5], unicode_data);
  533. data.numeric_value_decimal = AK::StringUtils::convert_to_int<i8>(segments[6]);
  534. data.numeric_value_digit = AK::StringUtils::convert_to_int<i8>(segments[7]);
  535. data.numeric_value_numeric = AK::StringUtils::convert_to_int<i8>(segments[8]);
  536. data.bidi_mirrored = segments[9] == "Y"sv;
  537. data.unicode_1_name = segments[10];
  538. data.iso_comment = segments[11];
  539. data.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[12]);
  540. data.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[13]);
  541. data.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[14]);
  542. if (auto abbreviation = unicode_data.code_point_abbreviations.get(data.code_point); abbreviation.has_value())
  543. data.abbreviation = *abbreviation;
  544. if (!assigned_code_point_range_start.has_value())
  545. assigned_code_point_range_start = data.code_point;
  546. if (data.name.starts_with("<"sv) && data.name.ends_with(", First>"sv)) {
  547. VERIFY(!code_point_range_start.has_value() && assigned_code_point_range_start.has_value());
  548. code_point_range_start = data.code_point;
  549. data.name = data.name.substring(1, data.name.length() - 9);
  550. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  551. assigned_code_point_range_start.clear();
  552. } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>"sv)) {
  553. VERIFY(code_point_range_start.has_value());
  554. CodePointRange code_point_range { *code_point_range_start, data.code_point };
  555. assigned_code_points.append(code_point_range);
  556. data.name = data.name.substring(1, data.name.length() - 8);
  557. code_point_range_start.clear();
  558. add_canonical_code_point_name(code_point_range, data.name, unicode_data);
  559. } else {
  560. add_canonical_code_point_name({ data.code_point, data.code_point }, data.name, unicode_data);
  561. if ((data.code_point > 0) && (data.code_point - previous_code_point) != 1) {
  562. VERIFY(assigned_code_point_range_start.has_value());
  563. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  564. assigned_code_point_range_start = data.code_point;
  565. }
  566. }
  567. bool has_special_casing { false };
  568. for (auto const& casing : unicode_data.special_casing) {
  569. if (casing.code_point == data.code_point) {
  570. data.special_casing_indices.append(casing.index);
  571. has_special_casing = true;
  572. }
  573. }
  574. bool has_case_folding { false };
  575. for (size_t i = 0; i < unicode_data.case_folding.size(); ++i) {
  576. if (auto const& folding = unicode_data.case_folding[i]; folding.code_point == data.code_point) {
  577. data.case_folding_indices.append(i);
  578. has_case_folding = true;
  579. }
  580. }
  581. unicode_data.code_points_with_non_zero_combining_class += data.canonical_combining_class != 0;
  582. unicode_data.simple_uppercase_mapping_size += data.simple_uppercase_mapping.has_value();
  583. unicode_data.simple_lowercase_mapping_size += data.simple_lowercase_mapping.has_value();
  584. unicode_data.simple_titlecase_mapping_size += data.simple_titlecase_mapping.has_value();
  585. unicode_data.code_points_with_decomposition_mapping += data.decomposition_mapping.has_value();
  586. unicode_data.code_points_with_special_casing += has_special_casing;
  587. unicode_data.largest_special_casing_size = max(unicode_data.largest_special_casing_size, data.special_casing_indices.size());
  588. unicode_data.code_points_with_case_folding += has_case_folding;
  589. unicode_data.largest_case_folding_size = max(unicode_data.largest_case_folding_size, data.case_folding_indices.size());
  590. previous_code_point = data.code_point;
  591. unicode_data.code_point_data.append(move(data));
  592. }
  593. return {};
  594. }
  595. static ErrorOr<void> generate_unicode_data_header(Core::BufferedFile& file, UnicodeData& unicode_data)
  596. {
  597. StringBuilder builder;
  598. SourceGenerator generator { builder };
  599. generator.set("special_casing_mapping_size", DeprecatedString::number(unicode_data.largest_special_casing_mapping_size));
  600. generator.set("case_folding_mapping_size", DeprecatedString::number(unicode_data.largest_case_folding_mapping_size));
  601. auto generate_enum = [&](StringView name, StringView default_, auto values, Vector<Alias> aliases = {}) {
  602. quick_sort(values);
  603. quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; });
  604. generator.set("name", name);
  605. generator.set("underlying", DeprecatedString::formatted("{}UnderlyingType", name));
  606. generator.set("type", ((values.size() + !default_.is_empty()) < 256) ? "u8"sv : "u16"sv);
  607. generator.append(R"~~~(
  608. using @underlying@ = @type@;
  609. enum class @name@ : @underlying@ {)~~~");
  610. if (!default_.is_empty()) {
  611. generator.set("default", default_);
  612. generator.append(R"~~~(
  613. @default@,)~~~");
  614. }
  615. for (auto const& value : values) {
  616. generator.set("value", value);
  617. generator.append(R"~~~(
  618. @value@,)~~~");
  619. }
  620. for (auto const& alias : aliases) {
  621. generator.set("alias", alias.alias);
  622. generator.set("value", alias.name);
  623. generator.append(R"~~~(
  624. @alias@ = @value@,)~~~");
  625. }
  626. generator.append(R"~~~(
  627. };
  628. )~~~");
  629. };
  630. generator.append(R"~~~(
  631. #pragma once
  632. #include <AK/Types.h>
  633. #include <LibUnicode/Forward.h>
  634. namespace Unicode {
  635. )~~~");
  636. generate_enum("Locale"sv, "None"sv, unicode_data.locales);
  637. generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions));
  638. generate_enum("CaseFoldingStatus"sv, {}, move(unicode_data.statuses));
  639. generate_enum("GeneralCategory"sv, {}, unicode_data.general_categories.keys(), unicode_data.general_category_aliases);
  640. generate_enum("Property"sv, {}, unicode_data.prop_list.keys(), unicode_data.prop_aliases);
  641. generate_enum("Script"sv, {}, unicode_data.script_list.keys(), unicode_data.script_aliases);
  642. generate_enum("Block"sv, {}, unicode_data.block_list.keys(), unicode_data.block_aliases);
  643. generate_enum("GraphemeBreakProperty"sv, {}, unicode_data.grapheme_break_props.keys());
  644. generate_enum("WordBreakProperty"sv, {}, unicode_data.word_break_props.keys());
  645. generate_enum("SentenceBreakProperty"sv, {}, unicode_data.sentence_break_props.keys());
  646. generate_enum("CompatibilityFormattingTag"sv, "Canonical"sv, unicode_data.compatibility_tags);
  647. generator.append(R"~~~(
  648. struct SpecialCasing {
  649. u32 code_point { 0 };
  650. u32 lowercase_mapping[@special_casing_mapping_size@];
  651. u32 lowercase_mapping_size { 0 };
  652. u32 uppercase_mapping[@special_casing_mapping_size@];
  653. u32 uppercase_mapping_size { 0 };
  654. u32 titlecase_mapping[@special_casing_mapping_size@];
  655. u32 titlecase_mapping_size { 0 };
  656. Locale locale { Locale::None };
  657. Condition condition { Condition::None };
  658. };
  659. struct CaseFolding {
  660. u32 code_point { 0 };
  661. CaseFoldingStatus status { CaseFoldingStatus::Common };
  662. u32 mapping[@case_folding_mapping_size@];
  663. u32 mapping_size { 0 };
  664. };
  665. struct CodePointDecompositionRaw {
  666. u32 code_point { 0 };
  667. CompatibilityFormattingTag tag { CompatibilityFormattingTag::Canonical };
  668. size_t decomposition_index { 0 };
  669. size_t decomposition_count { 0 };
  670. };
  671. struct CodePointDecomposition {
  672. u32 code_point { 0 };
  673. CompatibilityFormattingTag tag { CompatibilityFormattingTag::Canonical };
  674. ReadonlySpan<u32> decomposition;
  675. };
  676. Optional<Locale> locale_from_string(StringView locale);
  677. ReadonlySpan<SpecialCasing const*> special_case_mapping(u32 code_point);
  678. ReadonlySpan<CaseFolding const*> case_folding_mapping(u32 code_point);
  679. }
  680. )~~~");
  681. TRY(file.write(generator.as_string_view().bytes()));
  682. return {};
  683. }
  684. static ErrorOr<void> generate_unicode_data_implementation(Core::BufferedFile& file, UnicodeData const& unicode_data)
  685. {
  686. StringBuilder builder;
  687. SourceGenerator generator { builder };
  688. generator.set("string_index_type"sv, unicode_data.unique_strings.type_that_fits());
  689. generator.set("largest_special_casing_size", DeprecatedString::number(unicode_data.largest_special_casing_size));
  690. generator.set("special_casing_size", DeprecatedString::number(unicode_data.special_casing.size()));
  691. generator.set("largest_case_folding_size", DeprecatedString::number(unicode_data.largest_case_folding_size));
  692. generator.set("case_folding_size", DeprecatedString::number(unicode_data.case_folding.size()));
  693. generator.append(R"~~~(
  694. #include <AK/Array.h>
  695. #include <AK/BinarySearch.h>
  696. #include <AK/CharacterTypes.h>
  697. #include <AK/Optional.h>
  698. #include <AK/Span.h>
  699. #include <AK/DeprecatedString.h>
  700. #include <AK/StringView.h>
  701. #include <LibUnicode/CharacterTypes.h>
  702. #include <LibUnicode/UnicodeData.h>
  703. #include <LibUnicode/Normalize.h>
  704. namespace Unicode {
  705. )~~~");
  706. unicode_data.unique_strings.generate(generator);
  707. auto append_list_and_size = [&](auto const& list, StringView format) {
  708. if (list.is_empty()) {
  709. generator.append(", {}, 0");
  710. return;
  711. }
  712. bool first = true;
  713. generator.append(", {");
  714. for (auto const& item : list) {
  715. generator.append(first ? " "sv : ", "sv);
  716. generator.append(DeprecatedString::formatted(format, item));
  717. first = false;
  718. }
  719. generator.append(DeprecatedString::formatted(" }}, {}", list.size()));
  720. };
  721. generator.append(R"~~~(
  722. static constexpr Array<SpecialCasing, @special_casing_size@> s_special_case { {)~~~");
  723. for (auto const& casing : unicode_data.special_casing) {
  724. generator.set("code_point", DeprecatedString::formatted("{:#x}", casing.code_point));
  725. generator.append(R"~~~(
  726. { @code_point@)~~~");
  727. constexpr auto format = "0x{:x}"sv;
  728. append_list_and_size(casing.lowercase_mapping, format);
  729. append_list_and_size(casing.uppercase_mapping, format);
  730. append_list_and_size(casing.titlecase_mapping, format);
  731. generator.set("locale", casing.locale.is_empty() ? "None" : casing.locale);
  732. generator.append(", Locale::@locale@");
  733. generator.set("condition", casing.condition.is_empty() ? "None" : casing.condition);
  734. generator.append(", Condition::@condition@");
  735. generator.append(" },");
  736. }
  737. generator.append(R"~~~(
  738. } };
  739. static constexpr Array<CaseFolding, @case_folding_size@> s_case_folding { {)~~~");
  740. for (auto const& folding : unicode_data.case_folding) {
  741. generator.set("code_point", DeprecatedString::formatted("{:#x}", folding.code_point));
  742. generator.set("status", folding.status);
  743. generator.append(R"~~~(
  744. { @code_point@, CaseFoldingStatus::@status@)~~~");
  745. append_list_and_size(folding.mapping, "0x{:x}"sv);
  746. generator.append(" },");
  747. }
  748. generator.append(R"~~~(
  749. } };
  750. struct CodePointMapping {
  751. u32 code_point { 0 };
  752. u32 mapping { 0 };
  753. };
  754. struct SpecialCaseMapping {
  755. u32 code_point { 0 };
  756. Array<SpecialCasing const*, @largest_special_casing_size@> special_casing {};
  757. u32 special_casing_size { 0 };
  758. };
  759. struct CaseFoldingMapping {
  760. u32 code_point { 0 };
  761. Array<CaseFolding const*, @largest_case_folding_size@> case_folding {};
  762. u32 case_folding_size { 0 };
  763. };
  764. struct CodePointAbbreviation {
  765. u32 code_point { 0 };
  766. @string_index_type@ abbreviation { 0 };
  767. };
  768. template<typename MappingType>
  769. struct CodePointComparator {
  770. constexpr int operator()(u32 code_point, MappingType const& mapping)
  771. {
  772. return code_point - mapping.code_point;
  773. }
  774. };
  775. struct CodePointRangeComparator {
  776. constexpr int operator()(u32 code_point, CodePointRange const& range)
  777. {
  778. return (code_point > range.last) - (code_point < range.first);
  779. }
  780. };
  781. struct BlockNameData {
  782. CodePointRange code_point_range {};
  783. @string_index_type@ display_name { 0 };
  784. };
  785. struct BlockNameComparator : public CodePointRangeComparator {
  786. constexpr int operator()(u32 code_point, BlockNameData const& name)
  787. {
  788. return CodePointRangeComparator::operator()(code_point, name.code_point_range);
  789. }
  790. };
  791. struct CodePointName {
  792. CodePointRange code_point_range {};
  793. @string_index_type@ display_name { 0 };
  794. };
  795. struct CodePointNameComparator : public CodePointRangeComparator {
  796. constexpr int operator()(u32 code_point, CodePointName const& name)
  797. {
  798. return CodePointRangeComparator::operator()(code_point, name.code_point_range);
  799. }
  800. };
  801. )~~~");
  802. generator.set("decomposition_mappings_size", DeprecatedString::number(unicode_data.decomposition_mappings.size()));
  803. generator.append("\nstatic constexpr Array<u32, @decomposition_mappings_size@> s_decomposition_mappings_data { ");
  804. generator.append(DeprecatedString::join(", "sv, unicode_data.decomposition_mappings, "{:#x}"sv));
  805. generator.append(" };\n");
  806. auto append_code_point_mappings = [&](StringView name, StringView mapping_type, u32 size, auto mapping_getter) {
  807. generator.set("name", name);
  808. generator.set("mapping_type", mapping_type);
  809. generator.set("size", DeprecatedString::number(size));
  810. generator.append(R"~~~(
  811. static constexpr Array<@mapping_type@, @size@> s_@name@_mappings { {
  812. )~~~");
  813. constexpr size_t max_mappings_per_row = 20;
  814. size_t mappings_in_current_row = 0;
  815. for (auto const& data : unicode_data.code_point_data) {
  816. auto mapping = mapping_getter(data);
  817. if constexpr (requires { mapping.has_value(); }) {
  818. if (!mapping.has_value())
  819. continue;
  820. } else {
  821. if (mapping.is_empty())
  822. continue;
  823. }
  824. if (mappings_in_current_row++ > 0)
  825. generator.append(" ");
  826. generator.set("code_point", DeprecatedString::formatted("{:#x}", data.code_point));
  827. generator.append("{ @code_point@");
  828. if constexpr (IsSame<decltype(mapping), Optional<u32>> || IsSame<decltype(mapping), Optional<size_t>>) {
  829. generator.set("mapping", DeprecatedString::formatted("{:#x}", *mapping));
  830. generator.append(", @mapping@ },");
  831. } else if constexpr (IsSame<decltype(mapping), Optional<CodePointDecomposition>>) {
  832. generator.set("tag", mapping->tag);
  833. generator.set("start", DeprecatedString::number(mapping->decomposition_index));
  834. generator.set("size", DeprecatedString::number(mapping->decomposition_size));
  835. generator.append(", CompatibilityFormattingTag::@tag@, @start@, @size@ },");
  836. } else {
  837. append_list_and_size(mapping, "&s_@name@[{}]"sv);
  838. generator.append(" },");
  839. }
  840. if (mappings_in_current_row == max_mappings_per_row) {
  841. mappings_in_current_row = 0;
  842. generator.append("\n ");
  843. }
  844. }
  845. generator.append(R"~~~(
  846. } };
  847. )~~~");
  848. };
  849. append_code_point_mappings("combining_class"sv, "CodePointMapping"sv, unicode_data.code_points_with_non_zero_combining_class,
  850. [](auto const& data) -> Optional<u32> {
  851. if (data.canonical_combining_class == 0)
  852. return {};
  853. return data.canonical_combining_class;
  854. });
  855. append_code_point_mappings("uppercase"sv, "CodePointMapping"sv, unicode_data.simple_uppercase_mapping_size, [](auto const& data) { return data.simple_uppercase_mapping; });
  856. append_code_point_mappings("lowercase"sv, "CodePointMapping"sv, unicode_data.simple_lowercase_mapping_size, [](auto const& data) { return data.simple_lowercase_mapping; });
  857. append_code_point_mappings("titlecase"sv, "CodePointMapping"sv, unicode_data.simple_titlecase_mapping_size, [](auto const& data) { return data.simple_titlecase_mapping; });
  858. append_code_point_mappings("special_case"sv, "SpecialCaseMapping"sv, unicode_data.code_points_with_special_casing, [](auto const& data) { return data.special_casing_indices; });
  859. append_code_point_mappings("case_folding"sv, "CaseFoldingMapping"sv, unicode_data.code_points_with_case_folding, [](auto const& data) { return data.case_folding_indices; });
  860. append_code_point_mappings("abbreviation"sv, "CodePointAbbreviation"sv, unicode_data.code_point_abbreviations.size(), [](auto const& data) { return data.abbreviation; });
  861. append_code_point_mappings("decomposition"sv, "CodePointDecompositionRaw"sv, unicode_data.code_points_with_decomposition_mapping,
  862. [](auto const& data) {
  863. return data.decomposition_mapping;
  864. });
  865. auto append_code_point_range_list = [&](DeprecatedString name, Vector<CodePointRange> const& ranges) {
  866. generator.set("name", name);
  867. generator.set("size", DeprecatedString::number(ranges.size()));
  868. generator.append(R"~~~(
  869. static constexpr Array<CodePointRange, @size@> @name@ { {
  870. )~~~");
  871. constexpr size_t max_ranges_per_row = 20;
  872. size_t ranges_in_current_row = 0;
  873. for (auto const& range : ranges) {
  874. if (ranges_in_current_row++ > 0)
  875. generator.append(" ");
  876. generator.set("first", DeprecatedString::formatted("{:#x}", range.first));
  877. generator.set("last", DeprecatedString::formatted("{:#x}", range.last));
  878. generator.append("{ @first@, @last@ },");
  879. if (ranges_in_current_row == max_ranges_per_row) {
  880. ranges_in_current_row = 0;
  881. generator.append("\n ");
  882. }
  883. }
  884. generator.append(R"~~~(
  885. } };
  886. )~~~");
  887. };
  888. auto append_prop_list = [&](StringView collection_name, StringView property_format, PropList const& property_list) {
  889. for (auto const& property : property_list) {
  890. auto name = DeprecatedString::formatted(property_format, property.key);
  891. append_code_point_range_list(move(name), property.value);
  892. }
  893. auto property_names = property_list.keys();
  894. quick_sort(property_names);
  895. generator.set("name", collection_name);
  896. generator.set("size", DeprecatedString::number(property_names.size()));
  897. generator.append(R"~~~(
  898. static constexpr Array<ReadonlySpan<CodePointRange>, @size@> @name@ { {)~~~");
  899. for (auto const& property_name : property_names) {
  900. generator.set("name", DeprecatedString::formatted(property_format, property_name));
  901. generator.append(R"~~~(
  902. @name@.span(),)~~~");
  903. }
  904. generator.append(R"~~~(
  905. } };
  906. )~~~");
  907. };
  908. append_prop_list("s_general_categories"sv, "s_general_category_{}"sv, unicode_data.general_categories);
  909. append_prop_list("s_properties"sv, "s_property_{}"sv, unicode_data.prop_list);
  910. append_prop_list("s_scripts"sv, "s_script_{}"sv, unicode_data.script_list);
  911. append_prop_list("s_script_extensions"sv, "s_script_extension_{}"sv, unicode_data.script_extensions);
  912. append_prop_list("s_blocks"sv, "s_block_{}"sv, unicode_data.block_list);
  913. append_prop_list("s_grapheme_break_properties"sv, "s_grapheme_break_property_{}"sv, unicode_data.grapheme_break_props);
  914. append_prop_list("s_word_break_properties"sv, "s_word_break_property_{}"sv, unicode_data.word_break_props);
  915. append_prop_list("s_sentence_break_properties"sv, "s_sentence_break_property_{}"sv, unicode_data.sentence_break_props);
  916. auto append_code_point_display_names = [&](StringView type, StringView name, auto const& display_names) {
  917. constexpr size_t max_values_per_row = 30;
  918. size_t values_in_current_row = 0;
  919. generator.set("type", type);
  920. generator.set("name", name);
  921. generator.set("size", DeprecatedString::number(display_names.size()));
  922. generator.append(R"~~~(
  923. static constexpr Array<@type@, @size@> @name@ { {
  924. )~~~");
  925. for (auto const& display_name : display_names) {
  926. if (values_in_current_row++ > 0)
  927. generator.append(", ");
  928. generator.set("first", DeprecatedString::formatted("{:#x}", display_name.code_point_range.first));
  929. generator.set("last", DeprecatedString::formatted("{:#x}", display_name.code_point_range.last));
  930. generator.set("name", DeprecatedString::number(display_name.name));
  931. generator.append("{ { @first@, @last@ }, @name@ }");
  932. if (values_in_current_row == max_values_per_row) {
  933. values_in_current_row = 0;
  934. generator.append(",\n ");
  935. }
  936. }
  937. generator.append(R"~~~(
  938. } };
  939. )~~~");
  940. };
  941. append_code_point_display_names("BlockNameData"sv, "s_block_display_names"sv, unicode_data.block_display_names);
  942. append_code_point_display_names("CodePointName"sv, "s_code_point_display_names"sv, unicode_data.code_point_display_names);
  943. generator.append(R"~~~(
  944. Optional<StringView> code_point_block_display_name(u32 code_point)
  945. {
  946. if (auto const* entry = binary_search(s_block_display_names, code_point, nullptr, BlockNameComparator {}))
  947. return decode_string(entry->display_name);
  948. return {};
  949. }
  950. ReadonlySpan<BlockName> block_display_names()
  951. {
  952. static auto display_names = []() {
  953. Array<BlockName, s_block_display_names.size()> display_names;
  954. for (size_t i = 0; i < s_block_display_names.size(); ++i) {
  955. auto const& display_name = s_block_display_names[i];
  956. display_names[i] = { display_name.code_point_range, decode_string(display_name.display_name) };
  957. }
  958. return display_names;
  959. }();
  960. return display_names.span();
  961. }
  962. Optional<DeprecatedString> code_point_display_name(u32 code_point)
  963. {
  964. if (auto const* entry = binary_search(s_code_point_display_names, code_point, nullptr, CodePointNameComparator {})) {
  965. auto display_name = decode_string(entry->display_name);
  966. if (display_name.ends_with("{:X}"sv))
  967. return DeprecatedString::formatted(display_name, code_point);
  968. return display_name;
  969. }
  970. return {};
  971. }
  972. )~~~");
  973. auto append_code_point_mapping_search = [&](StringView method, StringView mappings, StringView fallback) {
  974. generator.set("method", method);
  975. generator.set("mappings", mappings);
  976. generator.set("fallback", fallback);
  977. generator.append(R"~~~(
  978. u32 @method@(u32 code_point)
  979. {
  980. auto const* mapping = binary_search(@mappings@, code_point, nullptr, CodePointComparator<CodePointMapping> {});
  981. return mapping ? mapping->mapping : @fallback@;
  982. }
  983. )~~~");
  984. };
  985. append_code_point_mapping_search("canonical_combining_class"sv, "s_combining_class_mappings"sv, "0"sv);
  986. append_code_point_mapping_search("to_unicode_uppercase"sv, "s_uppercase_mappings"sv, "code_point"sv);
  987. append_code_point_mapping_search("to_unicode_lowercase"sv, "s_lowercase_mappings"sv, "code_point"sv);
  988. append_code_point_mapping_search("to_unicode_titlecase"sv, "s_titlecase_mappings"sv, "code_point"sv);
  989. generator.append(R"~~~(
  990. ReadonlySpan<SpecialCasing const*> special_case_mapping(u32 code_point)
  991. {
  992. auto const* mapping = binary_search(s_special_case_mappings, code_point, nullptr, CodePointComparator<SpecialCaseMapping> {});
  993. if (mapping == nullptr)
  994. return {};
  995. return mapping->special_casing.span().slice(0, mapping->special_casing_size);
  996. }
  997. ReadonlySpan<CaseFolding const*> case_folding_mapping(u32 code_point)
  998. {
  999. auto const* mapping = binary_search(s_case_folding_mappings, code_point, nullptr, CodePointComparator<CaseFoldingMapping> {});
  1000. if (mapping == nullptr)
  1001. return {};
  1002. return mapping->case_folding.span().slice(0, mapping->case_folding_size);
  1003. }
  1004. Optional<StringView> code_point_abbreviation(u32 code_point)
  1005. {
  1006. auto const* mapping = binary_search(s_abbreviation_mappings, code_point, nullptr, CodePointComparator<CodePointAbbreviation> {});
  1007. if (mapping == nullptr)
  1008. return {};
  1009. if (mapping->abbreviation == 0)
  1010. return {};
  1011. return decode_string(mapping->abbreviation);
  1012. }
  1013. Optional<CodePointDecomposition const> code_point_decomposition(u32 code_point)
  1014. {
  1015. auto const* mapping = binary_search(s_decomposition_mappings, code_point, nullptr, CodePointComparator<CodePointDecompositionRaw> {});
  1016. if (mapping == nullptr)
  1017. return {};
  1018. return CodePointDecomposition { mapping->code_point, mapping->tag, ReadonlySpan<u32> { s_decomposition_mappings_data.data() + mapping->decomposition_index, mapping->decomposition_count } };
  1019. }
  1020. Optional<CodePointDecomposition const> code_point_decomposition_by_index(size_t index)
  1021. {
  1022. if (index >= s_decomposition_mappings.size())
  1023. return {};
  1024. auto const& mapping = s_decomposition_mappings[index];
  1025. return CodePointDecomposition { mapping.code_point, mapping.tag, ReadonlySpan<u32> { s_decomposition_mappings_data.data() + mapping.decomposition_index, mapping.decomposition_count } };
  1026. }
  1027. )~~~");
  1028. auto append_prop_search = [&](StringView enum_title, StringView enum_snake, StringView collection_name) {
  1029. generator.set("enum_title", enum_title);
  1030. generator.set("enum_snake", enum_snake);
  1031. generator.set("collection_name", collection_name);
  1032. generator.append(R"~~~(
  1033. bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
  1034. {
  1035. auto index = static_cast<@enum_title@UnderlyingType>(@enum_snake@);
  1036. auto const& ranges = @collection_name@.at(index);
  1037. auto const* range = binary_search(ranges, code_point, nullptr, CodePointRangeComparator {});
  1038. return range != nullptr;
  1039. }
  1040. )~~~");
  1041. };
  1042. auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& prop_list, Vector<Alias> const& aliases) -> ErrorOr<void> {
  1043. HashValueMap<StringView> hashes;
  1044. TRY(hashes.try_ensure_capacity(prop_list.size() + aliases.size()));
  1045. ValueFromStringOptions options {};
  1046. for (auto const& prop : prop_list) {
  1047. if constexpr (IsSame<RemoveCVReference<decltype(prop)>, DeprecatedString>) {
  1048. hashes.set(CaseInsensitiveStringViewTraits::hash(prop), prop);
  1049. options.sensitivity = CaseSensitivity::CaseInsensitive;
  1050. } else {
  1051. hashes.set(prop.key.hash(), prop.key);
  1052. }
  1053. }
  1054. for (auto const& alias : aliases)
  1055. hashes.set(alias.alias.hash(), alias.alias);
  1056. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
  1057. return {};
  1058. };
  1059. TRY(append_from_string("Locale"sv, "locale"sv, unicode_data.locales, {}));
  1060. append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv);
  1061. TRY(append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases));
  1062. append_prop_search("Property"sv, "property"sv, "s_properties"sv);
  1063. TRY(append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases));
  1064. append_prop_search("Script"sv, "script"sv, "s_scripts"sv);
  1065. append_prop_search("Script"sv, "script_extension"sv, "s_script_extensions"sv);
  1066. TRY(append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases));
  1067. append_prop_search("Block"sv, "block"sv, "s_blocks"sv);
  1068. TRY(append_from_string("Block"sv, "block"sv, unicode_data.block_list, unicode_data.block_aliases));
  1069. append_prop_search("GraphemeBreakProperty"sv, "grapheme_break_property"sv, "s_grapheme_break_properties"sv);
  1070. append_prop_search("WordBreakProperty"sv, "word_break_property"sv, "s_word_break_properties"sv);
  1071. append_prop_search("SentenceBreakProperty"sv, "sentence_break_property"sv, "s_sentence_break_properties"sv);
  1072. generator.append(R"~~~(
  1073. }
  1074. )~~~");
  1075. TRY(file.write(generator.as_string_view().bytes()));
  1076. return {};
  1077. }
  1078. static Vector<u32> flatten_code_point_ranges(Vector<CodePointRange> const& code_points)
  1079. {
  1080. Vector<u32> flattened;
  1081. for (auto const& range : code_points) {
  1082. flattened.grow_capacity(range.last - range.first);
  1083. for (u32 code_point = range.first; code_point <= range.last; ++code_point)
  1084. flattened.append(code_point);
  1085. }
  1086. return flattened;
  1087. }
  1088. static Vector<CodePointRange> form_code_point_ranges(Vector<u32> code_points)
  1089. {
  1090. Vector<CodePointRange> ranges;
  1091. u32 range_start = code_points[0];
  1092. u32 range_end = range_start;
  1093. for (size_t i = 1; i < code_points.size(); ++i) {
  1094. u32 code_point = code_points[i];
  1095. if ((code_point - range_end) == 1) {
  1096. range_end = code_point;
  1097. } else {
  1098. ranges.append({ range_start, range_end });
  1099. range_start = code_point;
  1100. range_end = code_point;
  1101. }
  1102. }
  1103. ranges.append({ range_start, range_end });
  1104. return ranges;
  1105. }
  1106. static void sort_and_merge_code_point_ranges(Vector<CodePointRange>& code_points)
  1107. {
  1108. quick_sort(code_points, [](auto const& range1, auto const& range2) {
  1109. return range1.first < range2.first;
  1110. });
  1111. for (size_t i = 0; i < code_points.size() - 1;) {
  1112. if (code_points[i].last >= code_points[i + 1].first) {
  1113. code_points[i].last = max(code_points[i].last, code_points[i + 1].last);
  1114. code_points.remove(i + 1);
  1115. } else {
  1116. ++i;
  1117. }
  1118. }
  1119. auto all_code_points = flatten_code_point_ranges(code_points);
  1120. code_points = form_code_point_ranges(all_code_points);
  1121. }
  1122. static void populate_general_category_unions(PropList& general_categories)
  1123. {
  1124. // The Unicode standard defines General Category values which are not in any UCD file. These
  1125. // values are simply unions of other values.
  1126. // https://www.unicode.org/reports/tr44/#GC_Values_Table
  1127. auto populate_union = [&](auto alias, auto categories) {
  1128. auto& code_points = general_categories.ensure(alias);
  1129. for (auto const& category : categories)
  1130. code_points.extend(general_categories.find(category)->value);
  1131. sort_and_merge_code_point_ranges(code_points);
  1132. };
  1133. populate_union("LC"sv, Array { "Ll"sv, "Lu"sv, "Lt"sv });
  1134. populate_union("L"sv, Array { "Lu"sv, "Ll"sv, "Lt"sv, "Lm"sv, "Lo"sv });
  1135. populate_union("M"sv, Array { "Mn"sv, "Mc"sv, "Me"sv });
  1136. populate_union("N"sv, Array { "Nd"sv, "Nl"sv, "No"sv });
  1137. populate_union("P"sv, Array { "Pc"sv, "Pd"sv, "Ps"sv, "Pe"sv, "Pi"sv, "Pf"sv, "Po"sv });
  1138. populate_union("S"sv, Array { "Sm"sv, "Sc"sv, "Sk"sv, "So"sv });
  1139. populate_union("Z"sv, Array { "Zs"sv, "Zl"sv, "Zp"sv });
  1140. populate_union("C"sv, Array { "Cc"sv, "Cf"sv, "Cs"sv, "Co"sv, "Cn"sv });
  1141. }
  1142. static void normalize_script_extensions(PropList& script_extensions, PropList const& script_list, Vector<Alias> const& script_aliases)
  1143. {
  1144. // The ScriptExtensions UCD file lays out its code point ranges rather uniquely compared to
  1145. // other files. The Script listed on each line may either be a full Script string or an aliased
  1146. // abbreviation. Further, the extensions may or may not include the base Script list. Normalize
  1147. // the extensions here to be keyed by the full Script name and always include the base list.
  1148. auto extensions = move(script_extensions);
  1149. script_extensions = script_list;
  1150. for (auto const& extension : extensions) {
  1151. auto it = find_if(script_aliases.begin(), script_aliases.end(), [&](auto const& alias) { return extension.key == alias.alias; });
  1152. auto const& key = (it == script_aliases.end()) ? extension.key : it->name;
  1153. auto& code_points = script_extensions.find(key)->value;
  1154. code_points.extend(extension.value);
  1155. sort_and_merge_code_point_ranges(code_points);
  1156. }
  1157. // Lastly, the Common and Inherited script extensions are special. They must not contain any
  1158. // code points which appear in other script extensions. The ScriptExtensions UCD file does not
  1159. // list these extensions, therefore this peculiarity must be handled programmatically.
  1160. // https://www.unicode.org/reports/tr24/#Assignment_ScriptX_Values
  1161. auto code_point_has_other_extension = [&](StringView key, u32 code_point) {
  1162. for (auto const& extension : extensions) {
  1163. if (extension.key == key)
  1164. continue;
  1165. if (any_of(extension.value, [&](auto const& r) { return (r.first <= code_point) && (code_point <= r.last); }))
  1166. return true;
  1167. }
  1168. return false;
  1169. };
  1170. auto get_code_points_without_other_extensions = [&](StringView key) {
  1171. auto code_points = flatten_code_point_ranges(script_list.find(key)->value);
  1172. code_points.remove_all_matching([&](u32 c) { return code_point_has_other_extension(key, c); });
  1173. return code_points;
  1174. };
  1175. auto common_code_points = get_code_points_without_other_extensions("Common"sv);
  1176. script_extensions.set("Common"sv, form_code_point_ranges(common_code_points));
  1177. auto inherited_code_points = get_code_points_without_other_extensions("Inherited"sv);
  1178. script_extensions.set("Inherited"sv, form_code_point_ranges(inherited_code_points));
  1179. }
  1180. ErrorOr<int> serenity_main(Main::Arguments arguments)
  1181. {
  1182. StringView generated_header_path;
  1183. StringView generated_implementation_path;
  1184. StringView unicode_data_path;
  1185. StringView special_casing_path;
  1186. StringView case_folding_path;
  1187. StringView derived_general_category_path;
  1188. StringView prop_list_path;
  1189. StringView derived_core_prop_path;
  1190. StringView derived_binary_prop_path;
  1191. StringView prop_alias_path;
  1192. StringView prop_value_alias_path;
  1193. StringView name_alias_path;
  1194. StringView scripts_path;
  1195. StringView script_extensions_path;
  1196. StringView blocks_path;
  1197. StringView emoji_data_path;
  1198. StringView normalization_path;
  1199. StringView grapheme_break_path;
  1200. StringView word_break_path;
  1201. StringView sentence_break_path;
  1202. Core::ArgsParser args_parser;
  1203. args_parser.add_option(generated_header_path, "Path to the Unicode Data header file to generate", "generated-header-path", 'h', "generated-header-path");
  1204. args_parser.add_option(generated_implementation_path, "Path to the Unicode Data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  1205. args_parser.add_option(unicode_data_path, "Path to UnicodeData.txt file", "unicode-data-path", 'u', "unicode-data-path");
  1206. args_parser.add_option(special_casing_path, "Path to SpecialCasing.txt file", "special-casing-path", 's', "special-casing-path");
  1207. args_parser.add_option(case_folding_path, "Path to CaseFolding.txt file", "case-folding-path", 'o', "case-folding-path");
  1208. args_parser.add_option(derived_general_category_path, "Path to DerivedGeneralCategory.txt file", "derived-general-category-path", 'g', "derived-general-category-path");
  1209. args_parser.add_option(prop_list_path, "Path to PropList.txt file", "prop-list-path", 'p', "prop-list-path");
  1210. args_parser.add_option(derived_core_prop_path, "Path to DerivedCoreProperties.txt file", "derived-core-prop-path", 'd', "derived-core-prop-path");
  1211. args_parser.add_option(derived_binary_prop_path, "Path to DerivedBinaryProperties.txt file", "derived-binary-prop-path", 'b', "derived-binary-prop-path");
  1212. args_parser.add_option(prop_alias_path, "Path to PropertyAliases.txt file", "prop-alias-path", 'a', "prop-alias-path");
  1213. args_parser.add_option(prop_value_alias_path, "Path to PropertyValueAliases.txt file", "prop-value-alias-path", 'v', "prop-value-alias-path");
  1214. args_parser.add_option(name_alias_path, "Path to NameAliases.txt file", "name-alias-path", 'm', "name-alias-path");
  1215. args_parser.add_option(scripts_path, "Path to Scripts.txt file", "scripts-path", 'r', "scripts-path");
  1216. args_parser.add_option(script_extensions_path, "Path to ScriptExtensions.txt file", "script-extensions-path", 'x', "script-extensions-path");
  1217. args_parser.add_option(blocks_path, "Path to Blocks.txt file", "blocks-path", 'k', "blocks-path");
  1218. args_parser.add_option(emoji_data_path, "Path to emoji-data.txt file", "emoji-data-path", 'e', "emoji-data-path");
  1219. args_parser.add_option(normalization_path, "Path to DerivedNormalizationProps.txt file", "normalization-path", 'n', "normalization-path");
  1220. args_parser.add_option(grapheme_break_path, "Path to GraphemeBreakProperty.txt file", "grapheme-break-path", 'f', "grapheme-break-path");
  1221. args_parser.add_option(word_break_path, "Path to WordBreakProperty.txt file", "word-break-path", 'w', "word-break-path");
  1222. args_parser.add_option(sentence_break_path, "Path to SentenceBreakProperty.txt file", "sentence-break-path", 'i', "sentence-break-path");
  1223. args_parser.parse(arguments);
  1224. auto generated_header_file = TRY(open_file(generated_header_path, Core::File::OpenMode::Write));
  1225. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::File::OpenMode::Write));
  1226. auto unicode_data_file = TRY(open_file(unicode_data_path, Core::File::OpenMode::Read));
  1227. auto derived_general_category_file = TRY(open_file(derived_general_category_path, Core::File::OpenMode::Read));
  1228. auto special_casing_file = TRY(open_file(special_casing_path, Core::File::OpenMode::Read));
  1229. auto case_folding_file = TRY(open_file(case_folding_path, Core::File::OpenMode::Read));
  1230. auto prop_list_file = TRY(open_file(prop_list_path, Core::File::OpenMode::Read));
  1231. auto derived_core_prop_file = TRY(open_file(derived_core_prop_path, Core::File::OpenMode::Read));
  1232. auto derived_binary_prop_file = TRY(open_file(derived_binary_prop_path, Core::File::OpenMode::Read));
  1233. auto prop_alias_file = TRY(open_file(prop_alias_path, Core::File::OpenMode::Read));
  1234. auto prop_value_alias_file = TRY(open_file(prop_value_alias_path, Core::File::OpenMode::Read));
  1235. auto name_alias_file = TRY(open_file(name_alias_path, Core::File::OpenMode::Read));
  1236. auto scripts_file = TRY(open_file(scripts_path, Core::File::OpenMode::Read));
  1237. auto script_extensions_file = TRY(open_file(script_extensions_path, Core::File::OpenMode::Read));
  1238. auto blocks_file = TRY(open_file(blocks_path, Core::File::OpenMode::Read));
  1239. auto emoji_data_file = TRY(open_file(emoji_data_path, Core::File::OpenMode::Read));
  1240. auto normalization_file = TRY(open_file(normalization_path, Core::File::OpenMode::Read));
  1241. auto grapheme_break_file = TRY(open_file(grapheme_break_path, Core::File::OpenMode::Read));
  1242. auto word_break_file = TRY(open_file(word_break_path, Core::File::OpenMode::Read));
  1243. auto sentence_break_file = TRY(open_file(sentence_break_path, Core::File::OpenMode::Read));
  1244. UnicodeData unicode_data {};
  1245. TRY(parse_special_casing(*special_casing_file, unicode_data));
  1246. TRY(parse_case_folding(*case_folding_file, unicode_data));
  1247. TRY(parse_prop_list(*derived_general_category_file, unicode_data.general_categories));
  1248. TRY(parse_prop_list(*prop_list_file, unicode_data.prop_list));
  1249. TRY(parse_prop_list(*derived_core_prop_file, unicode_data.prop_list));
  1250. TRY(parse_prop_list(*derived_binary_prop_file, unicode_data.prop_list));
  1251. TRY(parse_prop_list(*emoji_data_file, unicode_data.prop_list));
  1252. TRY(parse_normalization_props(*normalization_file, unicode_data));
  1253. TRY(parse_alias_list(*prop_alias_file, unicode_data.prop_list, unicode_data.prop_aliases));
  1254. TRY(parse_prop_list(*scripts_file, unicode_data.script_list));
  1255. TRY(parse_prop_list(*script_extensions_file, unicode_data.script_extensions, true));
  1256. TRY(parse_block_display_names(*blocks_file, unicode_data));
  1257. TRY(parse_prop_list(*blocks_file, unicode_data.block_list, false, true));
  1258. TRY(parse_name_aliases(*name_alias_file, unicode_data));
  1259. TRY(parse_prop_list(*grapheme_break_file, unicode_data.grapheme_break_props));
  1260. TRY(parse_prop_list(*word_break_file, unicode_data.word_break_props));
  1261. TRY(parse_prop_list(*sentence_break_file, unicode_data.sentence_break_props));
  1262. populate_general_category_unions(unicode_data.general_categories);
  1263. TRY(parse_unicode_data(*unicode_data_file, unicode_data));
  1264. TRY(parse_value_alias_list(*prop_value_alias_file, "gc"sv, unicode_data.general_categories.keys(), unicode_data.general_category_aliases));
  1265. TRY(parse_value_alias_list(*prop_value_alias_file, "sc"sv, unicode_data.script_list.keys(), unicode_data.script_aliases, false));
  1266. TRY(parse_value_alias_list(*prop_value_alias_file, "blk"sv, unicode_data.block_list.keys(), unicode_data.block_aliases, false, true));
  1267. normalize_script_extensions(unicode_data.script_extensions, unicode_data.script_list, unicode_data.script_aliases);
  1268. TRY(generate_unicode_data_header(*generated_header_file, unicode_data));
  1269. TRY(generate_unicode_data_implementation(*generated_implementation_file, unicode_data));
  1270. return 0;
  1271. }