From 04a4f6a2e88b663e952e99093e14a4ac6afdb7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 26 May 2023 12:41:54 +0200 Subject: [PATCH] GMLCompiler: Allow generating enum constants from strings As with other special cases, this depends on the property name. The UIDimension code is kept separate since it can handle both integers and strings, and the string names for special dimensions don't match with the enum names. --- .../Tools/CodeGenerators/GMLCompiler/main.cpp | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/GMLCompiler/main.cpp b/Meta/Lagom/Tools/CodeGenerators/GMLCompiler/main.cpp index 1577bd3e14..ce82b45ca7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/GMLCompiler/main.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/GMLCompiler/main.cpp @@ -154,12 +154,37 @@ static ErrorOr escape_string(JsonValue to_escape) return string; } +// This function assumes that the string is already the same as its enum constant's name. +// Therefore, it does not handle UI dimensions. +static ErrorOr> generate_enum_initializer_for(StringView property_name, JsonValue value) +{ + // The value is the enum's type name. + static HashMap enum_properties = { + { "text_alignment"sv, "Gfx::TextAlignment"sv }, + { "focus_policy"sv, "GUI::FocusPolicy"sv }, + { "foreground_role"sv, "Gfx::ColorRole"sv }, + { "text_wrapping"sv, "Gfx::TextWrapping"sv }, + }; + + auto const& enum_type_name = enum_properties.get(property_name); + if (!enum_type_name.has_value()) + return Optional {}; + + return String::formatted("{}::{}", *enum_type_name, value.as_string()); +} + // FIXME: In case of error, propagate the precise array+property that triggered the error. static ErrorOr generate_initializer_for(Optional property_name, JsonValue value) { if (value.is_string()) { - if (property_name.has_value() && takes_deprecated_string(*property_name)) - return String::formatted(R"~~~("{}"sv)~~~", TRY(escape_string(value))); + if (property_name.has_value()) { + if (takes_deprecated_string(*property_name)) + return String::formatted(R"~~~("{}"sv)~~~", TRY(escape_string(value))); + + if (auto const enum_value = TRY(generate_enum_initializer_for(*property_name, value)); enum_value.has_value()) + return String::formatted("{}", *enum_value); + } + return String::formatted(R"~~~("{}"_string)~~~", TRY(escape_string(value))); } // No need to handle the smaller integer types separately.