优化打印逻辑
This commit is contained in:
parent
210a6942f4
commit
d3494091a0
@ -7,8 +7,6 @@ import org.jcnc.snow.compiler.parser.ParserEngine;
|
||||
import org.jcnc.snow.compiler.parser.context.ParserContext;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.Node;
|
||||
import org.jcnc.snow.compiler.parser.function.ASTPrinter;
|
||||
import org.jcnc.snow.compiler.parser.util.ASTJsonSerializer;
|
||||
import org.jcnc.snow.compiler.parser.util.JsonFormatter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -25,18 +23,17 @@ public class Main {
|
||||
// 1. 词法分析
|
||||
LexerEngine lexerEngine = new LexerEngine(source);
|
||||
List<Token> tokens = lexerEngine.getAllTokens();
|
||||
TokenPrinter.printTokens(tokens); // 可选:打印 Token 列表
|
||||
|
||||
// 2. 语法分析
|
||||
ParserContext ctx = new ParserContext(tokens);
|
||||
List<Node> ast = new ParserEngine(ctx).parse();
|
||||
|
||||
// 3. 可读地打印 AST
|
||||
ASTPrinter.print(ast);
|
||||
|
||||
// 4. AST JSON
|
||||
System.out.println("=== Pretty JSON ===");
|
||||
System.out.println(JsonFormatter.prettyPrint(ASTJsonSerializer.toJsonString(ast)));
|
||||
|
||||
// 打印
|
||||
TokenPrinter.print(tokens); // 打印 Token 列表
|
||||
ASTPrinter.print(ast); // 打印 AST
|
||||
// ASTPrinter.printJson(ast); // 打印JSON AST
|
||||
|
||||
}
|
||||
}
|
||||
@ -32,7 +32,7 @@ public class TokenPrinter {
|
||||
* @param tokens 要打印的 Token 列表,不应为 null;列表中的每个元素
|
||||
* 都应包含有效的行号、列号、类型和词素信息
|
||||
*/
|
||||
public static void printTokens(List<Token> tokens) {
|
||||
public static void print(List<Token> tokens) {
|
||||
// 打印表头:列名对齐,宽度分别为 6、6、16
|
||||
System.out.printf("%-6s %-6s %-16s %s%n", "line", "col", "type", "lexeme");
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
@ -4,12 +4,14 @@ import org.jcnc.snow.compiler.parser.ast.*;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.ExpressionNode;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.Node;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.StatementNode;
|
||||
import org.jcnc.snow.compiler.parser.util.ASTJsonSerializer;
|
||||
import org.jcnc.snow.compiler.parser.util.JsonFormatter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AST 打印器,用于将抽象语法树(AST)中的节点打印为可读的、格式化的多行文本。
|
||||
* 每个节点根据其类型和结构打印相应的缩进层级,便于人类阅读和调试。
|
||||
* 以及将 AST 序列化为 JSON 并美化输出。
|
||||
*/
|
||||
public class ASTPrinter {
|
||||
|
||||
@ -31,7 +33,7 @@ public class ASTPrinter {
|
||||
* @param indent 当前的缩进层级(每层两个空格)。
|
||||
*/
|
||||
private static void print(Node n, int indent) {
|
||||
String pad = " ".repeat(indent); // 缩进字符串
|
||||
String pad = " ".repeat(indent);
|
||||
|
||||
switch (n) {
|
||||
case ModuleNode m -> {
|
||||
@ -97,4 +99,15 @@ public class ASTPrinter {
|
||||
System.out.println(pad + n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化整个 AST 为 JSON,并输出美化后的字符串。
|
||||
*
|
||||
* @param nodes 要序列化的 AST 节点列表。
|
||||
*/
|
||||
public static void printJson(List<Node> nodes) {
|
||||
String rawJson = ASTJsonSerializer.toJsonString(nodes);
|
||||
String pretty = JsonFormatter.prettyPrint(rawJson);
|
||||
System.out.println(pretty);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user