♻️ 使用module重构所有代码
This commit is contained in:
@@ -15,18 +15,39 @@ if(UNIX AND NOT APPLE)
|
||||
find_package(Threads REQUIRED)
|
||||
endif()
|
||||
|
||||
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
|
||||
|
||||
set(SOURCES
|
||||
test.cpp
|
||||
debug_printer.cpp
|
||||
../../src/language/ast/deserializer.cpp
|
||||
../../src/language/ast/detail.cpp
|
||||
../../src/language/ast/tree_sitter_utils.cpp
|
||||
test.cppm
|
||||
debug_printer.cppm
|
||||
../../src/utils/string.cppm
|
||||
../../src/utils/string.cpp
|
||||
../../src/tree-sitter/scanner.c
|
||||
../../src/tree-sitter/parser.c)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_sources(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
FILE_SET cxx_modules TYPE CXX_MODULES
|
||||
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../src/language/ast
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/bridge
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
FILES ../../src/bridge/tree_sitter.cppm
|
||||
../../src/bridge/spdlog.cppm
|
||||
../../src/language/ast/ast.cppm
|
||||
../../src/language/ast/types.cppm
|
||||
../../src/language/ast/deserializer.cppm
|
||||
../../src/language/ast/deserializer_impl.cppm
|
||||
../../src/language/ast/ts_utils.cppm
|
||||
../../src/language/ast/detail.cppm
|
||||
../../src/utils/string.cppm
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/debug_printer.cppm
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test.cppm)
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/language/ast)
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE SPDLOG_HEADER_ONLY
|
||||
FMT_HEADER_ONLY)
|
||||
@@ -37,8 +58,9 @@ target_link_libraries(
|
||||
fmt::fmt-header-only tree-sitter::tree-sitter
|
||||
$<$<PLATFORM_ID:Linux>:Threads::Threads>)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
||||
target_compile_options(
|
||||
${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic $<$<CONFIG:Debug>:-g -O0>
|
||||
$<$<CONFIG:Release>:-O3>)
|
||||
endif()
|
||||
target_compile_options(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
-Wall -Wextra -Wpedantic $<$<CONFIG:Debug>:-g -O0>
|
||||
-Wno-import-implementation-partition-unit-in-interface-unit
|
||||
$<$<CONFIG:Release>:-O3>)
|
||||
|
||||
+243
-4
@@ -1,8 +1,247 @@
|
||||
#include "debug_printer.hpp"
|
||||
#include <sstream>
|
||||
module;
|
||||
|
||||
namespace lsp::language::ast::debug
|
||||
import std;
|
||||
|
||||
export module lsp.test.ast.debug_printer;
|
||||
|
||||
import lsp.language.ast;
|
||||
|
||||
export namespace lsp::language::ast::debug
|
||||
{
|
||||
using std::size_t;
|
||||
|
||||
// ===== 打印配置 =====
|
||||
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, 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;
|
||||
void VisitMethodDeclaration(MethodDeclaration& node) override;
|
||||
void VisitPropertyDeclaration(PropertyDeclaration& node) override;
|
||||
void VisitExternalMethodDefinition(ExternalMethodDefinition& node) override;
|
||||
|
||||
// 基础表达式
|
||||
void VisitIdentifier(Identifier& node) override;
|
||||
void VisitLiteral(Literal& node) override;
|
||||
void VisitArrayExpression(ArrayExpression& node) override;
|
||||
void VisitParenthesizedExpression(ParenthesizedExpression& node) override;
|
||||
|
||||
// 访问表达式
|
||||
void VisitCallExpression(CallExpression& node) override;
|
||||
void VisitAttributeExpression(AttributeExpression& node) override;
|
||||
void VisitSubscriptExpression(SubscriptExpression& node) override;
|
||||
|
||||
// 一元表达式
|
||||
void VisitUnaryPlusExpression(UnaryPlusExpression& node) override;
|
||||
void VisitUnaryMinusExpression(UnaryMinusExpression& node) override;
|
||||
void VisitPrefixIncrementExpression(PrefixIncrementExpression& node) override;
|
||||
void VisitPrefixDecrementExpression(PrefixDecrementExpression& node) override;
|
||||
void VisitPostfixIncrementExpression(PostfixIncrementExpression& node) override;
|
||||
void VisitPostfixDecrementExpression(PostfixDecrementExpression& node) override;
|
||||
void VisitLogicalNotExpression(LogicalNotExpression& node) override;
|
||||
void VisitBitwiseNotExpression(BitwiseNotExpression& node) override;
|
||||
void VisitDerivativeExpression(DerivativeExpression& node) override;
|
||||
void VisitFunctionPointerExpression(FunctionPointerExpression& node) override;
|
||||
void VisitMatrixTransposeExpression(MatrixTransposeExpression& node) override;
|
||||
void VisitExprOperatorExpression(ExprOperatorExpression& node) override;
|
||||
|
||||
// 二元和三元表达式
|
||||
void VisitBinaryExpression(BinaryExpression& node) override;
|
||||
void VisitTernaryExpression(TernaryExpression& node) override;
|
||||
void VisitAssignmentExpression(AssignmentExpression& node) override;
|
||||
|
||||
// 特殊表达式
|
||||
void VisitAnonymousFunctionExpression(AnonymousFunctionExpression& node) override;
|
||||
void VisitNewExpression(NewExpression& node) override;
|
||||
void VisitEchoExpression(EchoExpression& node) override;
|
||||
void VisitRaiseExpression(RaiseExpression& node) override;
|
||||
void VisitInheritedExpression(InheritedExpression& node) override;
|
||||
void VisitTSSQLExpression(TSSQLExpression& node) override;
|
||||
void VisitColumnReference(ColumnReference& node) override;
|
||||
|
||||
// 模式
|
||||
void VisitUnpackPattern(UnpackPattern& node) override;
|
||||
|
||||
// 参数
|
||||
void VisitParameter(Parameter& node) override;
|
||||
|
||||
// 语句
|
||||
void VisitExpressionStatement(ExpressionStatement& node) override;
|
||||
void VisitBlockStatement(BlockStatement& node) override;
|
||||
void VisitIfStatement(IfStatement& node) override;
|
||||
void VisitForInStatement(ForInStatement& node) override;
|
||||
void VisitForToStatement(ForToStatement& node) override;
|
||||
void VisitWhileStatement(WhileStatement& node) override;
|
||||
void VisitRepeatStatement(RepeatStatement& node) override;
|
||||
void VisitCaseStatement(CaseStatement& node) override;
|
||||
void VisitTryStatement(TryStatement& node) override;
|
||||
void VisitBreakStatement(BreakStatement&) override;
|
||||
void VisitContinueStatement(ContinueStatement&) override;
|
||||
void VisitReturnStatement(ReturnStatement& node) override;
|
||||
void VisitUsesStatement(UsesStatement& node) override;
|
||||
void VisitMatrixIterationStatement(MatrixIterationStatement& node) override;
|
||||
|
||||
// 函数和声明
|
||||
void VisitFunctionDefinition(FunctionDefinition& node) override;
|
||||
void VisitFunctionDeclaration(FunctionDeclaration& node) override;
|
||||
void VisitVarDeclaration(VarDeclaration& node) override;
|
||||
void VisitStaticDeclaration(StaticDeclaration& node) override;
|
||||
void VisitGlobalDeclaration(GlobalDeclaration& node) override;
|
||||
void VisitConstDeclaration(ConstDeclaration& node) override;
|
||||
void VisitFieldDeclaration(FieldDeclaration& node) override;
|
||||
|
||||
// 条件编译
|
||||
void VisitCompilerDirective(CompilerDirective& node) override;
|
||||
void VisitConditionalBlock(ConditionalBlock& node) override;
|
||||
void VisitConditionalDirective(ConditionalDirective& node) override;
|
||||
void VisitTSLXBlock(TSLXBlock& node) override;
|
||||
|
||||
private:
|
||||
std::ostream& os_;
|
||||
PrintOptions options_;
|
||||
int current_indent_;
|
||||
const std::string* source_code_;
|
||||
std::vector<bool> is_last_child_stack_; // 用于树形结构绘制
|
||||
|
||||
// 缩进控制
|
||||
void IncreaseIndent() { current_indent_ += options_.indent_size; }
|
||||
void DecreaseIndent() { current_indent_ -= options_.indent_size; }
|
||||
|
||||
// 输出辅助
|
||||
void PrintIndent(std::optional<bool> is_last = std::nullopt);
|
||||
void PrintTreePrefix(bool is_last = false);
|
||||
std::string GetIndent() const;
|
||||
void EmitChildren(std::vector<std::function<void(bool)>>&& children);
|
||||
|
||||
// 颜色辅助
|
||||
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 PrintLocation(const Location& loc);
|
||||
void PrintSourceSnippet(const Location& loc, bool is_last_child);
|
||||
void PrintKeyValue(const std::string& key, const std::string& value, const Location& location, const char* value_color, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, const std::string& value, const char* value_color = nullptr, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, int value, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, bool value, std::optional<bool> is_last = std::nullopt);
|
||||
|
||||
// 表达式和语句打印
|
||||
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 PrintParameter(const Parameter& param, bool is_last = false);
|
||||
void PrintLeftHandSide(const LValue& lhs, const std::string& label = "", bool is_last = false);
|
||||
|
||||
// 枚举值打印
|
||||
void PrintOperator(BinaryOperator op);
|
||||
void PrintOperator(AssignmentOperator op);
|
||||
void PrintLiteralKind(LiteralKind kind);
|
||||
void PrintAccessModifier(AccessModifier modifier);
|
||||
void PrintMethodModifier(MethodModifier modifier);
|
||||
void PrintMethodType(MethodKind kind);
|
||||
void PrintReferenceModifier(ReferenceModifier modifier);
|
||||
void PrintConditionalCompilationType(ConditionalCompilationType type);
|
||||
void PrintTSSQLExpressionType(TSSQLExpressionType type);
|
||||
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 GetNodeKindName(NodeKind kind) const;
|
||||
};
|
||||
|
||||
// 便捷函数
|
||||
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());
|
||||
|
||||
using lsp::language::ast::VisitVariant;
|
||||
// ===== 辅助函数实现 =====
|
||||
|
||||
void DebugPrinter::Print(const ASTNode* node)
|
||||
@@ -2195,7 +2434,7 @@ namespace lsp::language::ast::debug
|
||||
for (size_t i = 0; i < node.branches.size(); ++i)
|
||||
{
|
||||
const auto& branch = node.branches[i];
|
||||
children.push_back([&, i](bool is_last) {
|
||||
children.push_back([&](bool is_last) {
|
||||
PrintIndent(is_last);
|
||||
PrintColored("Branch:", Color::Yellow);
|
||||
os_ << "\n";
|
||||
@@ -1,242 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include "../../src/language/ast/types.hpp"
|
||||
#include "../../src/language/ast/deserializer.hpp"
|
||||
|
||||
namespace lsp::language::ast::debug
|
||||
{
|
||||
// ===== 打印配置 =====
|
||||
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, 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;
|
||||
void VisitMethodDeclaration(MethodDeclaration& node) override;
|
||||
void VisitPropertyDeclaration(PropertyDeclaration& node) override;
|
||||
void VisitExternalMethodDefinition(ExternalMethodDefinition& node) override;
|
||||
|
||||
// 基础表达式
|
||||
void VisitIdentifier(Identifier& node) override;
|
||||
void VisitLiteral(Literal& node) override;
|
||||
void VisitArrayExpression(ArrayExpression& node) override;
|
||||
void VisitParenthesizedExpression(ParenthesizedExpression& node) override;
|
||||
|
||||
// 访问表达式
|
||||
void VisitCallExpression(CallExpression& node) override;
|
||||
void VisitAttributeExpression(AttributeExpression& node) override;
|
||||
void VisitSubscriptExpression(SubscriptExpression& node) override;
|
||||
|
||||
// 一元表达式
|
||||
void VisitUnaryPlusExpression(UnaryPlusExpression& node) override;
|
||||
void VisitUnaryMinusExpression(UnaryMinusExpression& node) override;
|
||||
void VisitPrefixIncrementExpression(PrefixIncrementExpression& node) override;
|
||||
void VisitPrefixDecrementExpression(PrefixDecrementExpression& node) override;
|
||||
void VisitPostfixIncrementExpression(PostfixIncrementExpression& node) override;
|
||||
void VisitPostfixDecrementExpression(PostfixDecrementExpression& node) override;
|
||||
void VisitLogicalNotExpression(LogicalNotExpression& node) override;
|
||||
void VisitBitwiseNotExpression(BitwiseNotExpression& node) override;
|
||||
void VisitDerivativeExpression(DerivativeExpression& node) override;
|
||||
void VisitFunctionPointerExpression(FunctionPointerExpression& node) override;
|
||||
void VisitMatrixTransposeExpression(MatrixTransposeExpression& node) override;
|
||||
void VisitExprOperatorExpression(ExprOperatorExpression& node) override;
|
||||
|
||||
// 二元和三元表达式
|
||||
void VisitBinaryExpression(BinaryExpression& node) override;
|
||||
void VisitTernaryExpression(TernaryExpression& node) override;
|
||||
void VisitAssignmentExpression(AssignmentExpression& node) override;
|
||||
|
||||
// 特殊表达式
|
||||
void VisitAnonymousFunctionExpression(AnonymousFunctionExpression& node) override;
|
||||
void VisitNewExpression(NewExpression& node) override;
|
||||
void VisitEchoExpression(EchoExpression& node) override;
|
||||
void VisitRaiseExpression(RaiseExpression& node) override;
|
||||
void VisitInheritedExpression(InheritedExpression& node) override;
|
||||
void VisitTSSQLExpression(TSSQLExpression& node) override;
|
||||
void VisitColumnReference(ColumnReference& node) override;
|
||||
|
||||
// 模式
|
||||
void VisitUnpackPattern(UnpackPattern& node) override;
|
||||
|
||||
// 参数
|
||||
void VisitParameter(Parameter& node) override;
|
||||
|
||||
// 语句
|
||||
void VisitExpressionStatement(ExpressionStatement& node) override;
|
||||
void VisitBlockStatement(BlockStatement& node) override;
|
||||
void VisitIfStatement(IfStatement& node) override;
|
||||
void VisitForInStatement(ForInStatement& node) override;
|
||||
void VisitForToStatement(ForToStatement& node) override;
|
||||
void VisitWhileStatement(WhileStatement& node) override;
|
||||
void VisitRepeatStatement(RepeatStatement& node) override;
|
||||
void VisitCaseStatement(CaseStatement& node) override;
|
||||
void VisitTryStatement(TryStatement& node) override;
|
||||
void VisitBreakStatement(BreakStatement&) override;
|
||||
void VisitContinueStatement(ContinueStatement&) override;
|
||||
void VisitReturnStatement(ReturnStatement& node) override;
|
||||
void VisitUsesStatement(UsesStatement& node) override;
|
||||
void VisitMatrixIterationStatement(MatrixIterationStatement& node) override;
|
||||
|
||||
// 函数和声明
|
||||
void VisitFunctionDefinition(FunctionDefinition& node) override;
|
||||
void VisitFunctionDeclaration(FunctionDeclaration& node) override;
|
||||
void VisitVarDeclaration(VarDeclaration& node) override;
|
||||
void VisitStaticDeclaration(StaticDeclaration& node) override;
|
||||
void VisitGlobalDeclaration(GlobalDeclaration& node) override;
|
||||
void VisitConstDeclaration(ConstDeclaration& node) override;
|
||||
void VisitFieldDeclaration(FieldDeclaration& node) override;
|
||||
|
||||
// 条件编译
|
||||
void VisitCompilerDirective(CompilerDirective& node) override;
|
||||
void VisitConditionalBlock(ConditionalBlock& node) override;
|
||||
void VisitConditionalDirective(ConditionalDirective& node) override;
|
||||
void VisitTSLXBlock(TSLXBlock& node) override;
|
||||
|
||||
private:
|
||||
std::ostream& os_;
|
||||
PrintOptions options_;
|
||||
int current_indent_;
|
||||
const std::string* source_code_;
|
||||
std::vector<bool> is_last_child_stack_; // 用于树形结构绘制
|
||||
|
||||
// 缩进控制
|
||||
void IncreaseIndent() { current_indent_ += options_.indent_size; }
|
||||
void DecreaseIndent() { current_indent_ -= options_.indent_size; }
|
||||
|
||||
// 输出辅助
|
||||
void PrintIndent(std::optional<bool> is_last = std::nullopt);
|
||||
void PrintTreePrefix(bool is_last = false);
|
||||
std::string GetIndent() const;
|
||||
void EmitChildren(std::vector<std::function<void(bool)>>&& children);
|
||||
|
||||
// 颜色辅助
|
||||
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 PrintLocation(const Location& loc);
|
||||
void PrintSourceSnippet(const Location& loc, bool is_last_child);
|
||||
void PrintKeyValue(const std::string& key, const std::string& value, const Location& location, const char* value_color, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, const std::string& value, const char* value_color = nullptr, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, int value, std::optional<bool> is_last = std::nullopt);
|
||||
void PrintKeyValue(const std::string& key, bool value, std::optional<bool> is_last = std::nullopt);
|
||||
|
||||
// 表达式和语句打印
|
||||
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 PrintParameter(const Parameter& param, bool is_last = false);
|
||||
void PrintLeftHandSide(const LValue& lhs, const std::string& label = "", bool is_last = false);
|
||||
|
||||
// 枚举值打印
|
||||
void PrintOperator(BinaryOperator op);
|
||||
void PrintOperator(AssignmentOperator op);
|
||||
void PrintLiteralKind(LiteralKind kind);
|
||||
void PrintAccessModifier(AccessModifier modifier);
|
||||
void PrintMethodModifier(MethodModifier modifier);
|
||||
void PrintMethodType(MethodKind kind);
|
||||
void PrintReferenceModifier(ReferenceModifier modifier);
|
||||
void PrintConditionalCompilationType(ConditionalCompilationType type);
|
||||
void PrintTSSQLExpressionType(TSSQLExpressionType type);
|
||||
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 GetNodeKindName(NodeKind kind) const;
|
||||
};
|
||||
|
||||
// 便捷函数
|
||||
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());
|
||||
}
|
||||
@@ -1,18 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
module;
|
||||
|
||||
extern "C" {
|
||||
#include <tree_sitter/api.h>
|
||||
}
|
||||
export module lsp.test.ast.main;
|
||||
|
||||
import std;
|
||||
import tree_sitter;
|
||||
import lsp.language.ast;
|
||||
import lsp.test.ast.debug_printer;
|
||||
|
||||
extern "C" const TSLanguage* tree_sitter_tsf(void);
|
||||
|
||||
#include "../../src/language/ast/deserializer.hpp"
|
||||
#include "./debug_printer.hpp"
|
||||
|
||||
using namespace lsp::language::ast;
|
||||
|
||||
// ==================== 文件读取 ====================
|
||||
@@ -199,7 +195,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
// 创建 Tree-Sitter 解析器
|
||||
TreeSitterParser ts_parser;
|
||||
[[maybe_unused]]TSTree* tree = ts_parser.Parse(source);
|
||||
[[maybe_unused]] TSTree* tree = ts_parser.Parse(source);
|
||||
TSNode root = ts_parser.GetRootNode();
|
||||
|
||||
// 创建 AST 反序列化器
|
||||
Reference in New Issue
Block a user