From bb738a8dbb6045d175ea186de418daf5138b677c Mon Sep 17 00:00:00 2001 From: CXO2 Date: Thu, 19 Sep 2024 11:43:09 +0900 Subject: [PATCH] Add multi-level namespace support in C++ generator --- Core/Generators/CPlusPlus/CPlusPlusGenerator.cs | 12 +++++++++--- Core/Meta/Extensions/StringExtensions.cs | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Core/Generators/CPlusPlus/CPlusPlusGenerator.cs b/Core/Generators/CPlusPlus/CPlusPlusGenerator.cs index c2f382a6..90879791 100644 --- a/Core/Generators/CPlusPlus/CPlusPlusGenerator.cs +++ b/Core/Generators/CPlusPlus/CPlusPlusGenerator.cs @@ -376,9 +376,15 @@ public override ValueTask Compile(BebopSchema schema, GeneratorConfig co builder.AppendLine("#include \"bebop.hpp\""); builder.AppendLine(""); + string[] namespaces = {}; if (!string.IsNullOrWhiteSpace(Config.Namespace)) { - builder.AppendLine($"namespace {Config.Namespace} {{"); + namespaces = Config.Namespace.Split("::"); + } + + foreach (string ns in namespaces) + { + builder.AppendLine($"namespace {ns} {{"); builder.AppendLine(""); } @@ -510,9 +516,9 @@ public override ValueTask Compile(BebopSchema schema, GeneratorConfig co } } - if (!string.IsNullOrWhiteSpace(Config.Namespace)) + foreach (string ns in namespaces) { - builder.AppendLine($"}} // namespace {Config.Namespace}"); + builder.AppendLine($"}} // namespace {ns}"); builder.AppendLine(""); } diff --git a/Core/Meta/Extensions/StringExtensions.cs b/Core/Meta/Extensions/StringExtensions.cs index 0d0a8238..b4d74af0 100644 --- a/Core/Meta/Extensions/StringExtensions.cs +++ b/Core/Meta/Extensions/StringExtensions.cs @@ -484,7 +484,7 @@ public static string EscapeString(this string? value) [GeneratedRegex(@"^.*[\*\?\[\]].*(\.[a-zA-Z0-9]+)?$")] private static partial Regex LegalFileGlobRegex(); - [GeneratedRegex(@"^[a-zA-Z]+(\.[a-zA-Z]+)*$")] + [GeneratedRegex(@"^[a-zA-Z]+(\.[a-zA-Z]+|::[a-zA-Z]+)*$")] private static partial Regex NamespaceRegex(); } }