Test parser hierarchy

This commit is contained in:
Aoran Zeng 2025-07-16 00:10:45 +08:00
parent efd98676b8
commit 676089ee7a
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
4 changed files with 187 additions and 104 deletions

View File

@ -0,0 +1,74 @@
- global-prefix = `GLOBAL_`
- global-output = `:terminal`
```c
int global_code_block = 1;
```
some comments
# Java
- java-prefix = `JAVA_`
## Maven Config
- maven-name = `maven`
```xml
<settings>
<mirrors>
</mirrors>
</settings>
```
## Gradle Config
```groovy
repositories {
maven { url 'https://example.com' }
}
```
# Python
- python-prefix = `PYTHON_`
## pip config
```bash
pip config set global.index-url https://example.com
```
## conda config
- language = `yaml`
```
channels:
- https://example.com/conda
```
# Docker
## Dockerfile
```dockerfile
FROM ubuntu:20.04
RUN echo "Hello World"
```
### Multi-stage Build
```dockerfile
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install
```

View File

@ -0,0 +1,63 @@
# Java
- java-prefix = `JAVA_`
## Maven Config
- maven-name = `maven`
```xml
<settings>
<mirrors>
</mirrors>
</settings>
```
## Gradle Config
```groovy
repositories {
maven { url 'https://example.com' }
}
```
# Python
- python-prefix = `PYTHON_`
## pip config
```bash
pip config set global.index-url https://example.com
```
## conda config
- language = `yaml`
```
channels:
- https://example.com/conda
```
# Docker
## Dockerfile
```dockerfile
FROM ubuntu:20.04
RUN echo "Hello World"
```
### Multi-stage Build
```dockerfile
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install
```

View File

@ -1,65 +0,0 @@
# Global Config
- prefix = `TEST_`
- output = `:terminal`
- translate = `:escape`
## Java
### Maven Config
- name = `maven_settings`
```xml
<mirror>
<id>test</id>
<name>Test Mirror</name>
<url>https://example.com</url>
</mirror>
```
### Gradle Config
```groovy
repositories {
maven { url 'https://example.com' }
}
```
## Python
- prefix = `PY_`
### pip config
```ini
[global]
index-url = https://example.com/simple
```
### conda config
- language = `yaml`
```
channels:
- https://example.com/conda
```
## Docker
### Dockerfile
```dockerfile
FROM ubuntu:20.04
RUN echo "Hello World"
```
#### Multi-stage Build
```dockerfile
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install
```

View File

@ -5,57 +5,68 @@
# Test File : test-parser.rakutest
# Test Authors : Aoran Zeng <ccmywish@qq.com>
# Created On : <2025-07-15>
# Last Modified : <2025-07-15>
# Last Modified : <2025-07-16>
# ---------------------------------------------------------------
use lib '../lib';
use Parser;
sub MAIN() {
my $test-file = './fixture/test-hierarchy.md'.IO;
unless $test-file.e {
die " test-hierarchy.md ";
}
my $test-file1 = './fixture/test-hierarchy-with-global.md'.IO;
my $test-file2 = './fixture/test-hierarchy-without-global.md'.IO;
my $parser1 = Parser::Parser.new($test-file1);
my $parser2 = Parser::Parser.new($test-file2);
my $parser = Parser::Parser.new($test-file);
$parser.parse;
$parser1.parse();
$parser2.parse();
say "====== ======";
for $parser.global-config.keys.sort -> $key {
my $value = $parser.global-config.get($key);
say "$key = {$value.as-string} (type: {$value.type})";
}
say "";
say "====== Section TOC ======";
print-sections-hierarchy($parser.root-sections, 0);
say "";
say "====== Section ======";
my @flat-sections = $parser.sections;
say " {+@flat-sections} sections:";
for @flat-sections -> $section {
my $hier-path = $section.get-hierarchical-path;
my $has-code = $section.raw-string ?? True !! False;
say " - {$section.title}";
say " : {$section.level}) $hier-path";
say " : $has-code";
# 显示section的配置
if $section.config.keys {
say " : " ~ $section.config.keys.join(", ");
}
say "";
sub print-sections-flatly($parser)
{
say "====== sections ======";
for $parser.sections.kv -> $i, $section {
my $title = $section.title || "(Global)";
my $has-config = $section.config.keys ?? "" !! "";
my $has-code = $section.raw-string ?? "" !! "";
say " [$i] Level {$section.level}: $title - $has-config, $has-code";
}
}
sub print-sections-hierarchy(@sections, $indent-level) {
my $indent = " " x $indent-level;
for @sections -> $section {
say "$indent- {$section.title}";
# 深度遍历
sub print-sections-hierarchyly($parser) {
say "====== hierarchy ======";
my $indent = 0;
sub format-section($section, $level) {
my $prefix = ' ' x $level;
my $title = $section.title // '(Global)';
return "{$prefix}- {$title} (level {$section.level})";
}
sub print-section-tree($section, $level) {
say format-section($section, $level);
if $section.has-children {
print-sections-hierarchy($section.children, $indent-level + 1);
for $section.children -> $child {
print-section-tree($child, $level + 1);
}
}
}
# 从parser的global section开始打印
my $global = $parser.global-section();
print-section-tree($global, $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);