forked from nginxinc/nginx-go-crossplane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
228 lines (196 loc) · 4.49 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
/**
* 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
}
func Lex(reader io.Reader) chan NgxToken {
tc := make(chan NgxToken, tokChanCap)
go tokenize(reader, tc)
return tc
}
//nolint:gocyclo,funlen,gocognit
func tokenize(reader io.Reader, tokenCh chan NgxToken) {
token := strings.Builder{}
tokenLine := 1
tokenStartLine := 1
lexState := skipSpace
newToken := false
dupSpecialChar := false
readNext := true
esc := false
depth := 0
var la, quote string
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++
}
} else {
readNext = true
}
// skip CRs
if la == "\r" || la == "\\\r" {
continue
}
if la == "\\" && !esc {
esc = true
continue
}
if esc {
esc = false
la = "\\" + la
}
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)
lexState = inComment
tokenStartLine = tokenLine
continue
}
}
if isSpace(la) {
emit(tokenStartLine, false, nil)
continue
}
// handle parameter expansion syntax (ex: "${var[@]}")
if token.Len() > 0 && strings.HasSuffix(token.String(), "$") && la == "{" {
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)
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)
}