-
Notifications
You must be signed in to change notification settings - Fork 503
/
parsing.go
297 lines (227 loc) · 6.04 KB
/
parsing.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package govaluate
import (
"fmt"
"errors"
"time"
"bytes"
"strconv"
"unicode"
)
func parseTokens(expression string) ([]ExpressionToken, error) {
var ret []ExpressionToken
var token, lastToken ExpressionToken
var state lexerState
var stream *lexerStream
var err error
var found bool
state = validLexerStates[0];
stream = newLexerStream(expression);
for stream.canRead() {
token, err, found = readToken(stream, state);
if(err != nil) {
return ret, err
}
if(!found) {
break;
}
if(!state.canTransitionTo(token.Kind)) {
firstStateName := fmt.Sprintf("%s [%v]", GetTokenKindString(state.kind), lastToken.Value);
nextStateName := fmt.Sprintf("%s [%v]", GetTokenKindString(token.Kind), token.Value);
return ret, errors.New("Cannot transition token types from " + firstStateName + " to " + nextStateName);
}
// append this valid token, find new lexer state.
ret = append(ret, token)
for _, possibleState := range validLexerStates {
if(possibleState.kind == token.Kind) {
state = possibleState
break;
}
}
lastToken = token;
}
if(!state.isEOF) {
return ret, errors.New("Unexpected end of expression");
}
return ret, nil
}
func readToken(stream *lexerStream, state lexerState) (ExpressionToken, error, bool) {
var ret ExpressionToken
var tokenValue interface{}
var tokenTime time.Time;
var tokenString string
var kind TokenKind
var character rune
var found bool
// numeric is 0-9, or .
// string starts with '
// variable is alphanumeric, always starts with a letter
// symbols are anything non-alphanumeric
// all others read into a buffer until they reach the end of the stream
for(stream.canRead()) {
character = stream.readCharacter()
if(unicode.IsSpace(character)) {
continue
}
kind = UNKNOWN;
// numeric constant
if(isNumeric(character)) {
tokenString = readTokenUntilFalse(stream, isNumeric);
tokenValue, _ = strconv.ParseFloat(tokenString, 64)
kind = NUMERIC;
break;
}
// variable
if(unicode.IsLetter(character)) {
tokenValue = readTokenUntilFalse(stream, unicode.IsLetter);
kind = VARIABLE;
if(tokenValue == "true") {
kind = BOOLEAN
tokenValue = true
} else {
if(tokenValue == "false") {
kind = BOOLEAN
tokenValue = false
}
}
break;
}
if(!isNotQuote(character)) {
tokenValue = readUntilFalse(stream, true, false, isNotQuote);
// advance the stream one position, since reading until false assumes the terminator is a real token
stream.rewind(-1)
// check to see if this can be parsed as a time.
tokenTime, found = tryParseTime(tokenValue.(string));
if(found) {
kind = TIME;
tokenValue = tokenTime;
} else {
kind = STRING;
}
break;
}
if(character == '(') {
tokenValue = character
kind = CLAUSE
break;
}
if(character == ')') {
tokenValue = character
kind = CLAUSE_CLOSE
break;
}
// must be a known symbol
tokenString = readTokenUntilFalse(stream, isNotAlphanumeric);
tokenValue = tokenString
// quick hack for the case where "-" can mean "prefixed negation" or "minus", which are used
// very differently.
if(state.canTransitionTo(PREFIX)) {
_, found = PREFIX_SYMBOLS[tokenString];
if(found) {
kind = PREFIX;
break;
}
}
_, found = MODIFIER_SYMBOLS[tokenString];
if(found) {
kind = MODIFIER;
break;
}
_, found = LOGICAL_SYMBOLS[tokenString];
if(found) {
kind = LOGICALOP;
break;
}
_, found = COMPARATOR_SYMBOLS[tokenString];
if(found) {
kind = COMPARATOR;
break;
}
errorMessage := fmt.Sprintf("Invalid token: %v", tokenValue);
return ret, errors.New(errorMessage), false;
}
ret.Kind = kind;
ret.Value = tokenValue;
return ret, nil, (kind != UNKNOWN);
}
func readTokenUntilFalse(stream *lexerStream, condition func(rune)(bool)) string {
var ret string;
stream.rewind(1)
ret = readUntilFalse(stream, false, true, condition);
return ret;
}
func readUntilFalse(stream *lexerStream, includeWhitespace bool, breakWhitespace bool, condition func(rune)(bool)) string {
var tokenBuffer bytes.Buffer
var character rune
for(stream.canRead()) {
character = stream.readCharacter()
if(unicode.IsSpace(character)) {
if(breakWhitespace && tokenBuffer.Len() > 0) {
break;
}
if(!includeWhitespace) {
continue;
}
}
if(condition(character)) {
tokenBuffer.WriteString(string(character));
} else {
stream.rewind(1)
break;
}
}
return tokenBuffer.String();
}
func isNumeric(character rune) bool {
return unicode.IsDigit(character) || character == '.';
}
func isNotQuote(character rune) bool {
return character != '\'' && character != '"';
}
func isNotAlphanumeric(character rune) bool {
return !(unicode.IsDigit(character) ||
unicode.IsLetter(character) ||
character == '(' ||
character == ')' ||
!isNotQuote(character));
}
/*
Attempts to parse the [candidate] as a Time.
Tries a series of standardized date formats, returns the Time if one applies,
otherwise returns false through the second return.
*/
func tryParseTime(candidate string) (time.Time, bool) {
var ret time.Time;
var found bool;
timeFormats := [...]string {
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.Kitchen,
time.RFC3339,
time.RFC3339Nano,
"2006-01-02", // RFC 3339
"2006-01-02 15:04", // RFC 3339 with minutes
"2006-01-02 15:04:05", // RFC 3339 with seconds
"2006-01-02 15:04:05-07:00", // RFC 3339 with seconds and timezone
"2006-01-02T15Z0700", // ISO8601 with hour
"2006-01-02T15:04Z0700", // ISO8601 with minutes
"2006-01-02T15:04:05Z0700", // ISO8601 with seconds
"2006-01-02T15:04:05.999999999Z0700", // ISO8601 with nanoseconds
};
for _, format := range timeFormats {
ret, found = tryParseExactTime(candidate, format);
if(found) {
return ret, true;
}
}
return time.Now(), false;
}
func tryParseExactTime(candidate string, format string) (time.Time, bool) {
var ret time.Time;
var err error;
ret, err = time.Parse(format, candidate);
if(err != nil) {
return time.Now(), false;
}
return ret, true;
}