GenerateUnicodeData.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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/HashMap.h>
  10. #include <AK/Optional.h>
  11. #include <AK/QuickSort.h>
  12. #include <AK/SourceGenerator.h>
  13. #include <AK/String.h>
  14. #include <AK/StringUtils.h>
  15. #include <AK/Types.h>
  16. #include <AK/Vector.h>
  17. #include <LibCore/ArgsParser.h>
  18. #include <LibCore/File.h>
  19. // Some code points are excluded from UnicodeData.txt, and instead are part of a "range" of code
  20. // points, as indicated by the "name" field. For example:
  21. // 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;
  22. // 4DBF;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;;
  23. struct CodePointRange {
  24. u32 first;
  25. u32 last;
  26. };
  27. // SpecialCasing source: https://www.unicode.org/Public/13.0.0/ucd/SpecialCasing.txt
  28. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#SpecialCasing.txt
  29. struct SpecialCasing {
  30. u32 index { 0 };
  31. u32 code_point { 0 };
  32. Vector<u32> lowercase_mapping;
  33. Vector<u32> uppercase_mapping;
  34. Vector<u32> titlecase_mapping;
  35. String locale;
  36. String condition;
  37. };
  38. // PropList source: https://www.unicode.org/Public/13.0.0/ucd/PropList.txt
  39. // Property descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#PropList.txt
  40. // https://www.unicode.org/reports/tr44/tr44-13.html#WordBreakProperty.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. // UnicodeData source: https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt
  48. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#UnicodeData.txt
  49. // https://www.unicode.org/reports/tr44/#General_Category_Values
  50. struct CodePointData {
  51. u32 code_point { 0 };
  52. String name;
  53. String general_category;
  54. u8 canonical_combining_class { 0 };
  55. String bidi_class;
  56. String decomposition_type;
  57. Optional<i8> numeric_value_decimal;
  58. Optional<i8> numeric_value_digit;
  59. Optional<i8> numeric_value_numeric;
  60. bool bidi_mirrored { false };
  61. String unicode_1_name;
  62. String iso_comment;
  63. Optional<u32> simple_uppercase_mapping;
  64. Optional<u32> simple_lowercase_mapping;
  65. Optional<u32> simple_titlecase_mapping;
  66. Vector<u32> special_casing_indices;
  67. Vector<StringView> prop_list;
  68. StringView word_break_property;
  69. };
  70. struct UnicodeData {
  71. Vector<SpecialCasing> special_casing;
  72. u32 largest_casing_transform_size { 0 };
  73. u32 largest_special_casing_size { 0 };
  74. Vector<String> locales;
  75. Vector<String> conditions;
  76. Vector<CodePointData> code_point_data;
  77. Vector<CodePointRange> code_point_ranges;
  78. u32 last_contiguous_code_point { 0 };
  79. // The Unicode standard defines General Category values which are not in any UCD file. These
  80. // values are simply unions of other values.
  81. // https://www.unicode.org/reports/tr44/#GC_Values_Table
  82. Vector<String> general_categories;
  83. Vector<Alias> general_category_unions {
  84. { "Ll | Lu | Lt"sv, "LC"sv },
  85. { "Lu | Ll | Lt | Lm | Lo"sv, "L"sv },
  86. { "Mn | Mc | Me"sv, "M"sv },
  87. { "Nd | Nl | No"sv, "N"sv },
  88. { "Pc | Pd | Ps | Pe | Pi | Pf | Po"sv, "P"sv },
  89. { "Sm | Sc | Sk | So"sv, "S"sv },
  90. { "Zs | Zl | Zp"sv, "Z"sv },
  91. { "Cc | Cf | Cs | Co"sv, "C"sv }, // FIXME: This union should also contain "Cn" (Unassigned), which we don't parse yet.
  92. };
  93. Vector<Alias> general_category_aliases;
  94. // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in
  95. // any UCD file. Assigned is set as the default enum value 0 so "property & Assigned == Assigned"
  96. // is always true. Any is not assigned code points here because this file only parses assigned
  97. // code points, whereas Any will include unassigned code points.
  98. // https://unicode.org/reports/tr18/#General_Category_Property
  99. PropList prop_list {
  100. { "Any"sv, {} },
  101. { "ASCII"sv, { { 0, 0x7f } } },
  102. };
  103. Vector<Alias> prop_aliases;
  104. PropList word_break_prop_list;
  105. };
  106. static constexpr auto s_desired_fields = Array {
  107. "general_category"sv,
  108. "simple_uppercase_mapping"sv,
  109. "simple_lowercase_mapping"sv,
  110. };
  111. static void write_to_file_if_different(Core::File& file, StringView contents)
  112. {
  113. auto const current_contents = file.read_all();
  114. if (StringView { current_contents.bytes() } == contents)
  115. return;
  116. VERIFY(file.seek(0));
  117. VERIFY(file.truncate(0));
  118. VERIFY(file.write(contents));
  119. }
  120. static void parse_special_casing(Core::File& file, UnicodeData& unicode_data)
  121. {
  122. auto parse_code_point_list = [&](auto const& line) {
  123. Vector<u32> code_points;
  124. auto segments = line.split(' ');
  125. for (auto const& code_point : segments)
  126. code_points.append(AK::StringUtils::convert_to_uint_from_hex<u32>(code_point).value());
  127. return code_points;
  128. };
  129. while (file.can_read_line()) {
  130. auto line = file.read_line();
  131. if (line.is_empty() || line.starts_with('#'))
  132. continue;
  133. if (auto index = line.find('#'); index.has_value())
  134. line = line.substring(0, *index);
  135. auto segments = line.split(';', true);
  136. VERIFY(segments.size() == 5 || segments.size() == 6);
  137. SpecialCasing casing {};
  138. casing.index = static_cast<u32>(unicode_data.special_casing.size());
  139. casing.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  140. casing.lowercase_mapping = parse_code_point_list(segments[1]);
  141. casing.titlecase_mapping = parse_code_point_list(segments[2]);
  142. casing.uppercase_mapping = parse_code_point_list(segments[3]);
  143. if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) {
  144. auto conditions = condition.split(' ', true);
  145. VERIFY(conditions.size() == 1 || conditions.size() == 2);
  146. if (conditions.size() == 2) {
  147. casing.locale = move(conditions[0]);
  148. casing.condition = move(conditions[1]);
  149. } else if (all_of(conditions[0], is_ascii_lower_alpha)) {
  150. casing.locale = move(conditions[0]);
  151. } else {
  152. casing.condition = move(conditions[0]);
  153. }
  154. casing.locale = casing.locale.to_uppercase();
  155. casing.condition.replace("_", "", true);
  156. if (!casing.locale.is_empty() && !unicode_data.locales.contains_slow(casing.locale))
  157. unicode_data.locales.append(casing.locale);
  158. if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition))
  159. unicode_data.conditions.append(casing.condition);
  160. }
  161. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.lowercase_mapping.size());
  162. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.titlecase_mapping.size());
  163. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.uppercase_mapping.size());
  164. unicode_data.special_casing.append(move(casing));
  165. }
  166. }
  167. static void parse_prop_list(Core::File& file, PropList& prop_list)
  168. {
  169. while (file.can_read_line()) {
  170. auto line = file.read_line();
  171. if (line.is_empty() || line.starts_with('#'))
  172. continue;
  173. if (auto index = line.find('#'); index.has_value())
  174. line = line.substring(0, *index);
  175. auto segments = line.split_view(';', true);
  176. VERIFY(segments.size() == 2);
  177. auto code_point_range = segments[0].trim_whitespace();
  178. auto property = segments[1].trim_whitespace();
  179. auto& code_points = prop_list.ensure(property);
  180. if (code_point_range.contains(".."sv)) {
  181. segments = code_point_range.split_view(".."sv);
  182. VERIFY(segments.size() == 2);
  183. auto begin = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  184. auto end = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[1]).value();
  185. code_points.append({ begin, end });
  186. } else {
  187. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(code_point_range).value();
  188. code_points.append({ code_point, code_point });
  189. }
  190. }
  191. }
  192. static void parse_alias_list(Core::File& file, PropList const& prop_list, Vector<Alias>& prop_aliases)
  193. {
  194. String current_property;
  195. auto append_alias = [&](auto alias, auto property) {
  196. // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip.
  197. if (alias == property)
  198. return;
  199. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  200. if (!prop_list.contains(property))
  201. return;
  202. prop_aliases.append({ property, alias });
  203. };
  204. while (file.can_read_line()) {
  205. auto line = file.read_line();
  206. if (line.is_empty() || line.starts_with('#')) {
  207. if (line.ends_with("Properties"sv))
  208. current_property = line.substring(2);
  209. continue;
  210. }
  211. // Note: For now, we only care about Binary Property aliases for Unicode property escapes.
  212. if (current_property != "Binary Properties"sv)
  213. continue;
  214. auto segments = line.split_view(';', true);
  215. VERIFY((segments.size() == 2) || (segments.size() == 3));
  216. auto alias = segments[0].trim_whitespace();
  217. auto property = segments[1].trim_whitespace();
  218. append_alias(alias, property);
  219. if (segments.size() == 3) {
  220. alias = segments[2].trim_whitespace();
  221. append_alias(alias, property);
  222. }
  223. }
  224. }
  225. static void parse_value_alias_list(Core::File& file, StringView desired_category, Vector<String> const& value_list, Vector<Alias>& prop_unions, Vector<Alias>& prop_aliases)
  226. {
  227. auto append_alias = [&](auto alias, auto value) {
  228. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  229. if (!value_list.contains_slow(value) && !any_of(prop_unions, [&](auto const& u) { return value == u.alias; }))
  230. return;
  231. prop_aliases.append({ value, alias });
  232. };
  233. while (file.can_read_line()) {
  234. auto line = file.read_line();
  235. if (line.is_empty() || line.starts_with('#'))
  236. continue;
  237. if (auto index = line.find('#'); index.has_value())
  238. line = line.substring(0, *index);
  239. auto segments = line.split_view(';', true);
  240. auto category = segments[0].trim_whitespace();
  241. if (category != desired_category)
  242. continue;
  243. VERIFY((segments.size() == 3) || (segments.size() == 4));
  244. auto value = segments[1].trim_whitespace();
  245. auto alias = segments[2].trim_whitespace();
  246. append_alias(alias, value);
  247. if (segments.size() == 4) {
  248. alias = segments[3].trim_whitespace();
  249. append_alias(alias, value);
  250. }
  251. }
  252. }
  253. static void parse_unicode_data(Core::File& file, UnicodeData& unicode_data)
  254. {
  255. Optional<u32> code_point_range_start;
  256. while (file.can_read_line()) {
  257. auto line = file.read_line();
  258. if (line.is_empty())
  259. continue;
  260. auto segments = line.split(';', true);
  261. VERIFY(segments.size() == 15);
  262. CodePointData data {};
  263. data.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  264. data.name = move(segments[1]);
  265. data.general_category = move(segments[2]);
  266. data.canonical_combining_class = AK::StringUtils::convert_to_uint<u8>(segments[3]).value();
  267. data.bidi_class = move(segments[4]);
  268. data.decomposition_type = move(segments[5]);
  269. data.numeric_value_decimal = AK::StringUtils::convert_to_int<i8>(segments[6]);
  270. data.numeric_value_digit = AK::StringUtils::convert_to_int<i8>(segments[7]);
  271. data.numeric_value_numeric = AK::StringUtils::convert_to_int<i8>(segments[8]);
  272. data.bidi_mirrored = segments[9] == "Y"sv;
  273. data.unicode_1_name = move(segments[10]);
  274. data.iso_comment = move(segments[11]);
  275. data.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[12]);
  276. data.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[13]);
  277. data.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[14]);
  278. if (data.name.starts_with("<"sv) && data.name.ends_with(", First>")) {
  279. VERIFY(!code_point_range_start.has_value());
  280. code_point_range_start = data.code_point;
  281. data.name = data.name.substring(1, data.name.length() - 9);
  282. } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>")) {
  283. VERIFY(code_point_range_start.has_value());
  284. unicode_data.code_point_ranges.append({ *code_point_range_start, data.code_point });
  285. data.name = data.name.substring(1, data.name.length() - 8);
  286. code_point_range_start.clear();
  287. }
  288. for (auto const& casing : unicode_data.special_casing) {
  289. if (casing.code_point == data.code_point)
  290. data.special_casing_indices.append(casing.index);
  291. }
  292. for (auto const& property : unicode_data.prop_list) {
  293. for (auto const& range : property.value) {
  294. if ((range.first <= data.code_point) && (data.code_point <= range.last)) {
  295. data.prop_list.append(property.key);
  296. break;
  297. }
  298. }
  299. }
  300. if (data.prop_list.is_empty())
  301. data.prop_list.append("Assigned"sv);
  302. for (auto const& property : unicode_data.word_break_prop_list) {
  303. for (auto const& range : property.value) {
  304. if ((range.first <= data.code_point) && (data.code_point <= range.last)) {
  305. data.word_break_property = property.key;
  306. break;
  307. }
  308. }
  309. if (!data.word_break_property.is_empty())
  310. break;
  311. }
  312. if (data.word_break_property.is_empty())
  313. data.word_break_property = "Other"sv;
  314. unicode_data.largest_special_casing_size = max(unicode_data.largest_special_casing_size, data.special_casing_indices.size());
  315. if (!unicode_data.general_categories.contains_slow(data.general_category))
  316. unicode_data.general_categories.append(data.general_category);
  317. unicode_data.code_point_data.append(move(data));
  318. }
  319. }
  320. static void generate_unicode_data_header(Core::File& file, UnicodeData& unicode_data)
  321. {
  322. StringBuilder builder;
  323. SourceGenerator generator { builder };
  324. generator.set("casing_transform_size", String::number(unicode_data.largest_casing_transform_size));
  325. generator.set("special_casing_size", String::number(unicode_data.largest_special_casing_size));
  326. auto generate_enum = [&](StringView name, StringView default_, Vector<String> values, Vector<Alias> unions = {}, Vector<Alias> aliases = {}, bool as_bitmask = false) {
  327. VERIFY((values.size() + !default_.is_empty()) <= 64);
  328. quick_sort(values);
  329. quick_sort(unions, [](auto& union1, auto& union2) { return union1.alias < union2.alias; });
  330. quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; });
  331. generator.set("name", name);
  332. generator.set("underlying", String::formatted("{}UnderlyingType", name));
  333. if (as_bitmask) {
  334. generator.append(R"~~~(
  335. using @underlying@ = u64;
  336. enum class @name@ : @underlying@ {)~~~");
  337. } else {
  338. generator.append(R"~~~(
  339. enum class @name@ {)~~~");
  340. }
  341. if (!default_.is_empty()) {
  342. generator.set("default", default_);
  343. generator.append(R"~~~(
  344. @default@,)~~~");
  345. }
  346. u8 index = 0;
  347. for (auto const& value : values) {
  348. generator.set("value", value);
  349. if (as_bitmask) {
  350. generator.set("index", String::number(index++));
  351. generator.append(R"~~~(
  352. @value@ = static_cast<@underlying@>(1) << @index@,)~~~");
  353. } else {
  354. generator.append(R"~~~(
  355. @value@,)~~~");
  356. }
  357. }
  358. for (auto const& union_ : unions) {
  359. generator.set("union", union_.alias);
  360. generator.set("value", union_.property);
  361. generator.append(R"~~~(
  362. @union@ = @value@,)~~~");
  363. }
  364. for (auto const& alias : aliases) {
  365. generator.set("alias", alias.alias);
  366. generator.set("value", alias.property);
  367. generator.append(R"~~~(
  368. @alias@ = @value@,)~~~");
  369. }
  370. generator.append(R"~~~(
  371. };
  372. )~~~");
  373. if (as_bitmask) {
  374. generator.append(R"~~~(
  375. constexpr @name@ operator&(@name@ value1, @name@ value2)
  376. {
  377. return static_cast<@name@>(static_cast<@underlying@>(value1) & static_cast<@underlying@>(value2));
  378. }
  379. constexpr @name@ operator|(@name@ value1, @name@ value2)
  380. {
  381. return static_cast<@name@>(static_cast<@underlying@>(value1) | static_cast<@underlying@>(value2));
  382. }
  383. )~~~");
  384. }
  385. };
  386. generator.append(R"~~~(
  387. #pragma once
  388. #include <AK/Optional.h>
  389. #include <AK/Types.h>
  390. #include <LibUnicode/Forward.h>
  391. namespace Unicode {
  392. )~~~");
  393. generate_enum("Locale"sv, "None"sv, move(unicode_data.locales));
  394. generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions));
  395. generate_enum("GeneralCategory"sv, "None"sv, unicode_data.general_categories, unicode_data.general_category_unions, unicode_data.general_category_aliases, true);
  396. generate_enum("Property"sv, "Assigned"sv, unicode_data.prop_list.keys(), {}, unicode_data.prop_aliases, true);
  397. generate_enum("WordBreakProperty"sv, "Other"sv, unicode_data.word_break_prop_list.keys());
  398. generator.append(R"~~~(
  399. struct SpecialCasing {
  400. u32 code_point { 0 };
  401. u32 lowercase_mapping[@casing_transform_size@];
  402. u32 lowercase_mapping_size { 0 };
  403. u32 uppercase_mapping[@casing_transform_size@];
  404. u32 uppercase_mapping_size { 0 };
  405. u32 titlecase_mapping[@casing_transform_size@];
  406. u32 titlecase_mapping_size { 0 };
  407. Locale locale { Locale::None };
  408. Condition condition { Condition::None };
  409. };
  410. struct UnicodeData {
  411. u32 code_point;)~~~");
  412. auto append_field = [&](StringView type, StringView name) {
  413. if (!s_desired_fields.span().contains_slow(name))
  414. return;
  415. generator.set("type", type);
  416. generator.set("name", name);
  417. generator.append(R"~~~(
  418. @type@ @name@;)~~~");
  419. };
  420. // Note: For compile-time performance, only primitive types are used.
  421. append_field("char const*"sv, "name"sv);
  422. append_field("GeneralCategory"sv, "general_category"sv);
  423. append_field("u8"sv, "canonical_combining_class"sv);
  424. append_field("char const*"sv, "bidi_class"sv);
  425. append_field("char const*"sv, "decomposition_type"sv);
  426. append_field("i8"sv, "numeric_value_decimal"sv);
  427. append_field("i8"sv, "numeric_value_digit"sv);
  428. append_field("i8"sv, "numeric_value_numeric"sv);
  429. append_field("bool"sv, "bidi_mirrored"sv);
  430. append_field("char const*"sv, "unicode_1_name"sv);
  431. append_field("char const*"sv, "iso_comment"sv);
  432. append_field("u32"sv, "simple_uppercase_mapping"sv);
  433. append_field("u32"sv, "simple_lowercase_mapping"sv);
  434. append_field("u32"sv, "simple_titlecase_mapping"sv);
  435. generator.append(R"~~~(
  436. SpecialCasing const* special_casing[@special_casing_size@] {};
  437. u32 special_casing_size { 0 };
  438. Property properties { Property::Assigned };
  439. WordBreakProperty word_break_property { WordBreakProperty::Other };
  440. };
  441. namespace Detail {
  442. Optional<UnicodeData> unicode_data_for_code_point(u32 code_point);
  443. Optional<Property> property_from_string(StringView const& property);
  444. Optional<GeneralCategory> general_category_from_string(StringView const& general_category);
  445. }
  446. }
  447. )~~~");
  448. write_to_file_if_different(file, generator.as_string_view());
  449. }
  450. static void generate_unicode_data_implementation(Core::File& file, UnicodeData const& unicode_data)
  451. {
  452. StringBuilder builder;
  453. SourceGenerator generator { builder };
  454. generator.set("special_casing_size", String::number(unicode_data.special_casing.size()));
  455. generator.set("code_point_data_size", String::number(unicode_data.code_point_data.size()));
  456. generator.append(R"~~~(
  457. #include <AK/Array.h>
  458. #include <AK/CharacterTypes.h>
  459. #include <AK/HashMap.h>
  460. #include <AK/StringView.h>
  461. #include <LibUnicode/UnicodeData.h>
  462. namespace Unicode {
  463. )~~~");
  464. auto append_list_and_size = [&](auto const& list, StringView format) {
  465. if (list.is_empty()) {
  466. generator.append(", {}, 0");
  467. return;
  468. }
  469. bool first = true;
  470. generator.append(", {");
  471. for (auto const& item : list) {
  472. generator.append(first ? " " : ", ");
  473. generator.append(String::formatted(format, item));
  474. first = false;
  475. }
  476. generator.append(String::formatted(" }}, {}", list.size()));
  477. };
  478. generator.append(R"~~~(
  479. static constexpr Array<SpecialCasing, @special_casing_size@> s_special_casing { {)~~~");
  480. for (auto const& casing : unicode_data.special_casing) {
  481. generator.set("code_point", String::formatted("{:#x}", casing.code_point));
  482. generator.append(R"~~~(
  483. { @code_point@)~~~");
  484. constexpr auto format = "0x{:x}"sv;
  485. append_list_and_size(casing.lowercase_mapping, format);
  486. append_list_and_size(casing.uppercase_mapping, format);
  487. append_list_and_size(casing.titlecase_mapping, format);
  488. generator.set("locale", casing.locale.is_empty() ? "None" : casing.locale);
  489. generator.append(", Locale::@locale@");
  490. generator.set("condition", casing.condition.is_empty() ? "None" : casing.condition);
  491. generator.append(", Condition::@condition@");
  492. generator.append(" },");
  493. }
  494. generator.append(R"~~~(
  495. } };
  496. static constexpr Array<UnicodeData, @code_point_data_size@> s_unicode_data { {)~~~");
  497. auto append_field = [&](StringView name, String value) {
  498. if (!s_desired_fields.span().contains_slow(name))
  499. return;
  500. generator.set("value", move(value));
  501. generator.append(", @value@");
  502. };
  503. for (auto const& data : unicode_data.code_point_data) {
  504. generator.set("code_point", String::formatted("{:#x}", data.code_point));
  505. generator.append(R"~~~(
  506. { @code_point@)~~~");
  507. append_field("name", String::formatted("\"{}\"", data.name));
  508. append_field("general_category", String::formatted("GeneralCategory::{}", data.general_category));
  509. append_field("canonical_combining_class", String::number(data.canonical_combining_class));
  510. append_field("bidi_class", String::formatted("\"{}\"", data.bidi_class));
  511. append_field("decomposition_type", String::formatted("\"{}\"", data.decomposition_type));
  512. append_field("numeric_value_decimal", String::number(data.numeric_value_decimal.value_or(-1)));
  513. append_field("numeric_value_digit", String::number(data.numeric_value_digit.value_or(-1)));
  514. append_field("numeric_value_numeric", String::number(data.numeric_value_numeric.value_or(-1)));
  515. append_field("bidi_mirrored", String::formatted("{}", data.bidi_mirrored));
  516. append_field("unicode_1_name", String::formatted("\"{}\"", data.unicode_1_name));
  517. append_field("iso_comment", String::formatted("\"{}\"", data.iso_comment));
  518. append_field("simple_uppercase_mapping", String::formatted("{:#x}", data.simple_uppercase_mapping.value_or(data.code_point)));
  519. append_field("simple_lowercase_mapping", String::formatted("{:#x}", data.simple_lowercase_mapping.value_or(data.code_point)));
  520. append_field("simple_titlecase_mapping", String::formatted("{:#x}", data.simple_titlecase_mapping.value_or(data.code_point)));
  521. append_list_and_size(data.special_casing_indices, "&s_special_casing[{}]"sv);
  522. bool first = true;
  523. for (auto const& property : data.prop_list) {
  524. generator.append(first ? ", " : " | ");
  525. generator.append(String::formatted("Property::{}", property));
  526. first = false;
  527. }
  528. generator.append(String::formatted(", WordBreakProperty::{}", data.word_break_property));
  529. generator.append(" },");
  530. }
  531. generator.append(R"~~~(
  532. } };
  533. static HashMap<u32, UnicodeData const*> const& ensure_code_point_map()
  534. {
  535. static HashMap<u32, UnicodeData const*> code_point_to_data_map;
  536. code_point_to_data_map.ensure_capacity(s_unicode_data.size());
  537. for (auto const& unicode_data : s_unicode_data)
  538. code_point_to_data_map.set(unicode_data.code_point, &unicode_data);
  539. return code_point_to_data_map;
  540. }
  541. static Optional<u32> index_of_code_point_in_range(u32 code_point)
  542. {)~~~");
  543. for (auto const& range : unicode_data.code_point_ranges) {
  544. generator.set("first", String::formatted("{:#x}", range.first));
  545. generator.set("last", String::formatted("{:#x}", range.last));
  546. generator.append(R"~~~(
  547. if ((code_point > @first@) && (code_point < @last@))
  548. return @first@;)~~~");
  549. }
  550. generator.append(R"~~~(
  551. return {};
  552. }
  553. namespace Detail {
  554. Optional<UnicodeData> unicode_data_for_code_point(u32 code_point)
  555. {
  556. static auto const& code_point_to_data_map = ensure_code_point_map();
  557. VERIFY(is_unicode(code_point));
  558. if (auto data = code_point_to_data_map.get(code_point); data.has_value())
  559. return *(data.value());
  560. if (auto index = index_of_code_point_in_range(code_point); index.has_value()) {
  561. auto data_for_range = *(code_point_to_data_map.get(*index).value());
  562. data_for_range.simple_uppercase_mapping = code_point;
  563. data_for_range.simple_lowercase_mapping = code_point;
  564. return data_for_range;
  565. }
  566. return {};
  567. }
  568. Optional<Property> property_from_string(StringView const& property)
  569. {
  570. if (property == "Assigned"sv)
  571. return Property::Assigned;)~~~");
  572. for (auto const& property : unicode_data.prop_list) {
  573. generator.set("property", property.key);
  574. generator.append(R"~~~(
  575. if (property == "@property@"sv)
  576. return Property::@property@;)~~~");
  577. }
  578. for (auto const& alias : unicode_data.prop_aliases) {
  579. generator.set("property", alias.alias);
  580. generator.append(R"~~~(
  581. if (property == "@property@"sv)
  582. return Property::@property@;)~~~");
  583. }
  584. generator.append(R"~~~(
  585. return {};
  586. }
  587. Optional<GeneralCategory> general_category_from_string(StringView const& general_category)
  588. {)~~~");
  589. for (auto const& general_category : unicode_data.general_categories) {
  590. generator.set("general_category", general_category);
  591. generator.append(R"~~~(
  592. if (general_category == "@general_category@"sv)
  593. return GeneralCategory::@general_category@;)~~~");
  594. }
  595. for (auto const& union_ : unicode_data.general_category_unions) {
  596. generator.set("general_category", union_.alias);
  597. generator.append(R"~~~(
  598. if (general_category == "@general_category@"sv)
  599. return GeneralCategory::@general_category@;)~~~");
  600. }
  601. for (auto const& alias : unicode_data.general_category_aliases) {
  602. generator.set("general_category", alias.alias);
  603. generator.append(R"~~~(
  604. if (general_category == "@general_category@"sv)
  605. return GeneralCategory::@general_category@;)~~~");
  606. }
  607. generator.append(R"~~~(
  608. return {};
  609. }
  610. }
  611. }
  612. )~~~");
  613. write_to_file_if_different(file, generator.as_string_view());
  614. }
  615. int main(int argc, char** argv)
  616. {
  617. char const* generated_header_path = nullptr;
  618. char const* generated_implementation_path = nullptr;
  619. char const* unicode_data_path = nullptr;
  620. char const* special_casing_path = nullptr;
  621. char const* prop_list_path = nullptr;
  622. char const* derived_core_prop_path = nullptr;
  623. char const* prop_alias_path = nullptr;
  624. char const* prop_value_alias_path = nullptr;
  625. char const* word_break_path = nullptr;
  626. Core::ArgsParser args_parser;
  627. args_parser.add_option(generated_header_path, "Path to the Unicode Data header file to generate", "generated-header-path", 'h', "generated-header-path");
  628. args_parser.add_option(generated_implementation_path, "Path to the Unicode Data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  629. args_parser.add_option(unicode_data_path, "Path to UnicodeData.txt file", "unicode-data-path", 'u', "unicode-data-path");
  630. args_parser.add_option(special_casing_path, "Path to SpecialCasing.txt file", "special-casing-path", 's', "special-casing-path");
  631. args_parser.add_option(prop_list_path, "Path to PropList.txt file", "prop-list-path", 'p', "prop-list-path");
  632. args_parser.add_option(derived_core_prop_path, "Path to DerivedCoreProperties.txt file", "derived-core-prop-path", 'd', "derived-core-prop-path");
  633. args_parser.add_option(prop_alias_path, "Path to PropertyAliases.txt file", "prop-alias-path", 'a', "prop-alias-path");
  634. args_parser.add_option(prop_value_alias_path, "Path to PropertyValueAliases.txt file", "prop-value-alias-path", 'v', "prop-value-alias-path");
  635. args_parser.add_option(word_break_path, "Path to WordBreakProperty.txt file", "word-break-path", 'w', "word-break-path");
  636. args_parser.parse(argc, argv);
  637. auto open_file = [&](StringView path, StringView flags, Core::OpenMode mode = Core::OpenMode::ReadOnly) {
  638. if (path.is_empty()) {
  639. warnln("{} is required", flags);
  640. args_parser.print_usage(stderr, argv[0]);
  641. exit(1);
  642. }
  643. auto file_or_error = Core::File::open(path, mode);
  644. if (file_or_error.is_error()) {
  645. warnln("Failed to open {}: {}", path, file_or_error.release_error());
  646. exit(1);
  647. }
  648. return file_or_error.release_value();
  649. };
  650. auto generated_header_file = open_file(generated_header_path, "-h/--generated-header-path", Core::OpenMode::ReadWrite);
  651. auto generated_implementation_file = open_file(generated_implementation_path, "-c/--generated-implementation-path", Core::OpenMode::ReadWrite);
  652. auto unicode_data_file = open_file(unicode_data_path, "-u/--unicode-data-path");
  653. auto special_casing_file = open_file(special_casing_path, "-s/--special-casing-path");
  654. auto prop_list_file = open_file(prop_list_path, "-p/--prop-list-path");
  655. auto derived_core_prop_file = open_file(derived_core_prop_path, "-d/--derived-core-prop-path");
  656. auto prop_alias_file = open_file(prop_alias_path, "-a/--prop-alias-path");
  657. auto prop_value_alias_file = open_file(prop_value_alias_path, "-v/--prop-value-alias-path");
  658. auto word_break_file = open_file(word_break_path, "-w/--word-break-path");
  659. UnicodeData unicode_data {};
  660. parse_special_casing(special_casing_file, unicode_data);
  661. parse_prop_list(prop_list_file, unicode_data.prop_list);
  662. parse_prop_list(derived_core_prop_file, unicode_data.prop_list);
  663. parse_alias_list(prop_alias_file, unicode_data.prop_list, unicode_data.prop_aliases);
  664. parse_prop_list(word_break_file, unicode_data.word_break_prop_list);
  665. parse_unicode_data(unicode_data_file, unicode_data);
  666. parse_value_alias_list(prop_value_alias_file, "gc"sv, unicode_data.general_categories, unicode_data.general_category_unions, unicode_data.general_category_aliases);
  667. generate_unicode_data_header(generated_header_file, unicode_data);
  668. generate_unicode_data_implementation(generated_implementation_file, unicode_data);
  669. return 0;
  670. }