Skip to content

Commit edb2565

Browse files
authored
Minor refactor the lexer to allow consuming multiple keywords at once (#142)
1 parent 15335c7 commit edb2565

File tree

10 files changed

+350
-334
lines changed

10 files changed

+350
-334
lines changed

parser/keyword.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
KeywordDrop = "DROP"
6262
KeywordDNS = "DNS"
6363
KeywordElse = "ELSE"
64+
KeywordEmbedded = "EMBEDDED"
6465
KeywordEnd = "END"
6566
KeywordEngine = "ENGINE"
6667
KeywordEstimate = "ESTIMATE"

parser/lexer.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,29 @@ func (t *Token) ToString() string {
7272
return t.String
7373
}
7474

75-
type Lexer struct {
76-
input string
75+
type lexerState struct {
7776
current int
7877
lastToken *Token
7978
}
8079

80+
type Lexer struct {
81+
lexerState
82+
83+
input string
84+
}
85+
8186
func NewLexer(buf string) *Lexer {
8287
return &Lexer{input: buf}
8388
}
8489

90+
func (l *Lexer) saveState() lexerState {
91+
return l.lexerState
92+
}
93+
94+
func (l *Lexer) restoreState(state lexerState) {
95+
l.lexerState = state
96+
}
97+
8598
func (l *Lexer) skipN(n int) {
8699
l.current += n
87100
}
@@ -280,15 +293,13 @@ func (l *Lexer) skipComments() {
280293
}
281294

282295
func (l *Lexer) peekToken() (*Token, error) {
283-
saveToken := l.lastToken
284-
saveCurrent := l.current
296+
savedState := l.saveState()
285297
if err := l.consumeToken(); err != nil {
286298
return nil, err
287299
}
288300
token := l.lastToken
289301

290-
l.lastToken = saveToken
291-
l.current = saveCurrent
302+
l.restoreState(savedState)
292303
return token, nil
293304
}
294305

0 commit comments

Comments
 (0)