From bce7dd64f74f60d6d973b148b8980bb1c394c032 Mon Sep 17 00:00:00 2001 From: Kris Cherven <50562493+krischerven@users.noreply.github.com> Date: Thu, 16 Apr 2020 19:51:04 -0400 Subject: [PATCH] fix lexer crash (#12) * fix lexer crash * Add recommended change * Add testing --- lexer.go | 3 ++- lexer_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lexer.go b/lexer.go index b5d7e8e..6045997 100644 --- a/lexer.go +++ b/lexer.go @@ -106,7 +106,8 @@ func longestMatch(source string, ic cursor, options []string) string { cur := ic - for { + for cur.pointer < uint(len(source)) { + value = append(value, strings.ToLower(string(source[cur.pointer]))...) cur.pointer++ diff --git a/lexer_test.go b/lexer_test.go index 3451fd9..16f4871 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -93,6 +93,10 @@ func TestToken_lexString(t *testing.T) { string bool value string }{ + { + string: false, + value: "a", + }, { string: true, value: "'abc'", @@ -165,6 +169,11 @@ func TestToken_lexIdentifier(t *testing.T) { input string value string }{ + { + identifier: true, + input: "a", + value: "a", + }, { identifier: true, input: "abc", @@ -275,6 +284,21 @@ func TestLex(t *testing.T) { tokens []token err error }{ + { + input: "select a", + tokens: []token{ + { + loc: location{col: 0, line: 0}, + value: string(selectKeyword), + kind: keywordKind, + }, + { + loc: location{col: 7, line: 0}, + value: "a", + kind: identifierKind, + }, + }, + }, { input: "select true", tokens: []token{