Skip to content

Commit f5a0788

Browse files
committed
Remove remaining debug calls.
Remove VariableDecl and ConstDecl in favour of using GenDecl. BuildSymbolTable finds structs, faults and constants.
1 parent 8305484 commit f5a0788

10 files changed

+77
-64
lines changed

server/internal/lsp/analysis/symbol_table.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ func (s *SymbolTable) RegisterType(typeDecl *ast.GenDecl, currentModule ast.Modu
7070
})
7171
}
7272

73+
func (s *SymbolTable) RegisterStruct(n *ast.StructDecl, currentModule ast.Module) {
74+
s.symbols = append(s.symbols, Symbol{
75+
Name: n.Name,
76+
Module: []string{currentModule.Name},
77+
NodeDecl: n,
78+
Range: n.Range,
79+
})
80+
}
81+
82+
func (s *SymbolTable) RegisterFault(n *ast.FaultDecl, currentModule ast.Module) {
83+
s.symbols = append(s.symbols, Symbol{
84+
Name: n.Name.Name,
85+
Module: []string{currentModule.Name},
86+
NodeDecl: n,
87+
Range: n.Range,
88+
})
89+
}
90+
7391
type SymbolID uint
7492

7593
type Symbol struct {
@@ -118,13 +136,18 @@ func (v *symbolTableGenerator) Enter(node ast.Node) walk.Visitor {
118136
}
119137

120138
case *ast.GenDecl:
121-
if n.Token == ast.VAR {
139+
if n.Token == ast.VAR || n.Token == ast.CONST {
122140
v.table.RegisterVariable(n, v.currentModule)
123-
} else if n.Token == ast.ENUM || n.Token == ast.STRUCT {
141+
} else if n.Token == ast.ENUM {
124142
v.table.RegisterType(n, v.currentModule)
125143
}
126-
}
127144

145+
case *ast.StructDecl:
146+
v.table.RegisterStruct(n, v.currentModule)
147+
148+
case *ast.FaultDecl:
149+
v.table.RegisterFault(n, v.currentModule)
150+
}
128151
return v
129152
}
130153

