GenerateUnicodeData.cpp 31 KB

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