improve ast

This commit is contained in:
csh
2025-10-26 22:51:52 +08:00
parent dbccd5b605
commit 7d9b966bc7
7 changed files with 2124 additions and 2224 deletions
+3 -5
View File
@@ -1,4 +1,3 @@
#include <iostream>
#include "./detail.hpp"
#include "./tree_sitter_utils.hpp"
#include "./deserializer.hpp"
@@ -19,8 +18,7 @@ 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";
program->span = ts::NodeLocation(root);
uint32_t count = ts_node_child_count(root);
for (uint32_t i = 0; i < count; i++)
@@ -53,8 +51,7 @@ 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";
program->span = ts::NodeLocation(root);
uint32_t count = ts_node_child_count(root);
for (uint32_t i = 0; i < count; i++)
@@ -75,6 +72,7 @@ namespace lsp::language::ast
program->statements.push_back(std::move(stmt));
}
result.result.root = std::move(program);
auto syntax_errors = detail::CollectSyntaxErrors(root, source);
result.result.errors.insert(result.result.errors.end(),
std::make_move_iterator(syntax_errors.begin()),
File diff suppressed because it is too large Load Diff
+62 -18
View File
@@ -91,6 +91,8 @@ namespace lsp::language::ast
kUnpackPattern,
kCaseBranch,
kClassMember,
kMatrixIterationStatement,
};
// ===== 枚举类型 =====
@@ -300,6 +302,7 @@ namespace lsp::language::ast
class ClassDefinition;
class UnitDefinition;
class TSSQLExpression;
class MatrixIterationStatement;
using ASTNodePtr = std::unique_ptr<ASTNode>;
using ExpressionPtr = std::unique_ptr<Expression>;
@@ -362,6 +365,13 @@ namespace lsp::language::ast
virtual void VisitFieldDeclaration(FieldDeclaration& node) = 0;
virtual void VisitTSSQLExpression(TSSQLExpression& node) = 0;
virtual void VisitUnpackPattern(UnpackPattern& node) = 0;
virtual void VisitMatrixIterationStatement(MatrixIterationStatement& node) = 0;
};
struct TypeAnnotation
{
std::string name;
Location location;
};
// ===== 基类 ASTNode =====
@@ -373,8 +383,7 @@ namespace lsp::language::ast
public:
NodeKind kind;
Location location;
std::string node_type_name;
Location span;
};
// ===== Expression 基类 =====
@@ -395,6 +404,7 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
};
class Literal : public Expression
@@ -405,7 +415,8 @@ namespace lsp::language::ast
public:
LiteralKind literal_kind;
std::string value;
std::string name;
Location location;
};
class BinaryExpression : public Expression
@@ -466,8 +477,8 @@ namespace lsp::language::ast
struct Argument
{
std::optional<std::string> name;
std::optional<Location> location;
ExpressionPtr value;
bool is_spread = false;
};
CallExpression() { kind = NodeKind::kCallExpression; }
@@ -485,8 +496,9 @@ namespace lsp::language::ast
void Accept(ASTVisitor& visitor) override { visitor.VisitAttributeExpression(*this); }
public:
std::string name;
Location location;
ExpressionPtr object;
std::string attribute;
};
class SubscriptExpression : public Expression
@@ -594,7 +606,8 @@ namespace lsp::language::ast
public:
std::string name;
std::optional<std::string> type_name;
Location location;
std::optional<TypeAnnotation> type;
ExpressionPtr initial_value;
};
@@ -605,10 +618,11 @@ namespace lsp::language::ast
void Accept(ASTVisitor& visitor) override { visitor.VisitStaticDeclaration(*this); }
public:
std::string name;
std::optional<std::string> type_name;
ExpressionPtr initial_value;
ReferenceModifier reference_modifier = ReferenceModifier::kNone;
std::string name;
Location location;
std::optional<TypeAnnotation> type;
ExpressionPtr initial_value;
};
class GlobalDeclaration : public ASTNode
@@ -619,7 +633,8 @@ namespace lsp::language::ast
public:
std::string name;
std::optional<std::string> type_name;
Location location;
std::optional<TypeAnnotation> type;
ExpressionPtr initial_value;
};
@@ -631,7 +646,7 @@ namespace lsp::language::ast
public:
std::string name;
std::optional<std::string> type_name;
std::optional<TypeAnnotation> type;
ExpressionPtr initial_value;
ReferenceModifier reference_modifier = ReferenceModifier::kNone;
};
@@ -880,17 +895,17 @@ namespace lsp::language::ast
struct Parameter
{
std::string name;
std::optional<std::string> type_name;
Location location;
std::optional<TypeAnnotation> type;
ExpressionPtr default_value;
bool is_var = false;
bool is_out = false;
Location location;
};
struct Signature
{
std::vector<Parameter> parameters;
std::optional<std::string> return_type;
std::optional<TypeAnnotation> return_type;
};
class FunctionDefinition : public Statement
@@ -901,6 +916,7 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
Signature signature;
StatementPtr body;
bool is_overload;
@@ -914,6 +930,7 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
Signature signature;
bool is_overload;
};
@@ -937,13 +954,13 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
Signature signature;
StatementPtr body;
MethodModifier modifier = MethodModifier::kNone;
MethodType method_type = MethodType::kNormal;
bool is_class_method = false;
bool is_operator_overload = false;
std::string operator_symbol;
};
class PropertyDeclaration : public Statement
@@ -954,6 +971,7 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
std::optional<std::string> type_name;
std::optional<std::string> index_value;
std::optional<std::string> read_accessor;
@@ -982,9 +1000,19 @@ namespace lsp::language::ast
ClassDefinition() { kind = NodeKind::kClassDefinition; }
void Accept(ASTVisitor& visitor) override { visitor.VisitClassDefinition(*this); }
struct ParentClass
{
std::optional<std::string> qualifier;
std::optional<Location> qualifier_location;
std::string name;
Location location;
};
public:
std::string name;
std::vector<std::string> parent_classes;
Location location;
std::vector<ParentClass> parent_classes;
std::vector<std::unique_ptr<ClassMember>> members;
};
@@ -996,6 +1024,7 @@ namespace lsp::language::ast
public:
std::string name;
Location location;
std::vector<StatementPtr> interface_statements;
std::vector<StatementPtr> implementation_statements;
std::vector<StatementPtr> initialization_statements;
@@ -1009,15 +1038,30 @@ namespace lsp::language::ast
void Accept(ASTVisitor& visitor) override { visitor.VisitExternalMethodDefinition(*this); }
public:
std::string owner_class;
struct
{
std::string name;
Location location;
} owner_class;
std::string name;
Location location;
Signature signature;
StatementPtr body;
MethodModifier modifier = MethodModifier::kNone;
MethodType method_type = MethodType::kNormal;
bool is_operator_overload = false;
bool is_class_method = false;
std::string operator_symbol;
};
class MatrixIterationStatement : public Statement
{
public:
MatrixIterationStatement() { kind = NodeKind::kMatrixIterationStatement; }
void Accept(ASTVisitor& visitor) override { visitor.VisitMatrixIterationStatement(*this); }
public:
ExpressionPtr target;
StatementPtr body;
};
class Program : public ASTNode