server/internal/lsp/analysis/symbol_table_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ func TestConvertToAST_registers_global_declarations(t *testing.T) {
1313
char dog = 2;
1414
enum Colors:int { RED, BLUE, GREEN }
1515
struct MyStruct { int data; }
16+
fault Err{OOPS,FIAL}
17+
const int A_CONSTANT = 12;
1618
`
1719

1820
tree := factory.ConvertToAST(factory.GetCST(source), source, "file.c3")
1921

2022
result := BuildSymbolTable(tree)
2123

22-
assert.Equal(t, 4, len(result.symbols))
24+
assert.Equal(t, 6, len(result.symbols))
2325
assert.Equal(t, "cat", result.symbols[0].Name)
2426
assert.Equal(t, "dog", result.symbols[1].Name)
2527
assert.Equal(t, "Colors", result.symbols[2].Name)
2628
assert.Equal(t, "MyStruct", result.symbols[3].Name)
29+
assert.Equal(t, "Err", result.symbols[4].Name)
30+
assert.Equal(t, "A_CONSTANT", result.symbols[5].Name)
2731
}

server/internal/lsp/ast/ast.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
VAR
2727
CONST
2828
STRUCT
29+
UNION
2930
ENUM
3031
FAULT
3132
)

server/internal/lsp/ast/ast_declaration.go

-20
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,6 @@ const (
5151
type StructType int
5252

5353
type (
54-
55-
// VariableDecl
56-
// Deprecated use GenDecl with Token as token.VAR
57-
VariableDecl struct {
58-
NodeAttributes
59-
Names []*Ident
60-
Type TypeInfo
61-
Initializer Expression
62-
}
63-
64-
// ConstDecl
65-
// Deprecated use GenDecl with Token as token.CONST
66-
ConstDecl struct {
67-
NodeAttributes
68-
Names []*Ident
69-
Type option.Option[TypeInfo]
70-
Initializer Expression
71-
}
72-
7354
GenDecl struct {
7455
NodeAttributes
7556
Token Token // const, variable
@@ -120,7 +101,6 @@ type (
120101
)
121102

122103
func (v *GenDecl) declNode() {}
123-
func (v *ConstDecl) declNode() {}
124104
func (v *FaultDecl) declNode() {}
125105
func (v *StructDecl) declNode() {}
126106
func (v *DefDecl) declNode() {}

server/internal/lsp/ast/ast_visitors.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type ASTVisitor interface {
1515
// Declarations
1616

1717
VisitFunctionDecl(node *FunctionDecl)
18-
VisitConstDeclaration(node *ConstDecl)
1918
VisitStructDecl(node *StructDecl)
2019
VisitFaultDecl(node *FaultDecl)
2120
VisitDefDecl(node *DefDecl)

server/internal/lsp/ast/factory/convert.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,21 @@ func convert_fault_declaration(node *sitter.Node, sourceCode []byte) ast.Declara
520520
}
521521

522522
func convert_const_declaration(node *sitter.Node, source []byte) ast.Declaration {
523-
constant := &ast.ConstDecl{
524-
Names: []*ast.Ident{},
523+
constant := &ast.GenDecl{
524+
Token: ast.Token(ast.CONST),
525525
NodeAttributes: ast.NewNodeAttributesBuilder().
526526
WithSitterPos(node).
527527
Build(),
528528
}
529+
valueSpec := &ast.ValueSpec{}
529530

530531
var idNode *sitter.Node
531532

532533
for i := 0; i < int(node.ChildCount()); i++ {
533534
n := node.Child(i)
534535
switch n.Type() {
535536
case "type":
536-
constant.Type = option.Some(convert_type(n, source))
537+
valueSpec.Type = convert_type(n, source)
537538

538539
case "const_ident":
539540
idNode = n
@@ -543,7 +544,7 @@ func convert_const_declaration(node *sitter.Node, source []byte) ast.Declaration
543544
}
544545
}
545546

546-
constant.Names = append(constant.Names,
547+
valueSpec.Names = append(valueSpec.Names,
547548
ast.NewIdentifierBuilder().
548549
WithName(idNode.Content(source)).
549550
WithSitterPos(idNode).
@@ -552,8 +553,10 @@ func convert_const_declaration(node *sitter.Node, source []byte) ast.Declaration
552553

553554
right := node.ChildByFieldName("right")
554555
if right != nil {
555-
constant.Initializer = convert_expression(right, source).(ast.Expression)
556+
expr := convert_expression(right, source).(ast.Expression)
557+
valueSpec.Value = expr
556558
}
559+
constant.Spec = valueSpec
557560

558561
return constant
559562
}
@@ -1276,7 +1279,7 @@ func convert_type_access_expr(node *sitter.Node, source []byte) ast.Expression {
12761279
}
12771280

12781281
func convert_field_expr(node *sitter.Node, source []byte) ast.Expression {
1279-
debugNode(node, source, "field_expr")
1282+
//debugNode(node, source, "field_expr")
12801283
argument := node.ChildByFieldName("argument")
12811284
var argumentNode ast.Expression
12821285
if argument.Type() == "ident" {

server/internal/lsp/ast/factory/convert_rules_exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func anyOf(name string, rules []NodeRule, node *sitter.Node, source []byte, debu
202202
func commaSep(convert nodeConverter, node *sitter.Node, source []byte) []ast.Node {
203203
var nodes []ast.Node
204204
for {
205-
debugNode(node, source, "commaSep")
205+
//debugNode(node, source, "commaSep")
206206
condition := convert(node, source)
207207

208208
if condition != nil {

server/internal/lsp/ast/factory/convert_statement.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package factory
22

33
import (
4-
"fmt"
54
"github.com/pherrymason/c3-lsp/internal/lsp/ast"
65
"github.com/pherrymason/c3-lsp/pkg"
76
"log"
@@ -103,7 +102,7 @@ func convert_declaration_stmt(node *sitter.Node, source []byte) ast.Statement {
103102
valueSpec := &ast.ValueSpec{}
104103
for i := 0; i < int(node.ChildCount()) && !end; i++ {
105104
n := node.Child(i)
106-
debugNode(n, source, "dd")
105+
//debugNode(n, source, "dd")
107106

108107
switch n.Type() {
109108
case "local_decl_storage":
@@ -499,7 +498,7 @@ func convert_foreach_stmt(node *sitter.Node, source []byte) ast.Statement {
499498
func convert_foreach_var(node *sitter.Node, source []byte) ast.ForeachValue {
500499
value := ast.ForeachValue{}
501500

502-
debugNode(node, source, fmt.Sprint(node.ChildCount()))
501+
//debugNode(node, source, fmt.Sprint(node.ChildCount()))
503502
for i := 0; i < int(node.ChildCount()); i++ {
504503
n := node.Child(i)
505504
switch n.Type() {

server/internal/lsp/ast/factory/convert_statement_test.go

+33-24
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,50 @@ func TestConvertToAST_declaration_stmt_constant(t *testing.T) {
1717
}{
1818
{
1919
input: "const int I;",
20-
expected: &ast.ConstDecl{
20+
expected: &ast.GenDecl{
21+
Token: ast.Token(ast.CONST),
2122
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(1, 3, 1, 15).Build(),
22-
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 13, 1, 14).BuildPtr()},
23-
Type: option.Some(ast.NewTypeInfoBuilder().
24-
WithName("int").
25-
IsBuiltin().
26-
WithStartEnd(1, 9, 1, 12).
27-
WithNameStartEnd(1, 9, 1, 12).
28-
Build()),
23+
Spec: &ast.ValueSpec{
24+
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 13, 1, 14).BuildPtr()},
25+
Type: ast.NewTypeInfoBuilder().
26+
WithName("int").
27+
IsBuiltin().
28+
WithStartEnd(1, 9, 1, 12).
29+
WithNameStartEnd(1, 9, 1, 12).
30+
Build(),
31+
},
2932
},
3033
},
3134
{
3235
input: "const int I = 1;", // With initialization
33-
expected: &ast.ConstDecl{
36+
expected: &ast.GenDecl{
37+
Token: ast.Token(ast.CONST),
3438
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(1, 3, 1, 19).Build(),
35-
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 13, 1, 14).BuildPtr()},
36-
Type: option.Some(ast.NewTypeInfoBuilder().
37-
WithName("int").
38-
IsBuiltin().
39-
WithStartEnd(1, 9, 1, 12).
40-
WithNameStartEnd(1, 9, 1, 12).
41-
Build()),
42-
Initializer: &ast.BasicLit{
43-
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(1, 17, 1, 18).Build(),
44-
Kind: ast.INT,
45-
Value: "1",
39+
Spec: &ast.ValueSpec{
40+
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 13, 1, 14).BuildPtr()},
41+
Type: ast.NewTypeInfoBuilder().
42+
WithName("int").
43+
IsBuiltin().
44+
WithStartEnd(1, 9, 1, 12).
45+
WithNameStartEnd(1, 9, 1, 12).
46+
Build(),
47+
Value: &ast.BasicLit{
48+
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(1, 17, 1, 18).Build(),
49+
Kind: ast.INT,
50+
Value: "1",
51+
},
4652
},
4753
},
4854
},
4955
{
5056
input: "const I;", // Without type
51-
expected: &ast.ConstDecl{
57+
expected: &ast.GenDecl{
58+
Token: ast.Token(ast.CONST),
5259
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(1, 3, 1, 11).Build(),
53-
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 9, 1, 10).BuildPtr()},
54-
Type: option.None[ast.TypeInfo](),
60+
Spec: &ast.ValueSpec{
61+
Names: []*ast.Ident{ast.NewIdentifierBuilder().WithName("I").WithStartEnd(1, 9, 1, 10).BuildPtr()},
62+
Type: nil,
63+
},
5564
},
5665
},
5766
}
@@ -66,7 +75,7 @@ func TestConvertToAST_declaration_stmt_constant(t *testing.T) {
6675

6776
tree := ConvertToAST(GetCST(source), source, "file.c3")
6877

69-
varDecl := tree.Modules[0].Declarations[0].(*ast.ConstDecl)
78+
varDecl := tree.Modules[0].Declarations[0].(*ast.GenDecl)
7079
assert.Equal(t, tt.expected, varDecl)
7180
})
7281
}

server/internal/lsp/ast/json_visitor.go

-5
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ func (v *JSONVisitor) VisitDeclaration(node Declaration) {
7373
v.VisitFunctionDecl(node.(*FunctionDecl))
7474
}
7575
}
76-
77-
func (v *JSONVisitor) VisitConstDeclaration(node *ConstDecl) {
78-
79-
}
80-
8176
func (v *JSONVisitor) VisitStructDecl(node *StructDecl) {
8277

8378
}

0 commit comments

Comments
 (0)