-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
196 lines (156 loc) · 4.12 KB
/
lex.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
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
package difflint
import (
"bufio"
"io"
"strings"
"github.com/pkg/errors"
)
type token struct {
directive directive
args []string // ["IF", "test.go:ID"] or ["END", "id"]
line int // Line number of the token.
}
type directive string
const (
directiveIf directive = "IF"
directiveEnd directive = "END"
)
type lexOptions struct {
// file is specifier that is being linted.
file string
// templates is the list of directive templates.
templates []string
}
// lex lexes the given reader and returns the list of tokens.
func lex(r io.Reader, options lexOptions) ([]token, error) {
// tokens is the list of tokens that are found in the file.
var tokens []token
// lineCount is the current line number.
var lineCount int
// Read the file line by line.
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
lineCount++
// Check if the line is a directive.
token, found, err := parseToken(line, lineCount, options.templates)
if err != nil {
return nil, err
}
if !found {
continue
}
tokens = append(tokens, *token)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return tokens, nil
}
// parseToken parses the given line and returns the token if it is a directive.
func parseToken(line string, lineNumber int, templates []string) (*token, bool, error) {
for _, template := range templates {
prefix, suffix, found := strings.Cut(template, "?")
if !found {
return nil, false, errors.New("template is missing ?")
}
if !strings.HasPrefix(line, prefix) || !strings.HasSuffix(line, suffix) {
continue
}
// Remove the prefix and suffix.
s := strings.TrimSuffix(strings.TrimPrefix(line, prefix), suffix)
args := strings.Split(s, " ")
d, err := parseDirective(args[0])
if err != nil {
return nil, false, err
}
return &token{
directive: d,
args: args[1:],
line: lineNumber,
}, true, nil
}
return nil, false, nil
}
// parseDirective parses the given string and returns the directive.
func parseDirective(s string) (directive, error) {
d := directive(s)
switch d {
case directiveIf, directiveEnd:
return d, nil
default:
return "", errors.Errorf("unknown directive %q", d)
}
}
// parseRules parses the given tokens and returns the list of rules.
func parseRules(file string, tokens []token, ranges []Range) ([]Rule, error) {
// Current rule being parsed.
r := Rule{}
var rules []Rule
for _, token := range tokens {
switch token.directive {
case directiveIf:
if r.Hunk.File != "" {
return nil, errors.New("unexpected IF directive at " + file + ":" + string(rune(token.line)))
}
targets, err := parseTargets(parseTargetsOptions{
args: token.args,
allowEmptyArgs: true,
})
if err != nil {
return nil, err
}
r.Targets = targets
r.Hunk.File = file
r.Hunk.Range = Range{Start: token.line}
case directiveEnd:
if r.Hunk.File == "" {
return nil, errors.New("unexpected END directive at " + file + ":" + string(rune(token.line)))
}
if len(token.args) == 1 {
r.ID = &(token.args[0])
}
if len(token.args) > 1 {
return nil, errors.Errorf("unexpected arguments %v", token.args)
}
r.Hunk.Range.End = token.line
for _, rng := range ranges {
if !Intersects(r.Hunk.Range, rng) {
continue
}
r.Present = true
break
}
rules = append(rules, r)
// Reset the rule.
r = Rule{}
default:
return nil, errors.Errorf("unknown directive %q", token.directive)
}
}
return rules, nil
}
// parseTargets parses the given list of targets and returns the list of targets.
type parseTargetsOptions struct {
args []string
allowEmptyArgs bool
}
// parseTargets parses the given list of targets and returns the list of targets.
func parseTargets(o parseTargetsOptions) ([]Target, error) {
if !o.allowEmptyArgs && len(o.args) == 0 {
return nil, errors.New("missing target")
}
var targets []Target
for _, arg := range o.args {
file, id, hasID := strings.Cut(arg, ":")
var target Target
if file != "" {
target.File = &file
}
if hasID {
target.ID = &id
}
targets = append(targets, target)
}
return targets, nil
}