Implement namespace

This commit is contained in:
Aoran Zeng 2025-07-16 20:37:19 +08:00
parent a8c82a7476
commit f26817f433
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
3 changed files with 30 additions and 3 deletions

View File

@ -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');

View File

@ -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;
}

View File

@ -99,6 +99,7 @@ class ConfigItem's-Value {
#| config items
#| get ConfigItem's-Value
my class ConfigBlock {
has %!items;