This commit is contained in:
csh
2025-10-25 23:52:33 +08:00
parent 857ba73755
commit e0e8c7b26d
10 changed files with 72 additions and 50 deletions
+13 -3
View File
@@ -1,3 +1,4 @@
#include <iostream>
#include "./detail.hpp"
#include "./tree_sitter_utils.hpp"
#include "./deserializer.hpp"
@@ -17,6 +18,10 @@ namespace lsp::language::ast
detail::ParseContext ctx(source, result.errors);
auto program = std::make_unique<Program>();
program->location = ts::NodeLocation(root);
program->node_type_name = "program";
uint32_t count = ts_node_child_count(root);
for (uint32_t i = 0; i < count; i++)
{
@@ -26,10 +31,11 @@ namespace lsp::language::ast
if (!ts_node_is_named(child))
continue;
StatementPtr stmt = detail::ParseStatement(child, ctx);
StatementPtr stmt = detail::ParseStatement(child, ctx);
if (stmt)
result.statements.push_back(std::move(stmt));
program->statements.push_back(std::move(stmt));
}
result.root = std::move(program);
// 收集语法错误
auto syntax_errors = detail::CollectSyntaxErrors(root, source);
@@ -46,6 +52,10 @@ namespace lsp::language::ast
detail::ParseContext ctx(source, result.result.errors);
auto program = std::make_unique<Program>();
program->location = ts::NodeLocation(root);
program->node_type_name = "program";
uint32_t count = ts_node_child_count(root);
for (uint32_t i = 0; i < count; i++)
{
@@ -62,7 +72,7 @@ namespace lsp::language::ast
StatementPtr stmt = detail::ParseStatement(child, ctx);
if (stmt)
result.result.statements.push_back(std::move(stmt));
program->statements.push_back(std::move(stmt));
}
auto syntax_errors = detail::CollectSyntaxErrors(root, source);
+1 -1
View File
@@ -46,7 +46,7 @@ namespace lsp::language::ast
// ===== 解析结果 =====
struct ParseResult
{
std::vector<StatementPtr> statements;
std::unique_ptr<Program> root;
std::vector<ParseError> errors;
bool HasErrors() const { return !errors.empty(); }
+4 -5
View File
@@ -1,4 +1,3 @@
#include <iostream>
#include <mutex>
#include "../../utils/string.hpp"
#include "./tree_sitter_utils.hpp"
@@ -207,7 +206,7 @@ namespace lsp::language::ast::detail
return type == "ERROR" || type == "MISSING";
}
std::vector<ParseError> CollectSyntaxErrors(TSNode node, const std::string& source)
std::vector<ParseError> CollectSyntaxErrors(TSNode node, std::string_view source)
{
std::vector<ParseError> errors;
@@ -1715,7 +1714,7 @@ namespace lsp::language::ast::detail
return stmt;
}
StatementPtr ParseUsesStatement(TSNode node, [[maybe_unused]] ParseContext& ctx)
StatementPtr ParseUsesStatement(TSNode node, ParseContext& ctx)
{
Location loc = ts::NodeLocation(node);
@@ -1728,11 +1727,11 @@ namespace lsp::language::ast::detail
for (uint32_t i = 0; i < count; i++)
{
const char* field = ts_node_field_name_for_child(node, i);
if (!field || std::string_view(field) != "module")
if (!field || std::string_view(field) != "units")
continue;
TSNode child = ts_node_child(node, i);
uses->modules.push_back(std::move(ts::Text(child, ctx.Source())));
uses->units.push_back(std::move(ts::Text(child, ctx.Source())));
}
return uses;
}
+1 -1
View File
@@ -122,7 +122,7 @@ namespace lsp::language::ast::detail
// ===== 基础辅助函数 =====
TSNode FindChildByType(TSNode parent, const std::string& type);
bool IsSyntaxErrorNode(TSNode node);
std::vector<ParseError> CollectSyntaxErrors(TSNode node, const std::string& source);
std::vector<ParseError> CollectSyntaxErrors(TSNode node, std::string_view source);
// 访问修饰符和方法修饰符解析
AccessModifier ParseAccessModifier(TSNode node, ParseContext& ctx);
@@ -2,7 +2,7 @@
namespace lsp::language::ast::ts
{
std::string Text(TSNode node, const std::string& source)
std::string Text(TSNode node, std::string_view source)
{
uint32_t start = ts_node_start_byte(node);
uint32_t end = ts_node_end_byte(node);
@@ -10,7 +10,7 @@ namespace lsp::language::ast::ts
if (start >= end || end > source.length())
return "";
return source.substr(start, end - start);
return std::string(source.substr(start, end - start));
}
Location NodeLocation(TSNode node)
@@ -8,7 +8,7 @@ extern "C" {
namespace lsp::language::ast::ts
{
std::string Text(TSNode node, const std::string& source);
std::string Text(TSNode node, std::string_view source);
Location NodeLocation(TSNode node);
+15 -2
View File
@@ -23,6 +23,7 @@ namespace lsp::language::ast
// ===== 节点类型枚举 =====
enum class NodeKind
{
kProgram,
// Expressions
kIdentifier,
kLiteral,
@@ -244,6 +245,7 @@ namespace lsp::language::ast
// ===== 前向声明 =====
class ASTNode;
class Program;
class Expression;
class Statement;
class Declaration;
@@ -310,6 +312,7 @@ namespace lsp::language::ast
public:
virtual ~ASTVisitor() = default;
virtual void VisitProgram(Program& node) = 0;
virtual void VisitUnitDefinition(UnitDefinition& node) = 0;
virtual void VisitMethodDeclaration(MethodDeclaration& node) = 0;
virtual void VisitPropertyDeclaration(PropertyDeclaration& node) = 0;
@@ -864,14 +867,14 @@ namespace lsp::language::ast
ExpressionPtr value;
};
class UsesStatement: public Statement
class UsesStatement : public Statement
{
public:
UsesStatement() { kind = NodeKind::kUsesStatement; }
void Accept(ASTVisitor& visitor) override { visitor.VisitUsesStatement(*this); }
public:
std::vector<std::string> modules;
std::vector<std::string> units;
};
struct Parameter
@@ -1017,6 +1020,16 @@ namespace lsp::language::ast
std::string operator_symbol;
};
class Program : public ASTNode
{
public:
Program() { kind = NodeKind::kProgram; }
void Accept(ASTVisitor& visitor) override { visitor.VisitProgram(*this); }
public:
std::vector<StatementPtr> statements;
};
template<typename T, typename... Args>
std::unique_ptr<T> MakeNode(Args&&... args)
{