/* * Copyright (c) 2021, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include "GeneratorUtil.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Some code points are excluded from UnicodeData.txt, and instead are part of a "range" of code // points, as indicated by the "name" field. For example: // 3400;;Lo;0;L;;;;;N;;;;; // 4DBF;;Lo;0;L;;;;;N;;;;; struct CodePointRange { u32 first; u32 last; }; // SpecialCasing source: https://www.unicode.org/Public/13.0.0/ucd/SpecialCasing.txt // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#SpecialCasing.txt struct SpecialCasing { u32 index { 0 }; u32 code_point { 0 }; Vector lowercase_mapping; Vector uppercase_mapping; Vector titlecase_mapping; String locale; String condition; }; // PropList source: https://www.unicode.org/Public/13.0.0/ucd/PropList.txt // Property descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#PropList.txt using PropList = HashMap>; // PropertyAliases source: https://www.unicode.org/Public/13.0.0/ucd/PropertyAliases.txt struct Alias { String property; String alias; }; // Normalization source: https://www.unicode.org/Public/13.0.0/ucd/DerivedNormalizationProps.txt // Normalization descriptions: https://www.unicode.org/reports/tr44/#DerivedNormalizationProps.txt enum class QuickCheck { Yes, No, Maybe, }; struct Normalization { CodePointRange code_point_range; Vector value; QuickCheck quick_check { QuickCheck::Yes }; }; using NormalizationProps = HashMap>; // UnicodeData source: https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#UnicodeData.txt // https://www.unicode.org/reports/tr44/#General_Category_Values struct CodePointData { u32 code_point { 0 }; String name; u8 canonical_combining_class { 0 }; String bidi_class; String decomposition_type; Optional numeric_value_decimal; Optional numeric_value_digit; Optional numeric_value_numeric; bool bidi_mirrored { false }; String unicode_1_name; String iso_comment; Optional simple_uppercase_mapping; Optional simple_lowercase_mapping; Optional simple_titlecase_mapping; Vector special_casing_indices; }; struct UnicodeData { u32 code_points_with_non_zero_combining_class { 0 }; u32 simple_uppercase_mapping_size { 0 }; u32 simple_lowercase_mapping_size { 0 }; Vector special_casing; u32 code_points_with_special_casing { 0 }; u32 largest_casing_transform_size { 0 }; u32 largest_special_casing_size { 0 }; Vector conditions; Vector code_point_data; Vector code_point_ranges; PropList general_categories; Vector general_category_aliases; // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in // any UCD file. Assigned code point ranges are derived as this generator is executed. // https://unicode.org/reports/tr18/#General_Category_Property PropList prop_list { { "Any"sv, { { 0, 0x10ffff } } }, { "Assigned"sv, {} }, { "ASCII"sv, { { 0, 0x7f } } }, }; Vector prop_aliases; PropList script_list { { "Unknown"sv, {} }, }; Vector script_aliases; PropList script_extensions; // FIXME: We are not yet doing anything with this data. It will be needed for String.prototype.normalize. NormalizationProps normalization_props; }; static Vector parse_code_point_list(StringView const& list) { Vector code_points; auto segments = list.split_view(' '); for (auto const& code_point : segments) code_points.append(AK::StringUtils::convert_to_uint_from_hex(code_point).value()); return code_points; } static CodePointRange parse_code_point_range(StringView const& list) { CodePointRange code_point_range {}; if (list.contains(".."sv)) { auto segments = list.split_view(".."sv); VERIFY(segments.size() == 2); auto begin = AK::StringUtils::convert_to_uint_from_hex(segments[0]).value(); auto end = AK::StringUtils::convert_to_uint_from_hex(segments[1]).value(); code_point_range = { begin, end }; } else { auto code_point = AK::StringUtils::convert_to_uint_from_hex(list).value(); code_point_range = { code_point, code_point }; } return code_point_range; } static void parse_special_casing(Core::File& file, UnicodeData& unicode_data) { while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty() || line.starts_with('#')) continue; if (auto index = line.find('#'); index.has_value()) line = line.substring(0, *index); auto segments = line.split(';', true); VERIFY(segments.size() == 5 || segments.size() == 6); SpecialCasing casing {}; casing.code_point = AK::StringUtils::convert_to_uint_from_hex(segments[0]).value(); casing.lowercase_mapping = parse_code_point_list(segments[1]); casing.titlecase_mapping = parse_code_point_list(segments[2]); casing.uppercase_mapping = parse_code_point_list(segments[3]); if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) { auto conditions = condition.split(' ', true); VERIFY(conditions.size() == 1 || conditions.size() == 2); if (conditions.size() == 2) { casing.locale = move(conditions[0]); casing.condition = move(conditions[1]); } else if (all_of(conditions[0], is_ascii_lower_alpha)) { casing.locale = move(conditions[0]); } else { casing.condition = move(conditions[0]); } if (!casing.locale.is_empty()) casing.locale = String::formatted("{:c}{}", to_ascii_uppercase(casing.locale[0]), casing.locale.substring_view(1)); casing.condition = casing.condition.replace("_", "", true); if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition)) unicode_data.conditions.append(casing.condition); } unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.lowercase_mapping.size()); unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.titlecase_mapping.size()); unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.uppercase_mapping.size()); unicode_data.special_casing.append(move(casing)); } quick_sort(unicode_data.special_casing, [](auto const& lhs, auto const& rhs) { if (lhs.code_point != rhs.code_point) return lhs.code_point < rhs.code_point; if (lhs.locale.is_empty() && !rhs.locale.is_empty()) return false; if (!lhs.locale.is_empty() && rhs.locale.is_empty()) return true; return lhs.locale < rhs.locale; }); for (u32 i = 0; i < unicode_data.special_casing.size(); ++i) unicode_data.special_casing[i].index = i; } static void parse_prop_list(Core::File& file, PropList& prop_list, bool multi_value_property = false) { while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty() || line.starts_with('#')) continue; if (auto index = line.find('#'); index.has_value()) line = line.substring(0, *index); auto segments = line.split_view(';', true); VERIFY(segments.size() == 2); auto code_point_range = parse_code_point_range(segments[0].trim_whitespace()); Vector properties; if (multi_value_property) properties = segments[1].trim_whitespace().split_view(' '); else properties = { segments[1].trim_whitespace() }; for (auto const& property : properties) { auto& code_points = prop_list.ensure(property.trim_whitespace()); code_points.append(code_point_range); } } } static void parse_alias_list(Core::File& file, PropList const& prop_list, Vector& prop_aliases) { String current_property; auto append_alias = [&](auto alias, auto property) { // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip. if (alias == property) return; // FIXME: We will, eventually, need to find where missing properties are located and parse them. if (!prop_list.contains(property)) return; prop_aliases.append({ property, alias }); }; while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty() || line.starts_with('#')) { if (line.ends_with("Properties"sv)) current_property = line.substring(2); continue; } // Note: For now, we only care about Binary Property aliases for Unicode property escapes. if (current_property != "Binary Properties"sv) continue; auto segments = line.split_view(';', true); VERIFY((segments.size() == 2) || (segments.size() == 3)); auto alias = segments[0].trim_whitespace(); auto property = segments[1].trim_whitespace(); append_alias(alias, property); if (segments.size() == 3) { alias = segments[2].trim_whitespace(); append_alias(alias, property); } } } static void parse_value_alias_list(Core::File& file, StringView desired_category, Vector const& value_list, Vector& prop_aliases, bool primary_value_is_first = true) { VERIFY(file.seek(0)); auto append_alias = [&](auto alias, auto value) { // Note: The value alias file contains lines such as "Ahom = Ahom", which we should just skip. if (alias == value) return; // FIXME: We will, eventually, need to find where missing properties are located and parse them. if (!value_list.contains_slow(value)) return; prop_aliases.append({ value, alias }); }; while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty() || line.starts_with('#')) continue; if (auto index = line.find('#'); index.has_value()) line = line.substring(0, *index); auto segments = line.split_view(';', true); auto category = segments[0].trim_whitespace(); if (category != desired_category) continue; VERIFY((segments.size() == 3) || (segments.size() == 4)); auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace(); auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace(); append_alias(alias, value); if (segments.size() == 4) { alias = segments[3].trim_whitespace(); append_alias(alias, value); } } } static void parse_normalization_props(Core::File& file, UnicodeData& unicode_data) { while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty() || line.starts_with('#')) continue; if (auto index = line.find('#'); index.has_value()) line = line.substring(0, *index); auto segments = line.split_view(';', true); VERIFY((segments.size() == 2) || (segments.size() == 3)); auto code_point_range = parse_code_point_range(segments[0].trim_whitespace()); auto property = segments[1].trim_whitespace().to_string(); Vector value; QuickCheck quick_check = QuickCheck::Yes; if (segments.size() == 3) { auto value_or_quick_check = segments[2].trim_whitespace(); if ((value_or_quick_check == "N"sv)) quick_check = QuickCheck::No; else if ((value_or_quick_check == "M"sv)) quick_check = QuickCheck::Maybe; else value = parse_code_point_list(value_or_quick_check); } auto& normalizations = unicode_data.normalization_props.ensure(property); normalizations.append({ code_point_range, move(value), quick_check }); auto& prop_list = unicode_data.prop_list.ensure(property); prop_list.append(move(code_point_range)); } } static void parse_unicode_data(Core::File& file, UnicodeData& unicode_data) { Optional code_point_range_start; auto& assigned_code_points = unicode_data.prop_list.find("Assigned"sv)->value; Optional assigned_code_point_range_start = 0; u32 previous_code_point = 0; while (file.can_read_line()) { auto line = file.read_line(); if (line.is_empty()) continue; auto segments = line.split(';', true); VERIFY(segments.size() == 15); CodePointData data {}; data.code_point = AK::StringUtils::convert_to_uint_from_hex(segments[0]).value(); data.name = move(segments[1]); data.canonical_combining_class = AK::StringUtils::convert_to_uint(segments[3]).value(); data.bidi_class = move(segments[4]); data.decomposition_type = move(segments[5]); data.numeric_value_decimal = AK::StringUtils::convert_to_int(segments[6]); data.numeric_value_digit = AK::StringUtils::convert_to_int(segments[7]); data.numeric_value_numeric = AK::StringUtils::convert_to_int(segments[8]); data.bidi_mirrored = segments[9] == "Y"sv; data.unicode_1_name = move(segments[10]); data.iso_comment = move(segments[11]); data.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex(segments[12]); data.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex(segments[13]); data.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex(segments[14]); if (!assigned_code_point_range_start.has_value()) assigned_code_point_range_start = data.code_point; if (data.name.starts_with("<"sv) && data.name.ends_with(", First>")) { VERIFY(!code_point_range_start.has_value() && assigned_code_point_range_start.has_value()); code_point_range_start = data.code_point; data.name = data.name.substring(1, data.name.length() - 9); assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point }); assigned_code_point_range_start.clear(); } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>")) { VERIFY(code_point_range_start.has_value()); CodePointRange code_point_range { *code_point_range_start, data.code_point }; unicode_data.code_point_ranges.append(code_point_range); assigned_code_points.append(code_point_range); data.name = data.name.substring(1, data.name.length() - 8); code_point_range_start.clear(); } else if ((data.code_point > 0) && (data.code_point - previous_code_point) != 1) { VERIFY(assigned_code_point_range_start.has_value()); assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point }); assigned_code_point_range_start = data.code_point; } bool has_special_casing { false }; for (auto const& casing : unicode_data.special_casing) { if (casing.code_point == data.code_point) { data.special_casing_indices.append(casing.index); has_special_casing = true; } } unicode_data.code_points_with_non_zero_combining_class += data.canonical_combining_class != 0; unicode_data.simple_uppercase_mapping_size += data.simple_uppercase_mapping.has_value(); unicode_data.simple_lowercase_mapping_size += data.simple_lowercase_mapping.has_value(); unicode_data.code_points_with_special_casing += has_special_casing; unicode_data.largest_special_casing_size = max(unicode_data.largest_special_casing_size, data.special_casing_indices.size()); previous_code_point = data.code_point; unicode_data.code_point_data.append(move(data)); } } static void generate_unicode_data_header(Core::File& file, UnicodeData& unicode_data) { StringBuilder builder; SourceGenerator generator { builder }; generator.set("casing_transform_size", String::number(unicode_data.largest_casing_transform_size)); auto generate_enum = [&](StringView name, StringView default_, Vector values, Vector aliases = {}) { quick_sort(values); quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; }); generator.set("name", name); generator.set("underlying", String::formatted("{}UnderlyingType", name)); generator.append(R"~~~( using @underlying@ = u8; enum class @name@ : @underlying@ {)~~~"); if (!default_.is_empty()) { generator.set("default", default_); generator.append(R"~~~( @default@,)~~~"); } for (auto const& value : values) { generator.set("value", value); generator.append(R"~~~( @value@,)~~~"); } for (auto const& alias : aliases) { generator.set("alias", alias.alias); generator.set("value", alias.property); generator.append(R"~~~( @alias@ = @value@,)~~~"); } generator.append(R"~~~( }; )~~~"); }; generator.append(R"~~~( #pragma once #include #include #include #include #include namespace Unicode { )~~~"); generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions)); generate_enum("GeneralCategory"sv, {}, unicode_data.general_categories.keys(), unicode_data.general_category_aliases); generate_enum("Property"sv, {}, unicode_data.prop_list.keys(), unicode_data.prop_aliases); generate_enum("Script"sv, {}, unicode_data.script_list.keys(), unicode_data.script_aliases); generator.append(R"~~~( struct SpecialCasing { u32 code_point { 0 }; u32 lowercase_mapping[@casing_transform_size@]; u32 lowercase_mapping_size { 0 }; u32 uppercase_mapping[@casing_transform_size@]; u32 uppercase_mapping_size { 0 }; u32 titlecase_mapping[@casing_transform_size@]; u32 titlecase_mapping_size { 0 }; Locale locale { Locale::None }; Condition condition { Condition::None }; }; namespace Detail { u32 canonical_combining_class(u32 code_point); u32 simple_uppercase_mapping(u32 code_point); u32 simple_lowercase_mapping(u32 code_point); Span special_case_mapping(u32 code_point); bool code_point_has_general_category(u32 code_point, GeneralCategory general_category); Optional general_category_from_string(StringView const& general_category); bool code_point_has_property(u32 code_point, Property property); Optional property_from_string(StringView const& property); bool code_point_has_script(u32 code_point, Script script); bool code_point_has_script_extension(u32 code_point, Script script); Optional