mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-20 23:26:47 +08:00
Move debug functions to Parser
This commit is contained in:
parent
c3657b44ef
commit
a6b0338ee0
@ -159,17 +159,15 @@ my class Section {
|
|||||||
class Parser {
|
class Parser {
|
||||||
has IO::Path $.input-file is rw;
|
has IO::Path $.input-file is rw;
|
||||||
#| 所有sections的扁平数组,已经是深度遍历的了
|
#| 所有sections的扁平数组,已经是深度遍历的了
|
||||||
has Section @.sections;
|
has Section @!sections;
|
||||||
|
|
||||||
method new($input-file) {
|
method new($input-file) {
|
||||||
self.bless(
|
self.bless(:$input-file);
|
||||||
:$input-file,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 获取根section(level 0)
|
# 获取根section(level 0)
|
||||||
method root-section() {
|
method root-section() {
|
||||||
return @.sections.first({ $_.level == 0 });
|
return @!sections.first({ $_.level == 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
# 配置项所在行 -> 解析为配置项
|
# 配置项所在行 -> 解析为配置项
|
||||||
@ -199,7 +197,7 @@ class Parser {
|
|||||||
# 都创建一个 root section (level 0)
|
# 都创建一个 root section (level 0)
|
||||||
my $root-config = Config.new();
|
my $root-config = Config.new();
|
||||||
$current-section = Section.new("", 0, $root-config);
|
$current-section = Section.new("", 0, $root-config);
|
||||||
@.sections.push: $current-section;
|
@!sections.push: $current-section;
|
||||||
|
|
||||||
# 开始遍历
|
# 开始遍历
|
||||||
my $line-count = 0;
|
my $line-count = 0;
|
||||||
@ -217,7 +215,7 @@ class Parser {
|
|||||||
# 准备创建一个新的 section
|
# 准备创建一个新的 section
|
||||||
$rawstr = "";
|
$rawstr = "";
|
||||||
my $new-section = Section.new($title, $level, Config.new());
|
my $new-section = Section.new($title, $level, Config.new());
|
||||||
@.sections.push: $new-section;
|
@!sections.push: $new-section;
|
||||||
|
|
||||||
# 找到合适的父节点
|
# 找到合适的父节点
|
||||||
my $parent = self.find-parent-section($level);
|
my $parent = self.find-parent-section($level);
|
||||||
@ -262,12 +260,59 @@ class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
method find-parent-section($new-level) {
|
method find-parent-section($new-level) {
|
||||||
# 从@.sections尾部向前找,找到第一个level小于new-level的section作为父节点
|
# 从@!sections尾部向前找,找到第一个level小于new-level的section作为父节点
|
||||||
for @.sections.reverse -> $section {
|
for @!sections.reverse -> $section {
|
||||||
if $section.level < $new-level {
|
if $section.level < $new-level {
|
||||||
return $section;
|
return $section;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Nil; # 没有找到父节点,说明是 root section
|
return Nil; # 没有找到父节点,说明是 root section
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 调试方法:扁平打印所有sections
|
||||||
|
method debug-print-sections-flatly() {
|
||||||
|
say "====== sections ======";
|
||||||
|
for @!sections.kv -> $i, $section {
|
||||||
|
my $title = $section.title || "(Root)";
|
||||||
|
my $has-config = $section.config.keys ?? "有配置" !! "无配置";
|
||||||
|
my $has-code = $section.codeblock ?? "有代码" !! "无代码";
|
||||||
|
say " [$i] Level {$section.level}: $title - $has-config, $has-code";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 调试方法:层级打印sections
|
||||||
|
method debug-print-sections-hierarchyly() {
|
||||||
|
say "====== hierarchy ======";
|
||||||
|
|
||||||
|
my $indent = 0;
|
||||||
|
|
||||||
|
# 嵌套的格式化函数
|
||||||
|
my sub format-section($section, $level) {
|
||||||
|
my $prefix = ' ' x $level;
|
||||||
|
my $title = $section.title // '(Root)';
|
||||||
|
return "{$prefix}- {$title} (level {$section.level})";
|
||||||
|
}
|
||||||
|
|
||||||
|
# 嵌套的递归打印函数
|
||||||
|
my sub print-section-tree($section, $level) {
|
||||||
|
say format-section($section, $level);
|
||||||
|
|
||||||
|
if $section.has-children {
|
||||||
|
for $section.children -> $child {
|
||||||
|
print-section-tree($child, $level + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my $root = self.root-section();
|
||||||
|
print-section-tree($root, $indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
# 调试方法:完整的调试信息打印
|
||||||
|
method debug-print-summary() {
|
||||||
|
self.debug-print-sections-flatly();
|
||||||
|
self.debug-print-sections-hierarchyly();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,53 +19,7 @@ my $parser2 = Parser::Parser.new($test-file2);
|
|||||||
$parser1.parse();
|
$parser1.parse();
|
||||||
$parser2.parse();
|
$parser2.parse();
|
||||||
|
|
||||||
|
|
||||||
sub print-sections-flatly($parser)
|
|
||||||
{
|
|
||||||
say "====== sections ======";
|
|
||||||
for $parser.sections.kv -> $i, $section {
|
|
||||||
my $title = $section.title || "(Root)";
|
|
||||||
my $has-config = $section.config.keys ?? "有配置" !! "无配置";
|
|
||||||
my $has-code = $section.codeblock ?? "有代码" !! "无代码";
|
|
||||||
say " [$i] Level {$section.level}: $title - $has-config, $has-code";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
sub print-sections-hierarchyly($parser) {
|
|
||||||
|
|
||||||
say "====== hierarchy ======";
|
|
||||||
|
|
||||||
my $indent = 0;
|
|
||||||
|
|
||||||
sub format-section($section, $level) {
|
|
||||||
my $prefix = ' ' x $level;
|
|
||||||
my $title = $section.title // '(Root)';
|
|
||||||
return "{$prefix}- {$title} (level {$section.level})";
|
|
||||||
}
|
|
||||||
|
|
||||||
sub print-section-tree($section, $level) {
|
|
||||||
say format-section($section, $level);
|
|
||||||
|
|
||||||
if $section.has-children {
|
|
||||||
for $section.children -> $child {
|
|
||||||
print-section-tree($child, $level + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
my $root = $parser.root-section();
|
|
||||||
print-section-tree($root, $indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
sub print-parser-summary($parser) {
|
|
||||||
print-sections-flatly($parser);
|
|
||||||
print-sections-hierarchyly($parser);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# 测试两个文件
|
# 测试两个文件
|
||||||
print-parser-summary($parser1);
|
$parser1.debug-print-summary();
|
||||||
say "\n\n";
|
say "";
|
||||||
print-parser-summary($parser2);
|
$parser2.debug-print-summary();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user