-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.go
More file actions
227 lines (207 loc) · 6.41 KB
/
Copy pathlexer.go
File metadata and controls
227 lines (207 loc) · 6.41 KB
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
package fql
import "strings"
// TokenType identifies the lexical class of a token.
type TokenType int
const (
TokenEOF TokenType = iota
TokenIllegal
TokenWord // bare word: field names and unquoted values (incl. wildcards)
TokenString // quoted string literal, single or double quotes
TokenBang // ! (negation prefix)
TokenColon // :
TokenNColon // !:
TokenTilde // ~
TokenNTilde // !~
TokenEq // =
TokenNEq // !=
TokenGT // >
TokenLT // <
TokenGTE // >=
TokenLTE // <=
TokenPlus // + (AND)
TokenComma // , (OR / list separator)
TokenLParen // (
TokenRParen // )
TokenLBrack // [
TokenRBrack // ]
TokenPipe // |
)
// Token is a single lexed unit with its source position.
type Token struct {
Type TokenType
Text string // raw text; for TokenString this is the DECODED value
Pos int // byte offset in the input
Line int // 1-based line
Col int // 1-based column
}
// wordBoundary reports whether ch terminates a bare word.
func wordBoundary(ch byte) bool {
switch ch {
case ' ', '\t', '\r', '\n', '!', ':', '~', '=', '<', '>', '+', ',', '(', ')', '[', ']', '|', '\'', '"':
return true
}
return false
}
type lexer struct {
input string
pos int
line int
col int
}
func newLexer(input string) *lexer {
return &lexer{input: input, line: 1, col: 1}
}
func (l *lexer) advance() byte {
ch := l.input[l.pos]
l.pos++
if ch == '\n' {
l.line++
l.col = 1
} else {
l.col++
}
return ch
}
func (l *lexer) peek() byte {
if l.pos >= len(l.input) {
return 0
}
return l.input[l.pos]
}
func (l *lexer) peekAt(offset int) byte {
if l.pos+offset >= len(l.input) {
return 0
}
return l.input[l.pos+offset]
}
// lex tokenizes the whole input. It never fails: unknown bytes become
// TokenIllegal tokens and lexing continues.
func (l *lexer) lex() []Token {
var tokens []Token
for l.pos < len(l.input) {
startPos, startLine, startCol := l.pos, l.line, l.col
ch := l.peek()
switch {
case ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n':
l.advance()
continue
case ch == '\'' || ch == '"':
value, ok := l.lexString(ch)
tt := TokenString
if !ok {
tt = TokenIllegal
}
tokens = append(tokens, Token{Type: tt, Text: value, Pos: startPos, Line: startLine, Col: startCol})
case ch == '!':
l.advance()
switch l.peek() {
case ':':
l.advance()
tokens = append(tokens, Token{Type: TokenNColon, Text: "!:", Pos: startPos, Line: startLine, Col: startCol})
case '~':
l.advance()
tokens = append(tokens, Token{Type: TokenNTilde, Text: "!~", Pos: startPos, Line: startLine, Col: startCol})
case '=':
l.advance()
tokens = append(tokens, Token{Type: TokenNEq, Text: "!=", Pos: startPos, Line: startLine, Col: startCol})
default:
tokens = append(tokens, Token{Type: TokenBang, Text: "!", Pos: startPos, Line: startLine, Col: startCol})
}
case ch == '>':
l.advance()
if l.peek() == '=' {
l.advance()
tokens = append(tokens, Token{Type: TokenGTE, Text: ">=", Pos: startPos, Line: startLine, Col: startCol})
} else {
tokens = append(tokens, Token{Type: TokenGT, Text: ">", Pos: startPos, Line: startLine, Col: startCol})
}
case ch == '<':
l.advance()
if l.peek() == '=' {
l.advance()
tokens = append(tokens, Token{Type: TokenLTE, Text: "<=", Pos: startPos, Line: startLine, Col: startCol})
} else {
tokens = append(tokens, Token{Type: TokenLT, Text: "<", Pos: startPos, Line: startLine, Col: startCol})
}
case ch == ':':
l.advance()
tokens = append(tokens, Token{Type: TokenColon, Text: ":", Pos: startPos, Line: startLine, Col: startCol})
case ch == '~':
l.advance()
tokens = append(tokens, Token{Type: TokenTilde, Text: "~", Pos: startPos, Line: startLine, Col: startCol})
case ch == '=':
l.advance()
tokens = append(tokens, Token{Type: TokenEq, Text: "=", Pos: startPos, Line: startLine, Col: startCol})
case ch == '+':
l.advance()
tokens = append(tokens, Token{Type: TokenPlus, Text: "+", Pos: startPos, Line: startLine, Col: startCol})
case ch == ',':
l.advance()
tokens = append(tokens, Token{Type: TokenComma, Text: ",", Pos: startPos, Line: startLine, Col: startCol})
case ch == '(':
l.advance()
tokens = append(tokens, Token{Type: TokenLParen, Text: "(", Pos: startPos, Line: startLine, Col: startCol})
case ch == ')':
l.advance()
tokens = append(tokens, Token{Type: TokenRParen, Text: ")", Pos: startPos, Line: startLine, Col: startCol})
case ch == '[':
l.advance()
tokens = append(tokens, Token{Type: TokenLBrack, Text: "[", Pos: startPos, Line: startLine, Col: startCol})
case ch == ']':
l.advance()
tokens = append(tokens, Token{Type: TokenRBrack, Text: "]", Pos: startPos, Line: startLine, Col: startCol})
case ch == '|':
l.advance()
tokens = append(tokens, Token{Type: TokenPipe, Text: "|", Pos: startPos, Line: startLine, Col: startCol})
default:
word := l.lexWord()
if word == "" {
// Unknown byte (e.g. control char) — consume so lexing progresses.
l.advance()
tokens = append(tokens, Token{Type: TokenIllegal, Text: string(ch), Pos: startPos, Line: startLine, Col: startCol})
} else {
tokens = append(tokens, Token{Type: TokenWord, Text: word, Pos: startPos, Line: startLine, Col: startCol})
}
}
}
tokens = append(tokens, Token{Type: TokenEOF, Pos: l.pos, Line: l.line, Col: l.col})
return tokens
}
// lexString consumes a quoted string with backslash escapes and returns the
// decoded value. ok is false when the closing quote is missing.
func (l *lexer) lexString(quote byte) (string, bool) {
l.advance() // opening quote
var sb strings.Builder
for l.pos < len(l.input) {
ch := l.advance()
if ch == '\\' && l.pos < len(l.input) {
next := l.advance()
switch next {
case quote, '\\':
// Only the quote char and backslash itself are escapes.
sb.WriteByte(next)
default:
// Everything else stays literal: FQL values are full of
// Windows paths ('*\regsvr32.exe') and regex escapes ('\d+')
// where \r \n \t \d must remain backslash + char, not a
// control character.
sb.WriteByte('\\')
sb.WriteByte(next)
}
continue
}
if ch == quote {
return sb.String(), true
}
sb.WriteByte(ch)
}
return sb.String(), false // unterminated
}
// lexWord consumes a run of non-boundary bytes.
func (l *lexer) lexWord() string {
start := l.pos
for l.pos < len(l.input) && !wordBoundary(l.input[l.pos]) {
l.advance()
}
return l.input[start:l.pos]
}