Merge pull request #1708 from molecula/sql-comment

CORE-860: Handle SQL comments during scan
This commit is contained in:
Ben Johnson 2021-09-21 08:58:23 -06:00 committed by GitHub
commit 14fdfe478b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View file

@ -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()))

View file

@ -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) {