From ac022ce0ab9a7004c1a2df25acbac0729dd01aa9 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 21 Sep 2021 08:29:30 -0600 Subject: [PATCH] CORE-860: Handle SQL comments during scan --- sql2/scanner.go | 12 ++++++++++++ sql2/scanner_test.go | 3 +++ 2 files changed, 15 insertions(+) diff --git a/sql2/scanner.go b/sql2/scanner.go index 552034a83..105f321b1 100644 --- a/sql2/scanner.go +++ b/sql2/scanner.go @@ -104,6 +104,11 @@ func (s *Scanner) Scan() (pos Pos, token Token, lit string) { case '+': return pos, PLUS, "+" case '-': + if s.peek() == '-' { + s.read() + s.skipComment() + continue + } return pos, MINUS, "-" case '*': return pos, STAR, "*" @@ -117,6 +122,13 @@ func (s *Scanner) Scan() (pos Pos, token Token, lit string) { } } +// skipComment reads all characters until the end of the line or EOF. +func (s *Scanner) skipComment() { + for ch := s.peek(); ch != '\n' && ch != -1; ch = s.peek() { + s.read() + } +} + func (s *Scanner) scanUnquotedIdent(pos Pos, prefix string) (Pos, Token, string) { assert(isUnquotedIdent(s.peek())) diff --git a/sql2/scanner_test.go b/sql2/scanner_test.go index 27e33222d..f4a25a0aa 100644 --- a/sql2/scanner_test.go +++ b/sql2/scanner_test.go @@ -38,6 +38,9 @@ func TestScanner_Scan(t *testing.T) { t.Run("StartingX", func(t *testing.T) { AssertScan(t, `xyz`, sql.IDENT, `xyz`) }) + t.Run("WithComment", func(t *testing.T) { + AssertScan(t, "-- this is a comment\n\n-- more comments\nfoo", sql.IDENT, `foo`) + }) }) t.Run("KEYWORD", func(t *testing.T) {