From 27eda77c9736361f15407c29e5258f635b1cd8d5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 26 Jan 2022 08:56:09 -0500 Subject: LibUnicode: Create a nearly empty generator for relative-time formatting This sets up the generator plumbing to create the relative-time data files. This data could probably be included in the date-time generator, but that generator is large enough that I'd rather put this tangentially related data in its own file. --- .../Tools/CodeGenerators/LibUnicode/CMakeLists.txt | 1 + .../GenerateUnicodeRelativeTimeFormat.cpp | 133 +++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeRelativeTimeFormat.cpp (limited to 'Meta/Lagom') diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/CMakeLists.txt index d71891a277..f699285269 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/CMakeLists.txt +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/CMakeLists.txt @@ -2,3 +2,4 @@ lagom_tool(GenerateUnicodeData SOURCES GenerateUnicodeData.cpp LIBS LagomMain) lagom_tool(GenerateUnicodeDateTimeFormat SOURCES GenerateUnicodeDateTimeFormat.cpp LIBS LagomMain LagomTimeZone) lagom_tool(GenerateUnicodeLocale SOURCES GenerateUnicodeLocale.cpp LIBS LagomMain) lagom_tool(GenerateUnicodeNumberFormat SOURCES GenerateUnicodeNumberFormat.cpp LIBS LagomMain) +lagom_tool(GenerateUnicodeRelativeTimeFormat SOURCES GenerateUnicodeRelativeTimeFormat.cpp LIBS LagomMain) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeRelativeTimeFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeRelativeTimeFormat.cpp new file mode 100644 index 0000000000..3df4a84bda --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeRelativeTimeFormat.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "GeneratorUtil.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using StringIndexType = u16; +constexpr auto s_string_index_type = "u16"sv; + +struct Locale { +}; + +struct UnicodeLocaleData { + UniqueStringStorage unique_strings; + HashMap locales; +}; + +static ErrorOr parse_all_locales(String dates_path, UnicodeLocaleData& locale_data) +{ + auto dates_iterator = TRY(path_to_dir_iterator(move(dates_path))); + + auto remove_variants_from_path = [&](String path) -> ErrorOr { + auto parsed_locale = TRY(CanonicalLanguageID::parse(locale_data.unique_strings, LexicalPath::basename(path))); + + StringBuilder builder; + builder.append(locale_data.unique_strings.get(parsed_locale.language)); + if (auto script = locale_data.unique_strings.get(parsed_locale.script); !script.is_empty()) + builder.appendff("-{}", script); + if (auto region = locale_data.unique_strings.get(parsed_locale.region); !region.is_empty()) + builder.appendff("-{}", region); + + return builder.build(); + }; + + while (dates_iterator.has_next()) { + auto dates_path = TRY(next_path_from_dir_iterator(dates_iterator)); + auto language = TRY(remove_variants_from_path(dates_path)); + + [[maybe_unused]] auto& locale = locale_data.locales.ensure(language); + } + + return {}; +} + +static void generate_unicode_locale_header(Core::File& file, UnicodeLocaleData&) +{ + StringBuilder builder; + SourceGenerator generator { builder }; + + generator.append(R"~~~( +#pragma once + +#include + +namespace Unicode { +)~~~"); + + generator.append(R"~~~( +} +)~~~"); + + VERIFY(file.write(generator.as_string_view())); +} + +static void generate_unicode_locale_implementation(Core::File& file, UnicodeLocaleData& locale_data) +{ + StringBuilder builder; + SourceGenerator generator { builder }; + generator.set("string_index_type"sv, s_string_index_type); + + generator.append(R"~~~( +#include +#include +#include + +namespace Unicode { +)~~~"); + + locale_data.unique_strings.generate(generator); + + generator.append(R"~~~( +} +)~~~"); + + VERIFY(file.write(generator.as_string_view())); +} + +ErrorOr serenity_main(Main::Arguments arguments) +{ + StringView generated_header_path; + StringView generated_implementation_path; + StringView dates_path; + + Core::ArgsParser args_parser; + args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path"); + args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path"); + args_parser.add_option(dates_path, "Path to cldr-dates directory", "dates-path", 'd', "dates-path"); + args_parser.parse(arguments); + + auto open_file = [&](StringView path) -> ErrorOr> { + if (path.is_empty()) { + args_parser.print_usage(stderr, arguments.argv[0]); + return Error::from_string_literal("Must provide all command line options"sv); + } + + return Core::File::open(path, Core::OpenMode::ReadWrite); + }; + + auto generated_header_file = TRY(open_file(generated_header_path)); + auto generated_implementation_file = TRY(open_file(generated_implementation_path)); + + UnicodeLocaleData locale_data; + TRY(parse_all_locales(dates_path, locale_data)); + + generate_unicode_locale_header(generated_header_file, locale_data); + generate_unicode_locale_implementation(generated_implementation_file, locale_data); + + return 0; +} -- cgit v1.2.3