-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilterscanner.go
183 lines (148 loc) · 3.71 KB
/
filterscanner.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
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elemental
import (
"bytes"
"strings"
"unicode/utf8"
)
// scanner scans a given input
type scanner struct {
buf bytes.Buffer
isWhitespace checkRuneFunc
isLetter checkRuneFunc
isDigit checkRuneFunc
}
// newScanner returns an instance of a Scanner.
func newScanner(
input string,
) *scanner {
var buf bytes.Buffer
_, _ = buf.WriteString(input)
return &scanner{
buf: buf,
isWhitespace: isWhitespace,
isLetter: isLetter,
isDigit: isDigit,
}
}
// read returns the next rune or eof
func (s *scanner) read() rune {
ch, _, err := s.buf.ReadRune()
if err != nil {
return runeEOF
}
return ch
}
// peekNextRune returns the next rune but does not read it.
func (s *scanner) peekNextRune() rune {
if s.buf.Len() == 0 {
return runeEOF
}
ch, _ := utf8.DecodeRune(s.buf.Bytes()[0:])
return ch
}
// unread a previously read rune
func (s *scanner) unread() {
_ = s.buf.UnreadRune()
}
// scan returns the next token and literal value.
func (s *scanner) scan() (parserToken, string) {
ch := s.read()
if s.isWhitespace(ch) {
s.unread()
return s.scanWhitespace()
}
if isOperatorStart(ch) {
// Chack if the next run can create an operator
nextCh := s.peekNextRune()
if nextCh != runeEOF {
if token, literal, ok := isOperator(ch, nextCh); ok {
s.read() // read only if it has matched.
return token, literal
}
}
// Check if the current rune is an operator
if token, literal, ok := isOperator(ch); ok {
return token, literal
}
}
if s.isLetter(ch) || s.isDigit(ch) {
s.unread()
return s.scanWord()
}
token, ok := runeToToken[ch]
if !ok {
return parserTokenILLEGAL, string(ch)
}
return token, string(ch)
}
// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *scanner) scanWhitespace() (parserToken, string) {
var buf bytes.Buffer
_, _ = buf.WriteRune(s.read())
for {
if ch := s.read(); ch == runeEOF {
break
} else if !s.isWhitespace(ch) {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
return parserTokenWHITESPACE, buf.String()
}
// scanWord consumes the current rune and all contiguous letters / digits.
func (s *scanner) scanWord() (parserToken, string) {
var buf bytes.Buffer
_, _ = buf.WriteRune(s.read())
for {
if ch := s.read(); ch == runeEOF {
break
} else if !s.isLetter(ch) && !s.isDigit(ch) {
s.unread()
break
} else {
if ch == '\\' {
// Move forward
ch = s.read()
}
if isOperatorStart(ch) {
// Check if the next rune can create an operator
nextCh := s.peekNextRune()
if nextCh != runeEOF {
if _, _, ok := isOperator(ch, nextCh); ok {
s.unread() // unread ch rune
break
}
}
// Check if ch is an operator by itself
if _, _, ok := isOperator(ch); ok {
s.unread()
break
}
}
_, _ = buf.WriteRune(ch)
}
}
return stringToToken(buf.String())
}
func stringToToken(output string) (parserToken, string) {
upper := strings.ToUpper(output)
if token, ok := wordToToken[upper]; ok {
return token, output
}
if token, ok := operatorsToToken[upper]; ok {
return token, output
}
return parserTokenWORD, output
}