-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbash.go
More file actions
161 lines (140 loc) · 4.84 KB
/
Copy pathbash.go
File metadata and controls
161 lines (140 loc) · 4.84 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
package languages
import (
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
sitter "github.com/zzet/gortex/internal/parser/tsitter"
"github.com/zzet/gortex/internal/parser/tsitter/bash"
)
// bashQAll is a single tree-sitter query alternating over every
// pattern the Bash extractor needs — function definitions, top-level
// variable assignments, and command calls. One cursor walk per file
// replaces the three independent EachMatch passes the previous design
// made. Capture names are disjoint across patterns so the dispatch in
// Extract branches on which one is set.
const bashQAll = `
[
(function_definition
name: (word) @func.name) @func.def
(variable_assignment
name: (variable_name) @var.name) @var.def
(command
name: (command_name) @cmd.name) @cmd.expr
]
`
// BashExtractor extracts Bash/Shell source files. A single precompiled
// alternation query drives one cursor walk per file.
type BashExtractor struct {
lang *sitter.Language
qAll *parser.PreparedQuery
}
func NewBashExtractor() *BashExtractor {
lang := bash.GetLanguage()
return &BashExtractor{
lang: lang,
qAll: parser.MustPreparedQuery(bashQAll, lang),
}
}
func (e *BashExtractor) Language() string { return "bash" }
func (e *BashExtractor) Extensions() []string { return []string{".sh", ".bash", ".zsh"} }
// bashDeferredCmd is a command call site held back until the single
// walk completes — command attribution needs funcRanges, which is
// built from the function nodes emitted during that same walk.
type bashDeferredCmd struct {
name string
line int
}
func (e *BashExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
tree, err := parser.ParseFile(src, e.lang)
if err != nil {
return nil, err
}
defer tree.Close()
root := tree.RootNode()
result := &parser.ExtractionResult{}
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: int(root.EndPoint().Row) + 1,
Language: "bash",
}
fileID := fileNode.ID
result.Nodes = append(result.Nodes, fileNode)
seen := make(map[string]bool)
var cmds []bashDeferredCmd
parser.EachMatch(e.qAll, root, src, func(m parser.QueryResult) {
switch {
case m.Captures["func.def"] != nil:
name := m.Captures["func.name"].Text
def := m.Captures["func.def"]
id := filePath + "::" + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindFunction, Name: name,
FilePath: filePath, StartLine: def.StartLine + 1, EndLine: def.EndLine + 1,
Language: "bash", Meta: map[string]any{"signature": name + "()"},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines, FilePath: filePath, Line: def.StartLine + 1,
})
case m.Captures["var.def"] != nil:
name := m.Captures["var.name"].Text
def := m.Captures["var.def"]
// Only top-level: parent is program.
if def.Node == nil || def.Node.Parent() == nil || def.Node.Parent().Type() != "program" {
return
}
id := filePath + "::" + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: name,
FilePath: filePath, StartLine: def.StartLine + 1, EndLine: def.EndLine + 1,
Language: "bash",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines, FilePath: filePath, Line: def.StartLine + 1,
})
case m.Captures["cmd.expr"] != nil:
cmdName := m.Captures["cmd.name"].Text
expr := m.Captures["cmd.expr"]
// Source/dot imports (`source foo.sh` / `. foo.sh`)
// attach to the file node, so they emit during the walk —
// no funcRanges needed.
if cmdName == "source" || cmdName == "." {
cmdNode := expr.Node
if cmdNode != nil && cmdNode.NamedChildCount() >= 2 {
arg := cmdNode.NamedChild(1)
if arg != nil {
importPath := strings.Trim(arg.Content(src), "\"'")
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: "unresolved::import::" + importPath,
Kind: graph.EdgeImports, FilePath: filePath, Line: expr.StartLine + 1,
})
}
}
return
}
// Regular command call — deferred until funcRanges is built.
cmds = append(cmds, bashDeferredCmd{name: cmdName, line: expr.StartLine + 1})
}
})
// Command attribution needs the enclosing function, so it runs
// after the single walk has emitted every function node.
funcRanges := buildFuncRanges(result)
for _, c := range cmds {
callerID := findEnclosingFunc(funcRanges, c.line)
if callerID == "" {
continue
}
result.Edges = append(result.Edges, &graph.Edge{
From: callerID, To: "unresolved::" + c.name,
Kind: graph.EdgeCalls, FilePath: filePath, Line: c.line,
})
}
return result, nil
}