Skip to content

Commit

Permalink
Refactor ast node tree generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Nov 24, 2024
1 parent 4f83557 commit 95eba64
Show file tree
Hide file tree
Showing 19 changed files with 2,064 additions and 1,771 deletions.
605 changes: 60 additions & 545 deletions server/internal/lsp/ast/ast.go

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions server/internal/lsp/ast/ast_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
// ASTBaseNodeBuilder
// --
type ASTBaseNodeBuilder struct {
bn ASTBaseNode
bn NodeAttributes
}

func NewBaseNodeBuilder(kind NodeType) *ASTBaseNodeBuilder {
func NewBaseNodeBuilder() *ASTBaseNodeBuilder {
return &ASTBaseNodeBuilder{
bn: ASTBaseNode{Kind: kind},
bn: NodeAttributes{},
}
}

func NewBaseNodeFromSitterNode(kind NodeType, node *sitter.Node) ASTBaseNode {
builder := NewBaseNodeBuilder(kind).
func NewBaseNodeFromSitterNode(node *sitter.Node) NodeAttributes {
builder := NewBaseNodeBuilder().
WithSitterPos(node)

return builder.Build()
}
func (d *ASTBaseNodeBuilder) Build() ASTBaseNode {
func (d *ASTBaseNodeBuilder) Build() NodeAttributes {
return d.bn
}

Expand Down Expand Up @@ -55,14 +55,14 @@ func (d *ASTBaseNodeBuilder) WithStartEnd(startRow uint, startCol uint, endRow u
// IdentifierBuilder
// --
type IdentifierBuilder struct {
bi Identifier
bi *Ident
bn ASTBaseNodeBuilder
}

func NewIdentifierBuilder() *IdentifierBuilder {
return &IdentifierBuilder{
bi: Identifier{},
bn: *NewBaseNodeBuilder(TypeIdentifier),
bi: &Ident{},
bn: *NewBaseNodeBuilder(),
}
}

Expand All @@ -71,7 +71,7 @@ func (i *IdentifierBuilder) WithName(name string) *IdentifierBuilder {
return i
}
func (i *IdentifierBuilder) WithPath(path string) *IdentifierBuilder {
i.bi.Path = path
i.bi.ModulePath = path
return i
}

Expand All @@ -85,11 +85,11 @@ func (i *IdentifierBuilder) WithStartEnd(startRow uint, startCol uint, endRow ui
return i
}

func (i *IdentifierBuilder) Build() Identifier {
func (i *IdentifierBuilder) Build() Ident {
ident := i.bi
ident.ASTBaseNode = i.bn.Build()
ident.NodeAttributes = i.bn.Build()

return ident
return *ident
}

// --
Expand All @@ -102,7 +102,7 @@ type TypeInfoBuilder struct {
func NewTypeInfoBuilder() *TypeInfoBuilder {
return &TypeInfoBuilder{
t: TypeInfo{
ASTBaseNode: ASTBaseNode{Kind: TypeTypeInfo},
NodeAttributes: NodeAttributes{},
},
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (b *TypeInfoBuilder) WithName(name string) *TypeInfoBuilder {
}

func (b *TypeInfoBuilder) WithPath(path string) *TypeInfoBuilder {
b.t.Identifier.Path = path
b.t.Identifier.ModulePath = path

return b
}
Expand All @@ -161,8 +161,8 @@ func (b *TypeInfoBuilder) WithNameStartEnd(startRow uint, startCol uint, endRow
}

func (b *TypeInfoBuilder) WithStartEnd(startRow uint, startCol uint, endRow uint, endCol uint) *TypeInfoBuilder {
b.t.ASTBaseNode.StartPos = Position{startRow, startCol}
b.t.ASTBaseNode.EndPos = Position{endRow, endCol}
b.t.NodeAttributes.StartPos = Position{startRow, startCol}
b.t.NodeAttributes.EndPos = Position{endRow, endCol}
return b
}

Expand All @@ -181,7 +181,7 @@ type DefDeclBuilder struct {
func NewDefDeclBuilder() *DefDeclBuilder {
return &DefDeclBuilder{
d: DefDecl{},
a: *NewBaseNodeBuilder(TypeDefDecl),
a: *NewBaseNodeBuilder(),
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func (b *DefDeclBuilder) WithIdentifierSitterPos(node *sitter.Node) *DefDeclBuil

func (b *DefDeclBuilder) Build() DefDecl {
def := b.d
def.ASTBaseNode = b.a.Build()
def.NodeAttributes = b.a.Build()

return def
}
135 changes: 135 additions & 0 deletions server/internal/lsp/ast/ast_declaration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ast

import (
"github.com/pherrymason/c3-lsp/pkg/option"
"go/token"
)

// ----------------------------------------------------------------------------
// Declarations

type (
// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
Spec interface {
Node
specNode()
}

ImportSpec struct {
NodeAttributes
Path string
}

ValueSpec struct {
Names []*Ident
Type Expression // value type, or nil
Values []Expression // initial values, or nil
}

// TypeSpec represents declarations of types like aliases, definition of types
// or parametrized types (generics)
TypeSpec struct {
Name *Ident // type name
TypeParams []Expression // type parameters; or nil
Assign token.Pos // position of '=', if any
Type Expression // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
}
)

func (*ImportSpec) specNode() {}
func (*ValueSpec) specNode() {}

const (
StructTypeNormal = iota
StructTypeUnion
StructTypeBitStruct
)

type StructType int

type (
// VariableDecl
// Deprecated use GenDecl with Token as token.VAR
VariableDecl struct {
NodeAttributes
Names []Ident
Type TypeInfo
Initializer Expression
}

// ConstDecl
// Deprecated use GenDecl with Token as token.CONST
ConstDecl struct {
NodeAttributes
Names []Ident
Type option.Option[TypeInfo]
Initializer Expression
}

GenDecl struct {
NodeAttributes
Token token.Token
Specs []Spec
}

EnumDecl struct {
NodeAttributes
Name string
BaseType TypeInfo
Properties []EnumProperty
Members []EnumMember
}

FaultDecl struct {
NodeAttributes
Name Ident
BackingType option.Option[TypeInfo]
Members []FaultMember
}

MacroDecl struct {
NodeAttributes
Signature MacroSignature
Body Block
}

DefDecl struct {
NodeAttributes
Name Ident
resolvesTo string
resolvesToType option.Option[TypeInfo]
}

StructDecl struct {
NodeAttributes
Name string
BackingType option.Option[TypeInfo]
Members []StructMemberDecl
StructType StructType
Implements []string
}

FunctionDecl struct {
NodeAttributes
ParentTypeId option.Option[Ident]
Signature FunctionSignature
Body Node
}

InterfaceDecl struct {
NodeAttributes
Name Ident
Methods []FunctionSignature
}
)

func (v *VariableDecl) declNode() {}
func (v *ConstDecl) declNode() {}
func (v *EnumDecl) declNode() {}
func (v *FaultDecl) declNode() {}
func (v *StructDecl) declNode() {}
func (v *DefDecl) declNode() {}
func (v *MacroDecl) declNode() {}

func (v *FunctionDecl) declNode() {}
func (v *InterfaceDecl) declNode() {}
Loading

0 comments on commit 95eba64

Please sign in to comment.