Skip to content

Commit

Permalink
spaces and lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
chirst committed Jun 25, 2024
1 parent 5c4effe commit b420bb4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
40 changes: 22 additions & 18 deletions compiler/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package compiler

import (
"slices"
"strings"
"unicode"
"unicode/utf8"
)
Expand Down Expand Up @@ -34,20 +36,22 @@ const (
NUMERIC
)

var keywords = []string{
"EXPLAIN",
"SELECT",
"FROM",
"CREATE",
"INSERT",
"INTO",
"TABLE",
"VALUES",
"INTEGER",
"TEXT",
}

func (*lexer) isKeyword(w string) bool {
ws := map[string]bool{
"EXPLAIN": true,
"SELECT": true,
"FROM": true,
"CREATE": true,
"INSERT": true,
"INTO": true,
"TABLE": true,
"VALUES": true,
"INTEGER": true,
"TEXT": true,
}
return ws[w]
uw := strings.ToUpper(w)
return slices.Contains(keywords, uw)
}

type lexer struct {
Expand All @@ -57,7 +61,8 @@ type lexer struct {
}

func NewLexer(src string) *lexer {
return &lexer{src: src}
ts := strings.Trim(src, " \t\n")
return &lexer{src: ts}
}

func (l *lexer) Lex() []token {
Expand Down Expand Up @@ -110,7 +115,7 @@ func (l *lexer) scanWhiteSpace() token {
for l.isWhiteSpace(l.peek(l.end)) {
l.next()
}
return token{tokenType: WHITESPACE, value: l.src[l.start:l.end]}
return token{tokenType: WHITESPACE, value: " "}
}

func (l *lexer) scanWord() token {
Expand All @@ -119,11 +124,10 @@ func (l *lexer) scanWord() token {
l.next()
}
value := l.src[l.start:l.end]
var tokenType tokenType = IDENTIFIER
if l.isKeyword(value) {
tokenType = KEYWORD
return token{tokenType: KEYWORD, value: strings.ToUpper(value)}
}
return token{tokenType: tokenType, value: value}
return token{tokenType: IDENTIFIER, value: value}
}

func (l *lexer) scanDigit() token {
Expand Down
27 changes: 27 additions & 0 deletions compiler/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ func TestLexSelect(t *testing.T) {
{IDENTIFIER, "foo"},
},
},
{
sql: "select * from foo",
expected: []token{
{KEYWORD, "SELECT"},
{WHITESPACE, " "},
{PUNCTUATOR, "*"},
{WHITESPACE, " "},
{KEYWORD, "FROM"},
{WHITESPACE, " "},
{IDENTIFIER, "foo"},
},
},
{
sql: `
select *
from foo
`,
expected: []token{
{KEYWORD, "SELECT"},
{WHITESPACE, " "},
{PUNCTUATOR, "*"},
{WHITESPACE, " "},
{KEYWORD, "FROM"},
{WHITESPACE, " "},
{IDENTIFIER, "foo"},
},
},
{
sql: "EXPLAIN SELECT 1",
expected: []token{
Expand Down

0 comments on commit b420bb4

Please sign in to comment.