-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathclojure.go
More file actions
222 lines (202 loc) · 6.68 KB
/
Copy pathclojure.go
File metadata and controls
222 lines (202 loc) · 6.68 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package languages
import (
"regexp"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
)
var (
clojureNsRe = regexp.MustCompile(`(?m)\(ns\s+([\w.\-]+)`)
clojureDefnRe = regexp.MustCompile(`(?m)\(defn-?\s+([\w\-!?*+<>=]+)`)
clojureMacroRe = regexp.MustCompile(`(?m)\(defmacro\s+([\w\-!?*+<>=]+)`)
clojureRecordRe = regexp.MustCompile(`(?m)\(defrecord\s+(\w+)`)
clojureTypeRe = regexp.MustCompile(`(?m)\(deftype\s+(\w+)`)
clojureProtoRe = regexp.MustCompile(`(?m)\(defprotocol\s+(\w+)`)
clojureRequireRe = regexp.MustCompile(`(?m)(?:\(require|\(:require|\(use|\(:import)\s+[\[\s]*(?:\[?\s*)?(['\s]?[\w.\-]+)`)
clojureCallRe = regexp.MustCompile(`\(([\w\-!?*+<>=]+)[\s)]`)
)
// ClojureExtractor extracts Clojure source files using regex.
type ClojureExtractor struct{}
func NewClojureExtractor() *ClojureExtractor { return &ClojureExtractor{} }
func (e *ClojureExtractor) Language() string { return "clojure" }
func (e *ClojureExtractor) Extensions() []string { return []string{".clj", ".cljs", ".cljc", ".edn"} }
func (e *ClojureExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
lines := strings.Split(string(src), "\n")
result := &parser.ExtractionResult{}
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: len(lines),
Language: "clojure",
}
result.Nodes = append(result.Nodes, fileNode)
seen := make(map[string]bool)
// Namespace
if m := clojureNsRe.FindSubmatchIndex(src); m != nil {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
id := filePath + "::" + name
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindPackage, Name: name,
FilePath: filePath, StartLine: line, EndLine: line,
Language: "clojure",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
seen[id] = true
}
// Functions (defn, defn-)
for _, m := range clojureDefnRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
endLine := clojureFormEnd(lines, line)
id := filePath + "::" + name
if seen[id] {
continue
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindFunction, Name: name,
FilePath: filePath, StartLine: line, EndLine: endLine,
Language: "clojure",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
}
// Macros
for _, m := range clojureMacroRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
id := filePath + "::" + name
if seen[id] {
continue
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindFunction, Name: name,
FilePath: filePath, StartLine: line, EndLine: line,
Language: "clojure", Meta: map[string]any{"macro": true},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
}
// Types: defrecord, deftype, defprotocol
for _, re := range []*regexp.Regexp{clojureRecordRe, clojureTypeRe, clojureProtoRe} {
for _, m := range re.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
kind := graph.KindType
if re == clojureProtoRe {
kind = graph.KindInterface
}
id := filePath + "::" + name
if seen[id] {
continue
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: kind, Name: name,
FilePath: filePath, StartLine: line, EndLine: line,
Language: "clojure",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
}
}
// Variables (def, excluding defn/defmacro/defrecord/deftype/defprotocol)
defAllRe := regexp.MustCompile(`(?m)\(def\s+([\w\-!?*+<>=]+)`)
for _, m := range defAllRe.FindAllSubmatchIndex(src, -1) {
// Check that "def" is not followed by n, macro, record, type, protocol
afterDef := string(src[m[0]+4 : m[2]])
if strings.HasPrefix(strings.TrimSpace(afterDef), "n") ||
strings.HasPrefix(strings.TrimSpace(afterDef), "macro") ||
strings.HasPrefix(strings.TrimSpace(afterDef), "record") ||
strings.HasPrefix(strings.TrimSpace(afterDef), "type") ||
strings.HasPrefix(strings.TrimSpace(afterDef), "protocol") {
continue
}
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
id := filePath + "::" + name
if seen[id] {
continue
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: name,
FilePath: filePath, StartLine: line, EndLine: line,
Language: "clojure",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
}
// Imports
for _, m := range clojureRequireRe.FindAllSubmatchIndex(src, -1) {
mod := strings.TrimLeft(string(src[m[2]:m[3]]), "' ")
if mod == "" {
continue
}
line := lineAt(src, m[0])
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: "unresolved::import::" + mod,
Kind: graph.EdgeImports, FilePath: filePath, Line: line,
})
}
// Call sites inside functions
funcRanges := buildFuncRanges(result)
for _, m := range clojureCallRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
if isClojureSpecialForm(name) {
continue
}
line := lineAt(src, m[0])
callerID := findEnclosingFunc(funcRanges, line)
if callerID == "" || strings.HasSuffix(callerID, "::"+name) {
continue
}
result.Edges = append(result.Edges, &graph.Edge{
From: callerID, To: "unresolved::" + name,
Kind: graph.EdgeCalls, FilePath: filePath, Line: line,
})
}
return result, nil
}
// clojureFormEnd finds the end of a top-level form by matching parens.
func clojureFormEnd(lines []string, startLine int) int {
depth := 0
for i := startLine - 1; i < len(lines); i++ {
for _, ch := range lines[i] {
switch ch {
case '(':
depth++
case ')':
depth--
if depth <= 0 {
return i + 1
}
}
}
}
return startLine
}
func isClojureSpecialForm(s string) bool {
switch s {
case "if", "do", "let", "fn", "def", "defn", "defn-", "defmacro",
"defrecord", "deftype", "defprotocol", "ns", "require", "use",
"import", "quote", "loop", "recur", "throw", "try", "catch",
"finally", "cond", "case", "when", "when-not", "when-let",
"if-let", "for", "doseq", "dotimes":
return true
}
return false
}
var _ parser.Extractor = (*ClojureExtractor)(nil)