-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
47 lines (39 loc) · 913 Bytes
/
processor.go
File metadata and controls
47 lines (39 loc) · 913 Bytes
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
package processor
import (
"bufio"
"bytes"
"regexp"
)
const (
zeroStr = ""
)
type (
Processor struct {
*bufio.Scanner
}
)
const (
firstAnnRegex = `(@[a-zA-Z_][0-9a-zA-Z_]*)\s*(.*)`
secondAnnRegex = `(@[a-zA-Z_][0-9a-zA-Z_]*)\((.+?)\)\s*(.*)`
)
var (
firstAnnRe = regexp.MustCompile(firstAnnRegex)
secondAnnRe = regexp.MustCompile(secondAnnRegex)
)
func NewProcessor(text string) *Processor {
return &Processor{bufio.NewScanner(bytes.NewBufferString(text))}
}
func (process *Processor) Scan(fn func(ann, key, value string) (err error)) (err error) {
for process.Scanner.Scan() {
text := process.Text()
if values := secondAnnRe.FindStringSubmatch(text); len(values) == 4 {
err = fn(values[1], values[2], values[3])
} else if values := firstAnnRe.FindStringSubmatch(text); len(values) == 3 {
err = fn(values[1], zeroStr, values[2])
}
if err != nil {
break
}
}
return
}