From f26817f43319326c8676958d20486783cd29e4e6 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Wed, 16 Jul 2025 20:37:19 +0800 Subject: [PATCH] Implement namespace --- tool/rawstr4c/lib/Config.rakumod | 26 +++++++++++++++++++++++--- tool/rawstr4c/lib/Generator.rakumod | 6 ++++++ tool/rawstr4c/lib/Parser.rakumod | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tool/rawstr4c/lib/Config.rakumod b/tool/rawstr4c/lib/Config.rakumod index 02fb92d..e3b3843 100644 --- a/tool/rawstr4c/lib/Config.rakumod +++ b/tool/rawstr4c/lib/Config.rakumod @@ -62,9 +62,7 @@ class SectionConfig { # ============================================================ # 返回当前 section 的 各种配置 - # 注意,这些函数仅仅忠实地要么返回层次化值,要么返回自己configblock的值,要么返回一个默认值 - # 从不考虑 section 其他部分对配置的影响 - # 对 section 其他部分的考虑是 Generator 的职责 + # 注意,这些函数全部都返回 ConfigValue's-Value 实例 #| RS4C-Mode method translate-mode() { @@ -111,6 +109,28 @@ class SectionConfig { return self.get-direct-config('name-literally', 'false'); } + + # RS4C-String + method namespace() { + my $config-namespace = self.get-direct-config('namespace', ''); + + my $current-namespace = $config-namespace.string-value; + + # 嵌套增加 + my $parent = $.section.parent; + while $parent { + if $parent.configblock.exist('namespace') { + $current-namespace = $parent.configblock.get('namespace').string-value ~ $current-namespace; + } else { + # 空字符串 + $current-namespace = '' ~ $current-namespace; + } + $parent = $parent.parent; + } + return Parser::ConfigItem's-Value.new($current-namespace); + } + + #| RS4C-Bool method debug() { return self.get-inherited-config('debug', 'false'); diff --git a/tool/rawstr4c/lib/Generator.rakumod b/tool/rawstr4c/lib/Generator.rakumod index c0efe3c..b157fd3 100644 --- a/tool/rawstr4c/lib/Generator.rakumod +++ b/tool/rawstr4c/lib/Generator.rakumod @@ -85,6 +85,7 @@ my class CVariableNameGenerator { $name = $config-name.string-value; } + my $namespace = $config.namespace.string-value; my $name-literally = $config.name-literally.bool-value; # 替换非法字符 @@ -102,8 +103,13 @@ my class CVariableNameGenerator { } else { # 否则,按照规则组装变量名 $varname ~= $prefix if $keep-prefix && $prefix; + + $varname ~= "_" if $varname && $namespace; + $varname ~= $namespace if $namespace; + $varname ~= "_" if $varname && $name; $varname ~= $name if $name; + $varname ~= "_" if $varname && $postfix && $keep-postfix; $varname ~= $postfix if $postfix && $keep-postfix; } diff --git a/tool/rawstr4c/lib/Parser.rakumod b/tool/rawstr4c/lib/Parser.rakumod index 1132799..ac2d239 100644 --- a/tool/rawstr4c/lib/Parser.rakumod +++ b/tool/rawstr4c/lib/Parser.rakumod @@ -99,6 +99,7 @@ class ConfigItem's-Value { #| 包含所有 config items 的容器 +#| 所有 get 出的值一定是 ConfigItem's-Value 实例 my class ConfigBlock { has %!items;