Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scan unicode identifier #16

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions sqllexer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sqllexer

import "unicode/utf8"

type TokenType int

const (
Expand Down Expand Up @@ -98,7 +100,7 @@ func (s *Lexer) Scan() Token {
case isWhitespace(ch):
return s.scanWhitespace()
case isLetter(ch):
return s.scanIdentifier()
return s.scanIdentifier(ch)
case isDoubleQuote(ch):
return s.scanDoubleQuotedIdentifier('"')
case isSingleQuote(ch):
Expand All @@ -114,9 +116,9 @@ func (s *Lexer) Scan() Token {
if isDigit(nextCh) || nextCh == '.' {
return s.scanNumberWithLeadingSign()
}
return s.scanOperator()
return s.scanOperator(ch)
case isDigit(ch):
return s.scanNumber()
return s.scanNumber(ch)
case isWildcard(ch):
return s.scanWildcard()
case ch == '$':
Expand All @@ -125,21 +127,21 @@ func (s *Lexer) Scan() Token {
return s.scanPositionalParameter()
}
if s.config.DBMS == DBMSSQLServer && isLetter(s.lookAhead(1)) {
return s.scanIdentifier()
return s.scanIdentifier(ch)
}
return s.scanDollarQuotedString()
case ch == ':':
if s.config.DBMS == DBMSOracle && isLetter(s.lookAhead(1)) {
return s.scanBindParameter()
}
return s.scanOperator()
return s.scanOperator(ch)
case ch == '@':
if s.config.DBMS == DBMSSQLServer && isLetter(s.lookAhead(1)) {
return s.scanBindParameter()
}
return s.scanOperator()
return s.scanOperator(ch)
case isOperator(ch):
return s.scanOperator()
return s.scanOperator(ch)
case isPunctuation(ch):
if ch == '[' && s.config.DBMS == DBMSSQLServer {
return s.scanDoubleQuotedIdentifier('[')
Expand All @@ -157,7 +159,8 @@ func (s *Lexer) lookAhead(n int) rune {
if s.cursor+n >= len(s.src) || s.cursor+n < 0 {
return 0
}
return rune(s.src[s.cursor+n])
r, _ := utf8.DecodeRuneInString(s.src[s.cursor+n:])
return r
}

// peek returns the rune at the cursor position.
Expand All @@ -172,10 +175,11 @@ func (s *Lexer) nextBy(n int) rune {
return 0
}
s.cursor += n
if s.cursor == len(s.src) {
if s.cursor >= len(s.src) {
return 0
}
return rune(s.src[s.cursor])
r, _ := utf8.DecodeRuneInString(s.src[s.cursor:])
return r
}

// next advances the cursor by 1 position and returns the rune at the cursor position.
Expand All @@ -197,17 +201,17 @@ func (s *Lexer) matchAt(match []rune) bool {

func (s *Lexer) scanNumberWithLeadingSign() Token {
s.start = s.cursor
s.next() // consume the leading sign
return s.scanNumberic()
ch := s.next() // consume the leading sign
return s.scanNumberic(ch)
}

func (s *Lexer) scanNumber() Token {
func (s *Lexer) scanNumber(ch rune) Token {
s.start = s.cursor
return s.scanNumberic()
return s.scanNumberic(ch)
}

func (s *Lexer) scanNumberic() Token {
if s.peek() == '0' {
func (s *Lexer) scanNumberic(ch rune) Token {
if ch == '0' {
nextCh := s.lookAhead(1)
if nextCh == 'x' || nextCh == 'X' {
return s.scanHexNumber()
Expand Down Expand Up @@ -288,12 +292,12 @@ func (s *Lexer) scanString() Token {
}
}

func (s *Lexer) scanIdentifier() Token {
func (s *Lexer) scanIdentifier(ch rune) Token {
// NOTE: this func does not distinguish between SQL keywords and identifiers
s.start = s.cursor
ch := s.next()
ch = s.nextBy(utf8.RuneLen(ch))
for isLetter(ch) || isDigit(ch) || ch == '.' || ch == '?' || ch == '$' {
ch = s.next()
ch = s.nextBy(utf8.RuneLen(ch))
}
// return the token as uppercase so that we can do case insensitive matching
return Token{IDENT, s.src[s.start:s.cursor]}
Expand Down Expand Up @@ -339,9 +343,8 @@ func (s *Lexer) scanWhitespace() Token {
return Token{WS, s.src[s.start:s.cursor]}
}

func (s *Lexer) scanOperator() Token {
func (s *Lexer) scanOperator(lastCh rune) Token {
s.start = s.cursor
lastCh := s.peek()
ch := s.next()
for isOperator(ch) && !(lastCh == '=' && ch == '?') {
// hack: we don't want to treat "=?" as an single operator
Expand Down
59 changes: 59 additions & 0 deletions sqllexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,65 @@ func TestLexer(t *testing.T) {
}
}

func TestLexerUnicode(t *testing.T) {
tests := []struct {
input string
expected []Token
lexerOpts []lexerOption
}{
{
input: `Descripció_CAT`,
expected: []Token{
{IDENT, `Descripció_CAT`},
},
},
{
input: `世界`,
expected: []Token{
{IDENT, `世界`},
},
},
{
input: `こんにちは`,
expected: []Token{
{IDENT, `こんにちは`},
},
},
{
input: `안녕하세요`,
expected: []Token{
{IDENT, `안녕하세요`},
},
},
{
input: `über`,
expected: []Token{
{IDENT, `über`},
},
},
{
input: `résumé`,
expected: []Token{
{IDENT, `résumé`},
},
},
{
input: `"über"`,
expected: []Token{
{IDENT, `"über"`},
},
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
lexer := New(tt.input, tt.lexerOpts...)
tokens := lexer.ScanAll()
assert.Equal(t, tt.expected, tokens)
})
}
}

func ExampleLexer() {
query := "SELECT * FROM users WHERE id = 1"
lexer := New(query)
Expand Down