This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
73 lines (66 loc) · 1.51 KB
/
scanner.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
// Copyright 2018 Schibsted
package buildbuild
import (
"bufio"
"io"
"unicode"
"unicode/utf8"
)
type Scanner struct {
*bufio.Scanner
io.Closer
Filename string
scannerSpecials map[rune]bool
scannerNewword bool
}
var (
builddescSpecials = map[rune]bool{'(': true, ')': true, '[': true, ']': true, ':': true}
argsSpecials = map[rune]bool{'[': true, ']': true}
)
func NewScanner(r io.ReadCloser, file string) *Scanner {
s := &Scanner{Scanner: bufio.NewScanner(r), Closer: r, Filename: file}
s.Split(s.Splitter)
s.scannerSpecials = builddescSpecials
return s
}
func (s *Scanner) Splitter(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Skip leading spaces, return special tokens, otherwise scan for words.
start := 0
var r rune
var width int
for {
if start >= len(data) {
return start, nil, nil
}
r, width = utf8.DecodeRune(data[start:])
// Check for comments, runs from # to end of line.
if r == '#' {
for start < len(data) {
r, width = utf8.DecodeRune(data[start:])
if r == '\n' {
break
}
start += width
}
} else if !unicode.IsSpace(r) {
break
}
start += width
s.scannerNewword = true
}
if s.scannerSpecials[r] {
return start + width, data[start : start+width], nil
}
end := start + width
for end < len(data) {
r, width = utf8.DecodeRune(data[end:])
if unicode.IsSpace(r) || s.scannerSpecials[r] {
return end, data[start:end], nil
}
end += width
}
if atEOF {
return len(data), data[start:], nil
}
return start, nil, nil
}