-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathc.go
128 lines (100 loc) · 2.66 KB
/
c.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
package deps
import (
"context"
"fmt"
"io"
"regexp"
"strings"
"github.com/wakatime/wakatime-cli/pkg/file"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
)
var cExcludeRegex = regexp.MustCompile(`(?i)^(stdio\.h|stdlib\.h|string\.h|time\.h)$`)
// StateC is a token parsing state.
type StateC int
const (
// StateCUnknown represents a unknown token parsing state.
StateCUnknown StateC = iota
// StateCImport means we are in import section during token parsing.
StateCImport
)
// ParserC is a dependency parser for the C programming language.
// It is not thread safe.
type ParserC struct {
State StateC
Output []string
}
// Parse parses dependencies from C file content using the C lexer.
func (p *ParserC) Parse(ctx context.Context, filepath string) ([]string, error) {
logger := log.Extract(ctx)
reader, err := file.OpenNoLock(filepath) // nolint:gosec
if err != nil {
return nil, fmt.Errorf("failed to open file %q: %s", filepath, err)
}
defer func() {
if err := reader.Close(); err != nil {
logger.Debugf("failed to close file: %s", err)
}
}()
p.init()
defer p.init()
data, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("failed to read from reader: %s", err)
}
l := lexers.Get(heartbeat.LanguageC.String())
if l == nil {
return nil, fmt.Errorf("failed to get lexer for %s", heartbeat.LanguageC.String())
}
iter, err := l.Tokenise(nil, string(data))
if err != nil {
return nil, fmt.Errorf("failed to tokenize file content: %s", err)
}
for _, token := range iter.Tokens() {
p.processToken(token)
}
return p.Output, nil
}
func (p *ParserC) append(dep string) {
// only consider first part of an import path
dep = strings.Split(dep, "/")[0]
if len(dep) == 0 {
return
}
dep = strings.TrimSpace(dep)
if cExcludeRegex.MatchString(dep) {
return
}
// trim extension
dep = strings.TrimSuffix(dep, ".h")
p.Output = append(p.Output, dep)
}
func (p *ParserC) init() {
p.Output = nil
p.State = StateCUnknown
}
func (p *ParserC) processToken(token chroma.Token) {
switch token.Type {
case chroma.CommentPreproc:
p.processCommentPreproc(token.Value)
case chroma.CommentPreprocFile:
p.processCommentPreprocFile(token.Value)
}
}
func (p *ParserC) processCommentPreproc(value string) {
if strings.HasPrefix(strings.TrimSpace(value), "include") {
p.State = StateCImport
}
}
func (p *ParserC) processCommentPreprocFile(value string) {
if p.State != StateCImport {
return
}
if value != "\n" && value != "#" {
value = strings.Trim(value, `"<> `)
p.append(value)
}
p.State = StateCUnknown
}