update test_ast file
This commit is contained in:
@@ -7,16 +7,88 @@
|
||||
|
||||
namespace lsp::language::ast
|
||||
{
|
||||
// ===== 打印配置 =====
|
||||
struct PrintOptions
|
||||
{
|
||||
bool show_source_code = false; // 是否显示源代码片段
|
||||
bool show_location = true; // 是否显示位置信息
|
||||
bool use_colors = true; // 是否使用颜色
|
||||
bool use_tree_chars = true; // 是否使用树形字符
|
||||
bool compact_mode = false; // 紧凑模式
|
||||
bool show_node_kind = false; // 是否显示节点类型枚举
|
||||
int indent_size = 2; // 缩进大小
|
||||
int max_source_length = 60; // 源码最大显示长度
|
||||
|
||||
// 预设配置
|
||||
static PrintOptions Verbose()
|
||||
{
|
||||
PrintOptions opts;
|
||||
opts.show_source_code = true;
|
||||
opts.show_location = true;
|
||||
opts.show_node_kind = true;
|
||||
opts.compact_mode = false;
|
||||
return opts;
|
||||
}
|
||||
|
||||
static PrintOptions Compact()
|
||||
{
|
||||
PrintOptions opts;
|
||||
opts.compact_mode = true;
|
||||
opts.use_tree_chars = false;
|
||||
opts.show_location = false;
|
||||
return opts;
|
||||
}
|
||||
|
||||
static PrintOptions Default()
|
||||
{
|
||||
return PrintOptions();
|
||||
}
|
||||
};
|
||||
|
||||
// ===== ANSI 颜色代码 =====
|
||||
namespace Color
|
||||
{
|
||||
inline const char* Reset = "\033[0m";
|
||||
inline const char* Bold = "\033[1m";
|
||||
inline const char* Dim = "\033[2m";
|
||||
|
||||
// 前景色
|
||||
inline const char* Red = "\033[31m";
|
||||
inline const char* Green = "\033[32m";
|
||||
inline const char* Yellow = "\033[33m";
|
||||
inline const char* Blue = "\033[34m";
|
||||
inline const char* Magenta = "\033[35m";
|
||||
inline const char* Cyan = "\033[36m";
|
||||
inline const char* White = "\033[37m";
|
||||
inline const char* Gray = "\033[90m";
|
||||
|
||||
// 亮色
|
||||
inline const char* BrightRed = "\033[91m";
|
||||
inline const char* BrightGreen = "\033[92m";
|
||||
inline const char* BrightYellow = "\033[93m";
|
||||
inline const char* BrightBlue = "\033[94m";
|
||||
inline const char* BrightMagenta = "\033[95m";
|
||||
inline const char* BrightCyan = "\033[96m";
|
||||
}
|
||||
|
||||
class DebugPrinter : public ASTVisitor
|
||||
{
|
||||
public:
|
||||
explicit DebugPrinter(std::ostream& os = std::cout, int indent_size = 2) :
|
||||
os_(os), indent_size_(indent_size), current_indent_(0) {}
|
||||
explicit DebugPrinter(std::ostream& os = std::cout, const PrintOptions& opts = PrintOptions::Default()) :
|
||||
os_(os), options_(opts), current_indent_(0), source_code_(nullptr) {}
|
||||
|
||||
void Print(const ASTNode* node);
|
||||
void PrintStatements(const std::vector<StatementPtr>& statements);
|
||||
void PrintParseResult(const ParseResult& result);
|
||||
|
||||
// 设置源代码用于显示
|
||||
void SetSourceCode(const std::string* source) { source_code_ = source; }
|
||||
|
||||
// 设置打印选项
|
||||
void SetOptions(const PrintOptions& opts) { options_ = opts; }
|
||||
|
||||
// Visitor 接口实现
|
||||
void VisitProgram(Program& node) override;
|
||||
void VisitUnitDefinition(UnitDefinition& node) override;
|
||||
void VisitClassDefinition(ClassDefinition& node) override;
|
||||
void VisitClassMember(ClassMember& node) override;
|
||||
@@ -69,20 +141,40 @@ namespace lsp::language::ast
|
||||
|
||||
private:
|
||||
std::ostream& os_;
|
||||
int indent_size_;
|
||||
PrintOptions options_;
|
||||
int current_indent_;
|
||||
const std::string* source_code_;
|
||||
std::vector<bool> is_last_child_stack_; // 用于树形结构绘制
|
||||
|
||||
void IncreaseIndent() { current_indent_ += indent_size_; }
|
||||
void DecreaseIndent() { current_indent_ -= indent_size_; }
|
||||
void PrintIndent();
|
||||
// 缩进控制
|
||||
void IncreaseIndent() { current_indent_ += options_.indent_size; }
|
||||
void DecreaseIndent() { current_indent_ -= options_.indent_size; }
|
||||
|
||||
// 输出辅助
|
||||
void PrintIndent(bool is_last = false);
|
||||
void PrintTreePrefix(bool is_last = false);
|
||||
std::string GetIndent() const;
|
||||
|
||||
void PrintLocation(const Location& loc);
|
||||
// 颜色辅助
|
||||
const char* GetColor(const char* color) const;
|
||||
void PrintColored(const std::string& text, const char* color);
|
||||
|
||||
// 节点信息输出
|
||||
void PrintNodeHeader(const std::string& type_name, const Location& loc);
|
||||
void PrintExpression(const Expression* expr);
|
||||
void PrintLocation(const Location& loc);
|
||||
void PrintSourceSnippet(const Location& loc);
|
||||
void PrintKeyValue(const std::string& key, const std::string& value, const char* value_color = nullptr);
|
||||
void PrintKeyValue(const std::string& key, int value);
|
||||
void PrintKeyValue(const std::string& key, bool value);
|
||||
|
||||
// 表达式和语句打印
|
||||
void PrintExpression(const Expression* expr, const std::string& label = "", bool is_last = false);
|
||||
void PrintStatement(const Statement* stmt, const std::string& label = "", bool is_last = false);
|
||||
void PrintSignature(const Signature& sig);
|
||||
void PrintParameter(const Parameter& param);
|
||||
void PrintLeftHandSide(const LeftHandSide& lhs);
|
||||
void PrintParameter(const Parameter& param, bool is_last = false);
|
||||
void PrintLeftHandSide(const LeftHandSide& lhs, const std::string& label = "", bool is_last = false);
|
||||
|
||||
// 枚举值打印
|
||||
void PrintOperator(BinaryOperator op);
|
||||
void PrintOperator(UnaryOperator op);
|
||||
void PrintOperator(AssignmentOperator op);
|
||||
@@ -91,10 +183,21 @@ namespace lsp::language::ast
|
||||
void PrintMethodModifier(MethodModifier modifier);
|
||||
void PrintReferenceModifier(ReferenceModifier modifier);
|
||||
void PrintError(const ParseError& error);
|
||||
|
||||
// 工具函数
|
||||
std::string EscapeString(const std::string& str) const;
|
||||
std::string TruncateString(const std::string& str, size_t max_len) const;
|
||||
std::string GetSourceText(const Location& loc) const;
|
||||
};
|
||||
|
||||
std::string DebugString(const ASTNode* node);
|
||||
std::string DebugString(const ParseResult& result);
|
||||
void DebugPrint(const ASTNode* node);
|
||||
void DebugPrint(const ParseResult& result);
|
||||
// 便捷函数
|
||||
std::string DebugString(const ASTNode* node, const PrintOptions& opts = PrintOptions::Default());
|
||||
std::string DebugString(const ParseResult& result, const PrintOptions& opts = PrintOptions::Default());
|
||||
|
||||
void DebugPrint(const ASTNode* node, const PrintOptions& opts = PrintOptions::Default());
|
||||
void DebugPrint(const ParseResult& result, const PrintOptions& opts = PrintOptions::Default());
|
||||
|
||||
// 带源码的打印
|
||||
void DebugPrint(const ASTNode* node, const std::string& source, const PrintOptions& opts = PrintOptions::Default());
|
||||
void DebugPrint(const ParseResult& result, const std::string& source, const PrintOptions& opts = PrintOptions::Default());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user