Move debug functions to Parser

This commit is contained in:
Aoran Zeng
2025-07-16 01:52:07 +08:00
parent c3657b44ef
commit a6b0338ee0
2 changed files with 57 additions and 58 deletions

View File

@@ -19,53 +19,7 @@ my $parser2 = Parser::Parser.new($test-file2);
$parser1.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);
say "\n\n";
print-parser-summary($parser2);
$parser1.debug-print-summary();
say "";
$parser2.debug-print-summary();