update test_ast file
This commit is contained in:
parent
e0e8c7b26d
commit
6aa28f9b23
File diff suppressed because it is too large
Load Diff
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,12 +105,23 @@ void PrintUsage(const char* program_name)
|
|||
{
|
||||
std::cout << "Usage: " << program_name << " <file_path> [options]\n";
|
||||
std::cout << "\nOptions:\n";
|
||||
std::cout << " -v, --verbose Print verbose output\n";
|
||||
std::cout << " -v, --verbose Show verbose output with source code\n";
|
||||
std::cout << " -c, --compact Use compact output mode\n";
|
||||
std::cout << " -s, --show-source Show source code snippets (default: off)\n";
|
||||
std::cout << " -l, --hide-location Hide location information\n";
|
||||
std::cout << " -n, --no-colors Disable colored output\n";
|
||||
std::cout << " -t, --no-tree Disable tree characters\n";
|
||||
std::cout << " -k, --show-kind Show node kind enums\n";
|
||||
std::cout << " -i, --incremental Test incremental parsing\n";
|
||||
std::cout << " -h, --help Show this help message\n";
|
||||
std::cout << "\nExample:\n";
|
||||
std::cout << "\nPreset Modes:\n";
|
||||
std::cout << " --verbose Equivalent to: -s -k\n";
|
||||
std::cout << " --compact Equivalent to: -c -l -t\n";
|
||||
std::cout << "\nExamples:\n";
|
||||
std::cout << " " << program_name << " test.tsf\n";
|
||||
std::cout << " " << program_name << " test.tsf -v -s\n";
|
||||
std::cout << " " << program_name << " test.tsf --verbose\n";
|
||||
std::cout << " " << program_name << " test.tsf --compact\n";
|
||||
std::cout << " " << program_name << " test.tsf -s -n # Show source without colors\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
|
|
@ -122,9 +133,11 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
|
||||
std::string filepath;
|
||||
bool verbose = false;
|
||||
bool test_incremental = false;
|
||||
|
||||
// 默认打印选项
|
||||
PrintOptions opts = PrintOptions::Default();
|
||||
|
||||
// 解析命令行参数
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
|
|
@ -136,7 +149,31 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
else if (arg == "-v" || arg == "--verbose")
|
||||
{
|
||||
verbose = true;
|
||||
opts = PrintOptions::Verbose();
|
||||
}
|
||||
else if (arg == "-c" || arg == "--compact")
|
||||
{
|
||||
opts = PrintOptions::Compact();
|
||||
}
|
||||
else if (arg == "-s" || arg == "--show-source")
|
||||
{
|
||||
opts.show_source_code = true;
|
||||
}
|
||||
else if (arg == "-l" || arg == "--hide-location")
|
||||
{
|
||||
opts.show_location = false;
|
||||
}
|
||||
else if (arg == "-n" || arg == "--no-colors")
|
||||
{
|
||||
opts.use_colors = false;
|
||||
}
|
||||
else if (arg == "-t" || arg == "--no-tree")
|
||||
{
|
||||
opts.use_tree_chars = false;
|
||||
}
|
||||
else if (arg == "-k" || arg == "--show-kind")
|
||||
{
|
||||
opts.show_node_kind = true;
|
||||
}
|
||||
else if (arg == "-i" || arg == "--incremental")
|
||||
{
|
||||
|
|
@ -158,71 +195,68 @@ int main(int argc, char* argv[])
|
|||
// 读取文件
|
||||
std::cout << "Reading file: " << filepath << "\n";
|
||||
std::string source = ReadFile(filepath);
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "File size: " << source.length() << " bytes\n";
|
||||
std::cout << "----------------------------------------\n";
|
||||
std::cout << source << "\n";
|
||||
std::cout << "----------------------------------------\n\n";
|
||||
}
|
||||
std::cout << "File size: " << source.length() << " bytes\n\n";
|
||||
|
||||
// 创建 Tree-Sitter 解析器
|
||||
std::cout << "Parsing with Tree-Sitter...\n";
|
||||
TreeSitterParser ts_parser;
|
||||
TSTree* tree = ts_parser.Parse(source);
|
||||
TSNode root = ts_parser.GetRootNode();
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "Root node type: " << ts_node_type(root) << "\n";
|
||||
std::cout << "Root node child count: " << ts_node_child_count(root) << "\n\n";
|
||||
}
|
||||
|
||||
// 创建 AST 反序列化器
|
||||
Deserializer deserializer;
|
||||
|
||||
ParseResult result;
|
||||
|
||||
if (test_incremental)
|
||||
{
|
||||
std::cout << "Using incremental parsing...\n";
|
||||
std::cout << "Using incremental parsing...\n\n";
|
||||
auto inc_result = deserializer.ParseIncremental(root, source);
|
||||
result = std::move(inc_result.result);
|
||||
|
||||
std::cout << "Incremental Parse Statistics:\n";
|
||||
std::cout << " Nodes parsed: " << inc_result.nodes_parsed << "\n";
|
||||
std::cout << " Nodes unchanged: " << inc_result.nodes_unchanged << "\n";
|
||||
std::cout << " Total nodes: " << inc_result.TotalNodes() << "\n";
|
||||
std::cout << " Change rate: " << (inc_result.ChangeRate() * 100) << "%\n\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Using full parsing...\n";
|
||||
result = deserializer.Parse(root, source);
|
||||
}
|
||||
|
||||
// 打印解析结果
|
||||
std::cout << "\n";
|
||||
DebugPrint(result);
|
||||
// 打印 AST 结果(带源码)
|
||||
DebugPrint(result, source, opts);
|
||||
|
||||
// 打印摘要
|
||||
std::cout << "\n========================================\n";
|
||||
std::cout << "Summary:\n";
|
||||
std::cout << "\n";
|
||||
std::cout << Color::BrightBlue << "========================================\n"
|
||||
<< Color::Reset;
|
||||
std::cout << Color::BrightCyan << "Summary:\n"
|
||||
<< Color::Reset;
|
||||
std::cout << " File: " << filepath << "\n";
|
||||
std::cout << " Size: " << source.length() << " bytes\n";
|
||||
std::cout << " AST Nodes: " << result.statements.size() << "\n";
|
||||
std::cout << " Errors: " << result.errors.size() << "\n";
|
||||
|
||||
if (result.root)
|
||||
{
|
||||
std::cout << " Statements: " << result.root->statements.size() << "\n";
|
||||
}
|
||||
|
||||
std::cout << " Errors: ";
|
||||
if (result.HasErrors())
|
||||
{
|
||||
std::cout << " Status: FAILED (with errors)\n";
|
||||
std::cout << Color::BrightRed << result.errors.size() << " ✗" << Color::Reset << "\n";
|
||||
std::cout << " Status: " << Color::BrightRed << "FAILED" << Color::Reset << "\n";
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << " Status: SUCCESS\n";
|
||||
std::cout << Color::BrightGreen << "0 ✓" << Color::Reset << "\n";
|
||||
std::cout << " Status: " << Color::BrightGreen << "SUCCESS" << Color::Reset << "\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
std::cerr << Color::BrightRed << "Error: " << e.what() << Color::Reset << "\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue