-
Notifications
You must be signed in to change notification settings - Fork 38
/
haskell.go
121 lines (98 loc) · 2.78 KB
/
haskell.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
package deps
import (
"fmt"
"io"
"os"
"strings"
"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"
)
// StateHaskell is a token parsing state.
type StateHaskell int
const (
// StateHaskellUnknown represents an unknown token parsing state.
StateHaskellUnknown StateHaskell = iota
// StateHaskellImport means we are in import section during token parsing.
StateHaskellImport
)
// ParserHaskell is a dependency parser for the Haskell programming language.
// It is not thread safe.
type ParserHaskell struct {
State StateHaskell
Output []string
}
// Parse parses dependencies from Haskell file content using the chroma Haskell lexer.
func (p *ParserHaskell) Parse(filepath string) ([]string, error) {
reader, err := os.Open(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 {
log.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.LanguageHaskell.String())
if l == nil {
return nil, fmt.Errorf("failed to get lexer for %s", heartbeat.LanguageHaskell.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 *ParserHaskell) append(dep string) {
// trim whitespaces
dep = strings.TrimSpace(dep)
// if dot separated import path, select first element
dep = strings.Split(dep, ".")[0]
// trim whitespaces
dep = strings.TrimSpace(dep)
p.Output = append(p.Output, dep)
}
func (p *ParserHaskell) init() {
p.State = StateHaskellUnknown
p.Output = []string{}
}
func (p *ParserHaskell) processToken(token chroma.Token) {
switch {
case token.Type == chroma.KeywordReserved:
p.processKeywordReserved(token.Value)
case token.Type == chroma.Keyword:
p.processKeyword(token.Value)
case token.Type == chroma.NameNamespace:
p.processNameNamespace(token.Value)
case token.Type != chroma.Text:
p.State = StateHaskellUnknown
}
}
func (p *ParserHaskell) processKeywordReserved(value string) {
switch strings.TrimSpace(value) {
case "import":
p.State = StateHaskellImport
default:
p.State = StateHaskellUnknown
}
}
func (p *ParserHaskell) processKeyword(value string) {
if p.State != StateHaskellImport || strings.TrimSpace(value) != "qualified" {
p.State = StateHaskellUnknown
}
}
func (p *ParserHaskell) processNameNamespace(value string) {
if p.State == StateHaskellImport {
p.append(value)
}
}