-
Notifications
You must be signed in to change notification settings - Fork 17
/
parser.go
160 lines (128 loc) · 3.54 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package parser
import (
"context"
"fmt"
"os"
"strings"
treeutils "aspect.build/cli/gazelle/common/treesitter"
"github.com/emirpasic/gods/sets/treeset"
sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/kotlin"
)
type ParseResult struct {
File string
Imports []string
Package string
HasMain bool
}
type Parser interface {
Parse(filePath, source string) (*ParseResult, []error)
}
type treeSitterParser struct {
Parser
parser *sitter.Parser
}
func NewParser() Parser {
sitter := sitter.NewParser()
sitter.SetLanguage(kotlin.GetLanguage())
p := treeSitterParser{
parser: sitter,
}
return &p
}
var KotlinTreeSitterName = "kotlin"
var KotlinLang = kotlin.GetLanguage()
func (p *treeSitterParser) Parse(filePath, source string) (*ParseResult, []error) {
var result = &ParseResult{
File: filePath,
Imports: make([]string, 0),
}
errs := make([]error, 0)
ctx := context.Background()
sourceCode := []byte(source)
tree, err := p.parser.ParseCtx(ctx, nil, sourceCode)
if err != nil {
errs = append(errs, err)
}
if tree != nil {
rootNode := tree.RootNode()
// Extract imports from the root nodes
for i := 0; i < int(rootNode.NamedChildCount()); i++ {
nodeI := rootNode.NamedChild(i)
if nodeI.Type() == "import_list" {
for j := 0; j < int(nodeI.NamedChildCount()); j++ {
nodeJ := nodeI.NamedChild(j)
if nodeJ.Type() == "import_header" {
for k := 0; k < int(nodeJ.ChildCount()); k++ {
nodeK := nodeJ.Child(k)
if nodeK.Type() == "identifier" {
isStar := false
for l := k + 1; l < int(nodeJ.ChildCount()); l++ {
if nodeJ.Child(l).Type() == ".*" {
isStar = true
break
}
}
result.Imports = append(result.Imports, readIdentifier(nodeK, sourceCode, !isStar))
}
}
}
}
} else if nodeI.Type() == "package_header" {
if result.Package != "" {
fmt.Printf("Multiple package declarations found in %s\n", filePath)
os.Exit(1)
}
result.Package = readIdentifier(getLoneChild(nodeI, "identifier"), sourceCode, false)
} else if nodeI.Type() == "function_declaration" {
nodeJ := getLoneChild(nodeI, "simple_identifier")
if nodeJ.Content(sourceCode) == "main" {
result.HasMain = true
}
}
}
treeErrors := treeutils.QueryErrors(KotlinTreeSitterName, KotlinLang, sourceCode, rootNode)
if treeErrors != nil {
errs = append(errs, treeErrors...)
}
}
return result, errs
}
type KotlinImports struct {
imports *treeset.Set
}
func getLoneChild(node *sitter.Node, name string) *sitter.Node {
for i := 0; i < int(node.NamedChildCount()); i++ {
if node.NamedChild(i).Type() == name {
return node.NamedChild(i)
}
}
fmt.Printf("Node %v must contain node of type %q", node, name)
os.Exit(1)
return nil
}
func readIdentifier(node *sitter.Node, sourceCode []byte, ignoreLast bool) string {
if node.Type() != "identifier" {
fmt.Printf("Must be type 'identifier': %v - %s", node.Type(), node.Content(sourceCode))
os.Exit(1)
}
var s strings.Builder
total := int(node.NamedChildCount())
if ignoreLast {
total = total - 1
}
for c := 0; c < total; c++ {
nodeC := node.NamedChild(c)
// TODO: are there any other node types under an "identifier"
if nodeC.Type() == "simple_identifier" {
if s.Len() > 0 {
s.WriteString(".")
}
s.WriteString(nodeC.Content(sourceCode))
} else if nodeC.Type() != "comment" {
fmt.Printf("Unexpected node type '%v' within: %s", nodeC.Type(), node.Content(sourceCode))
os.Exit(1)
}
}
return s.String()
}