-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
205 lines (168 loc) Β· 3.84 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
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
197
198
199
200
201
202
203
204
205
package main
import (
"bufio"
"bytes"
"io"
)
// eof represents a marker rune for the end of the reader, defining it here
// gives us a possibility to recognize it in the Scanner
var eof = rune(0)
type Scanner struct {
r *reader
}
// NewScanner creates a Scanner struct, which contains a buffered rune reader.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: &reader{r: bufio.NewReader(r)}}
}
// Scan will scan individual runes, and identify if a specific rune is ...
// encountered
func (s *Scanner) Scan() (token Token, pos Pos, lit string) {
// Read the next rune
ch, pos := s.r.read()
switch {
case isWhitespace(ch):
return s.scanWhitespace()
case isPercent(ch):
return s.scanField()
case isHyphen(ch):
s.r.unread()
return s.scanCard()
default:
// Otherwise read the individual character
switch ch {
case eof:
return EOF, pos, ""
}
}
return ILLEGAL, pos, string(ch)
}
// scanField will scan the FIELD Token, as well as return the literal
// string contained in that FIELD Token.
func (s *Scanner) scanField() (token Token, pos Pos, lit string) {
// Save the position of the field
_, pos = s.r.curr()
// Create buffer, here we'll write the runes into
var buf bytes.Buffer
// The next character should be another percent
ch, _ := s.r.read()
if !isPercent(ch) {
s.r.unread()
return ILLEGAL, pos, ""
}
// We read until we see the first non-whitespace character
for {
if ch, _ = s.r.read(); !isWhitespace(ch) {
s.r.unread()
break
}
}
// Read until:
// * double percent
// * eof
// * card
for {
ch, _ = s.r.read()
if ch == eof {
break
} else if isPercent(ch) {
// Peak ahead
chNext, _ := s.r.read()
s.r.unread()
if isPercent(chNext) {
s.r.unread()
break
}
} else if isHyphen(ch) {
// We start at depth 1, because
// we already read the first hyphen
if s.isCard(1) {
break
}
// isCard will step back one too much, we
// want to write the initial hyphen
s.r.read()
}
// Write runes into buffer
_, _ = buf.WriteRune(ch)
}
return FIELD, pos, buf.String()
}
// scanCard will scan the CARD Token
func (s *Scanner) scanCard() (token Token, pos Pos, lit string) {
// Save the position of the field
_, pos = s.r.curr()
// Create buffer, here we'll write the runes into
var buf bytes.Buffer
// When a hyphen is found it should be three consecutive hyphens
if !s.isCard(0) {
return ILLEGAL, pos, ""
}
// Read until:
// * eof
// * not a hyphen
// * more than 3 consecutive hyphens
i := 0
for {
ch, _ := s.r.read()
if ch == eof {
break
} else if !isHyphen(ch) {
s.r.unread()
break
} else if i > 2 {
s.r.unread()
break
} else {
_, _ = buf.WriteRune(ch)
i++
}
}
return CARD, pos, buf.String()
}
// scanWhiteSpace will scan WHITESPACE Tokens
func (s *Scanner) scanWhitespace() (token Token, pos Pos, lit string) {
// Save the position of the field
_, pos = s.r.curr()
// Create buffer, here we'll write the runes into
var buf bytes.Buffer
// Read until:
// * eof
// * discontinuation of whitespaces
for {
ch, _ := s.r.read()
if ch == eof {
break
} else if !isWhitespace(ch) {
s.r.unread()
break
}
_, _ = buf.WriteRune(ch)
}
return WS, pos, buf.String()
}
// isCard will identify if a encountered `-` (hyphen) is part of a CARD Token,
// the next three characters should also be `-`
func (s *Scanner) isCard(depth int) bool {
ch, _ := s.r.read()
if !isHyphen(ch) || depth > 2 {
s.r.unreadRepeat(depth + 1)
return false
}
if depth < 2 {
return s.isCard(depth + 1)
}
s.r.unreadRepeat(depth + 1)
return true
}
func isWhitespace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n'
}
func isLetter(ch rune) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func isPercent(ch rune) bool {
return ch == '%'
}
func isHyphen(ch rune) bool {
return ch == '-'
}