-
Notifications
You must be signed in to change notification settings - Fork 15
/
lex.go
338 lines (288 loc) · 8.25 KB
/
lex.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package crossplane
import (
"bufio"
"fmt"
"io"
"strings"
)
type NgxToken struct {
Value string
Line int
IsQuoted bool
Error error
}
type state int
const (
skipSpace state = iota
inWord
inComment
inVar
inQuote
)
const TokenChanCap = 2048
//nolint:gochecknoglobals
var lexerFile = "lexer" // pseudo file name for use by parse errors
//nolint:gochecknoglobals
var tokChanCap = TokenChanCap // capacity of lexer token channel
// note: this is only used during tests, should not be changed
func SetTokenChanCap(size int) {
tokChanCap = size
}
// Lexer is an interface for implementing lexers that handle external NGINX tokens during the lexical analysis phase.
type Lexer interface {
// Lex processes a matched token and returns a channel of NgxToken objects.
// This method performs lexical analysis on the matched token and produces a stream of tokens for the parser to consume.
// The external lexer should close the channel once it has completed lexing the input to signal the end of tokens.
// Failure to close the channel will cause the receiver to wait indefinitely.
Lex(s *SubScanner, matchedToken string) <-chan NgxToken
}
// LexOptions allows customization of the lexing process by specifying external lexers
// that can handle specific directives. By registering interest in particular directives,
// external lexers can ensure that these directives are processed separately
// from the general lexical analysis logic.
type LexOptions struct {
Lexers []RegisterLexer
extLexers map[string]Lexer
}
// RegisterLexer is an option that cna be used to add a lexer to tokenize external NGINX tokens.
type RegisterLexer interface {
applyLexOptions(options *LexOptions)
}
type registerLexer struct {
l Lexer
stringTokens []string
}
func (rl registerLexer) applyLexOptions(o *LexOptions) {
if o.extLexers == nil {
o.extLexers = make(map[string]Lexer)
}
for _, s := range rl.stringTokens {
o.extLexers[s] = rl.l
}
}
// LexWithLexer registers a Lexer that implements tokenization of an NGINX configuration after one of the given
// stringTokens is encountered by Lex.
func LexWithLexer(l Lexer, stringTokens ...string) RegisterLexer { //nolint:ireturn
return registerLexer{l: l, stringTokens: stringTokens}
}
// LexWithOptions allows for custom lexing behavior through external lexers specified in the LexOptions.
func LexWithOptions(r io.Reader, options LexOptions) chan NgxToken {
for _, o := range options.Lexers {
o.applyLexOptions(&options)
}
tc := make(chan NgxToken, tokChanCap)
go tokenize(r, tc, options)
return tc
}
func Lex(reader io.Reader) chan NgxToken {
return LexWithOptions(reader, LexOptions{})
}
// SubScanner provides an interface for scanning alternative grammars within NGINX configuration data.
type SubScanner struct {
scanner *bufio.Scanner
tokenLine int
}
// Scan advances the scanner to the next token which will be available though the Text method. It returns false
// when the scan stops by reaching the end of input.
func (e *SubScanner) Scan() bool {
if !e.scanner.Scan() {
return false
}
if t := e.scanner.Text(); isEOL(t) {
e.tokenLine++
}
return true
}
// Err returns the fist non-EOF error encountered by the Scanner.
func (e *SubScanner) Err() error { return e.scanner.Err() }
// Text returns the most recent token generated by a call to Scan.
func (e *SubScanner) Text() string { return e.scanner.Text() }
// Line returns the line number of the most recent token generated by a call to Scan.
func (e *SubScanner) Line() int { return e.tokenLine }
//nolint:gocyclo,funlen,gocognit,maintidx
func tokenize(reader io.Reader, tokenCh chan NgxToken, options LexOptions) {
token := strings.Builder{}
tokenLine := 1
tokenStartLine := 1
lexState := skipSpace
newToken := false
dupSpecialChar := false
readNext := true
esc := false
depth := 0
var la, quote string
nextTokenIsDirective := true
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanRunes)
emit := func(line int, quoted bool, err error) {
tokenCh <- NgxToken{Value: token.String(), Line: line, IsQuoted: quoted, Error: err}
token.Reset()
lexState = skipSpace
}
for {
if readNext {
if !scanner.Scan() {
break // done
}
la = scanner.Text()
if isEOL(la) {
tokenLine++
nextTokenIsDirective = true
}
} else {
readNext = true
}
// skip CRs
if la == "\r" || la == "\\\r" {
continue
}
if la == "\\" && !esc {
esc = true
continue
}
if esc {
esc = false
la = "\\" + la
}
if token.Len() > 0 {
tokenStr := token.String()
if nextTokenIsDirective {
if ext, ok := options.extLexers[tokenStr]; ok {
// saving lex state before emitting tokenStr to know if we encountered start quote
lastLexState := lexState
emit(tokenStartLine, lexState == inQuote, nil)
externalScanner := &SubScanner{scanner: scanner, tokenLine: tokenLine}
extTokenCh := ext.Lex(externalScanner, tokenStr)
for tok := range extTokenCh {
tokenCh <- tok
}
tokenLine = externalScanner.tokenLine
// if we detected a start quote and current char after external lexer processing is end quote we skip it
if lastLexState == inQuote && la == quote {
continue
}
}
}
}
switch lexState {
case skipSpace:
if !isSpace(la) {
lexState = inWord
newToken = true
readNext = false // re-eval
tokenStartLine = tokenLine
}
continue
case inWord:
if newToken {
newToken = false
if la == "#" {
token.WriteString(la)
nextTokenIsDirective = false
lexState = inComment
tokenStartLine = tokenLine
continue
}
}
if isSpace(la) {
emit(tokenStartLine, false, nil)
nextTokenIsDirective = false
continue
}
// handle parameter expansion syntax (ex: "${var[@]}")
if token.Len() > 0 && strings.HasSuffix(token.String(), "$") && la == "{" {
nextTokenIsDirective = false
token.WriteString(la)
lexState = inVar
dupSpecialChar = false
continue
}
// if a quote is found, add the whole string to the token buffer
if la == `"` || la == "'" {
if token.Len() > 0 {
// if a quote is inside a token, treat it like any other char
token.WriteString(la)
} else {
// swallow quote and change state
quote = la
lexState = inQuote
tokenStartLine = tokenLine
}
dupSpecialChar = false
continue
}
// handle special characters that are treated like full tokens
if la == "{" || la == "}" || la == ";" {
// if token complete yield it and reset token buffer
if token.Len() > 0 {
emit(tokenStartLine, false, nil)
}
// only '}' can be repeated
if dupSpecialChar && la != "}" {
emit(tokenStartLine, false, &ParseError{
File: &lexerFile,
What: fmt.Sprintf(`unexpected "%s"`, la),
Line: &tokenLine,
})
close(tokenCh)
return
}
dupSpecialChar = true
if la == "{" {
depth++
}
if la == "}" {
depth--
// early exit if unbalanced braces
if depth < 0 {
emit(tokenStartLine, false, &ParseError{File: &lexerFile, What: `unexpected "}"`, Line: &tokenLine})
close(tokenCh)
return
}
}
token.WriteString(la)
// this character is a full token so emit it
emit(tokenStartLine, false, nil)
nextTokenIsDirective = true
continue
}
dupSpecialChar = false
token.WriteString(la)
case inComment:
if isEOL(la) {
emit(tokenStartLine, false, nil)
continue
}
token.WriteString(la)
case inVar:
token.WriteString(la)
// this is using the same logic as the exiting lexer, but this is wrong since it does not terminate on token boundary
if !strings.HasSuffix(token.String(), "}") && !isSpace(la) {
continue
}
lexState = inWord
case inQuote:
if la == quote {
emit(tokenStartLine, true, nil)
continue
}
if la == "\\"+quote {
la = quote
}
token.WriteString(la)
}
}
if token.Len() > 0 {
emit(tokenStartLine, lexState == inQuote, nil)
}
if depth > 0 {
emit(tokenStartLine, false, &ParseError{File: &lexerFile, What: `unexpected end of file, expecting "}"`, Line: &tokenLine})
}
close(tokenCh)
}