Skip to content

Commit

Permalink
refact (lexSymbolState): eliminate the bytes.Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambrt committed Sep 17, 2023
1 parent 5bc80cb commit df8771a
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,34 @@ func lexSymbolState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexSymbolState", "lexer")
defer l.current.clear()

// we'll push the runes we read into this buffer and when appropriate will
// emit tokens using the buffer's data.
var buf bytes.Buffer

WriteToBuf:
ReadRunes:
// keep reading runes into the buffer until we encounter eof of non-text runes.
for {
r := l.read()
switch {
case r == eof:
break WriteToBuf
break ReadRunes
case (isSpace(r) || isSpecial(r)): // whitespace or a special char
l.unread()
break WriteToBuf
default: // otherwise, write the rune into the keyword buffer
buf.WriteRune(r)
break ReadRunes
default:
continue ReadRunes
}
}

// before emitting a token, do we have a special string? But, first let's
// check if we're dealing with a quoted string, since we want to support
// emitting string tokens for "and", "or" so those tokens can be used in
// comparison expr. Example: name % "Johnson and"
switch strings.ToLower(buf.String()) {
switch strings.ToLower(runesToString(l.current)) {
case "and":
l.emit(andToken, "and")
return lexStartState, nil
case "or":
l.emit(orToken, "or")
return lexStartState, nil
default:
l.emit(symbolToken, buf.String())
l.emit(symbolToken, runesToString(l.current))
return lexStartState, nil
}
}
Expand Down

0 comments on commit df8771a

Please sign in to comment.