forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
251 lines (208 loc) · 5.57 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package excellent
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"unicode"
)
// XTokenType is a set of types than can be scanned
type XTokenType int
const (
// BODY - Not in expression
BODY XTokenType = iota
// IDENTIFIER - 'contact.age' in '@contact.age'
IDENTIFIER
// EXPRESSION - the body of an expression '1+2' in '@(1+2)'
EXPRESSION
// EOF - end of expression
EOF
)
func isNameChar(ch rune) bool {
return unicode.IsLetter(ch) || unicode.IsNumber(ch) || ch == '_'
}
// Scanner is something which can scan tokens from input
type Scanner interface {
Scan() (XTokenType, string)
SetUnescapeBody(bool)
}
// xscanner represents a lexical scanner.
type xscanner struct {
input *xinput
identifierTopLevels []string
unescapeBody bool // unescape @@ sequences in the body
}
// NewXScanner returns a new instance of our excellent scanner
func NewXScanner(r io.Reader, identifierTopLevels []string) Scanner {
return &xscanner{
input: newInput(bufio.NewReader(r)),
identifierTopLevels: identifierTopLevels,
unescapeBody: true,
}
}
func (s *xscanner) SetUnescapeBody(unescape bool) {
s.unescapeBody = unescape
}
// scanExpression consumes the current rune and all contiguous pieces until the end of the expression
// our read should be after the '('
func (s *xscanner) scanExpression() (XTokenType, string) {
// create a buffer and read the current character into it.
buf := &bytes.Buffer{}
// our parentheses depth
parens := 1
// read every subsequent character until we reach the end of the expression
for ch := s.input.read(); ch != eof; ch = s.input.read() {
if ch == '"' {
buf.WriteRune(ch)
s.readTextLiteral(buf)
} else if ch == '(' {
buf.WriteRune(ch)
parens++
} else if ch == ')' {
parens--
// we are the end of the expression
if parens == 0 {
break
}
buf.WriteRune(ch)
} else {
buf.WriteRune(ch)
}
}
if parens == 0 {
return EXPRESSION, buf.String()
}
return BODY, strings.Join([]string{"@(", buf.String()}, "")
}
// reads the remainder of a " quoted text literal
func (s *xscanner) readTextLiteral(buf *bytes.Buffer) {
escaped := false
for ch := s.input.read(); ch != eof; ch = s.input.read() {
buf.WriteRune(ch)
if ch == '"' && !escaped {
break
} else if ch == '\\' {
escaped = true
} else {
escaped = false
}
}
}
// scanIdentifier consumes the current rune and all contiguous pieces until the end of the identifer
// our read should be after the '@'
func (s *xscanner) scanIdentifier() (XTokenType, string) {
// Create a buffer and read the current character into it.
buf := &strings.Builder{}
var topLevel string
// Read every subsequent character until we reach the end of the identifier
for ch := s.input.read(); ch != eof; ch = s.input.read() {
if ch == '.' && topLevel == "" {
topLevel = buf.String()
}
// only include period if it's followed by a valid name char
if ch == '.' {
peek := s.input.read()
if isNameChar(peek) {
buf.WriteRune(ch)
buf.WriteRune(peek)
} else {
// this period actually signifies the end of the indentifier
s.input.unread(peek)
s.input.unread('.')
break
}
} else if isNameChar(ch) {
buf.WriteRune(ch)
} else {
s.input.unread(ch)
break
}
}
identifier := buf.String()
if topLevel == "" {
topLevel = identifier
}
topLevel = strings.ToLower(topLevel)
// only return as an identifier if the toplevel scope is valid
if s.identifierTopLevels != nil {
for _, validTopLevel := range s.identifierTopLevels {
if topLevel == validTopLevel {
return IDENTIFIER, identifier
}
}
} else {
return IDENTIFIER, identifier
}
// this was something that looked like an identifier but wasn't an allowed top-level variable, e.g. email address
return BODY, fmt.Sprintf("@%s", identifier)
}
// scanBody consumes the current body until we reach the end of the file or the start of an expression
func (s *xscanner) scanBody() (XTokenType, string) {
// Create a buffer and read the current character into it.
buf := &strings.Builder{}
// read characters until we reach the end of the file or the start of an expression or identifier
for ch := s.input.read(); ch != eof; ch = s.input.read() {
// could be start of an expression
if ch == '@' {
peek := s.input.read()
// start of an expression
if peek == '(' {
s.input.unread(peek)
s.input.unread('@')
break
// @@, means literal @
} else if peek == '@' {
buf.WriteRune('@')
if !s.unescapeBody {
buf.WriteRune('@')
}
// this is an identifier
} else if isNameChar(peek) {
s.input.unread(peek)
s.input.unread('@')
break
// @ at the end of the input
} else if peek == eof {
buf.WriteRune('@')
// @ followed by non-letter
} else {
buf.WriteRune('@')
buf.WriteRune(peek)
}
} else {
buf.WriteRune(ch)
}
}
return BODY, buf.String()
}
// Scan returns the next token and literal value.
func (s *xscanner) Scan() (XTokenType, string) {
for ch := s.input.read(); ch != eof; ch = s.input.read() {
switch ch {
case '@':
peek := s.input.read()
// start of an expression
if peek == '(' {
return s.scanExpression()
// @@, means literal @
} else if peek == '@' {
s.input.unread('@')
s.input.unread('@')
return s.scanBody()
// this is an identifier
} else if isNameChar(peek) {
s.input.unread(peek)
return s.scanIdentifier()
// '@' followed by non-letter, plain body
}
s.input.unread(peek)
s.input.unread('@')
return s.scanBody()
default:
s.input.unread(ch)
return s.scanBody()
}
}
return EOF, ""
}