-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
lexer.go
209 lines (178 loc) · 3.8 KB
/
lexer.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
package parser
import (
"bufio"
"bytes"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser/flag"
"io"
)
type lexer struct {
reader *bufio.Reader
file string
line int
column int
Latest flag.Flag
}
func lex(content string) *lexer {
return newLexer(bytes.NewBuffer([]byte(content)))
}
func newLexer(r io.Reader) *lexer {
return &lexer{
line: 1,
reader: bufio.NewReader(r),
}
}
func (s *lexer) scan() flag.Flag {
s.Latest = s.getNextFlag()
return s.Latest
}
//func (s *lexer) all() flag.Flags {
// tokens := make([]flag.Flag, 0)
// for {
// v := s.scan()
// if v.Type == flag.EOF || v.Type == -1 {
// break
// }
// tokens = append(tokens, v)
// }
// return tokens
//}
func (s *lexer) getNextFlag() flag.Flag {
retoFlag:
ch := s.peek()
switch {
case isSpace(ch):
s.skipWhitespace()
goto retoFlag
case isEOF(ch):
return s.NewToken(flag.EOF).Lit(string(s.read()))
case ch == ';':
return s.NewToken(flag.Semicolon).Lit(string(s.read()))
case ch == '{':
return s.NewToken(flag.BlockStart).Lit(string(s.read()))
case ch == '}':
return s.NewToken(flag.BlockEnd).Lit(string(s.read()))
case ch == '#':
return s.scanComment()
case ch == '$':
return s.scanVariable()
case isQuote(ch):
return s.scanQuotedString(ch)
default:
return s.scanKeyword()
}
}
func (s *lexer) peek() rune {
r, _, _ := s.reader.ReadRune()
_ = s.reader.UnreadRune()
return r
}
type runeCheck func(rune) bool
func (s *lexer) readUntil(until runeCheck) string {
var buf bytes.Buffer
buf.WriteRune(s.read())
for {
if ch := s.peek(); isEOF(ch) {
break
} else if until(ch) {
break
} else {
buf.WriteRune(s.read())
}
}
return buf.String()
}
func (s *lexer) NewToken(tokenType flag.Type) flag.Flag {
return flag.Flag{
Type: tokenType,
Line: s.line,
Column: s.column,
}
}
func (s *lexer) readWhile(while runeCheck) string {
var buf bytes.Buffer
buf.WriteRune(s.read())
for {
if ch := s.peek(); while(ch) {
buf.WriteRune(s.read())
} else {
break
}
}
return buf.String()
}
func (s *lexer) skipWhitespace() {
s.readWhile(isSpace)
}
func (s *lexer) scanComment() flag.Flag {
return s.NewToken(flag.Comment).Lit(s.readUntil(isEndOfLine))
}
func (s *lexer) scanQuotedString(delimiter rune) flag.Flag {
var buf bytes.Buffer
tok := s.NewToken(flag.QuotedString)
buf.WriteRune(s.read()) //consume delimiter
for {
ch := s.read()
if ch == rune(flag.EOF) {
panic("unexpected end of file while scanning a string, maybe an unclosed quote?")
}
if ch == '\\' {
if needsEscape(s.peek(), delimiter) {
switch s.read() {
case 'n':
buf.WriteRune('\n')
case 'r':
buf.WriteRune('\r')
case 't':
buf.WriteRune('\t')
case '\\':
buf.WriteRune('\\')
case delimiter:
buf.WriteRune(delimiter)
}
continue
}
}
buf.WriteRune(ch)
if ch == delimiter {
break
}
}
return tok.Lit(buf.String())
}
func (s *lexer) scanKeyword() flag.Flag {
return s.NewToken(flag.Keyword).Lit(s.readUntil(isKeywordTerminator))
}
func (s *lexer) scanVariable() flag.Flag {
return s.NewToken(flag.Variable).Lit(s.readUntil(isKeywordTerminator))
}
func (s *lexer) read() rune {
ch, _, err := s.reader.ReadRune()
if err != nil {
return rune(flag.EOF)
}
if ch == '\n' {
s.column = 1
s.line++
} else {
s.column++
}
return ch
}
func isQuote(ch rune) bool {
return ch == '"' || ch == '\'' || ch == '`'
}
func isKeywordTerminator(ch rune) bool {
return isSpace(ch) || isEndOfLine(ch) || ch == '{' || ch == ';'
}
func needsEscape(ch, delimiter rune) bool {
return ch == delimiter || ch == 'n' || ch == 't' || ch == '\\' || ch == 'r'
}
func isSpace(ch rune) bool {
return ch == ' ' || ch == '\t' || isEndOfLine(ch)
}
func isEOF(ch rune) bool {
return ch == rune(flag.EOF)
}
func isEndOfLine(ch rune) bool {
return ch == '\r' || ch == '\n'
}