From 42909109c86709e1d9c9e12b53a389706a23a3e8 Mon Sep 17 00:00:00 2001 From: robi Date: Tue, 4 Sep 2018 23:31:32 +0800 Subject: [PATCH] parser: fix single line comment end with newline --- parser/lexer.go | 14 ++++++++------ parser/lexer_test.go | 4 ++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/parser/lexer.go b/parser/lexer.go index 8ce6c01379752..f82708383629c 100644 --- a/parser/lexer.go +++ b/parser/lexer.go @@ -307,12 +307,14 @@ func startWithSharp(s *Scanner) (tok int, pos Pos, lit string) { func startWithDash(s *Scanner) (tok int, pos Pos, lit string) { pos = s.r.pos() - if strings.HasPrefix(s.r.s[pos.Offset:], "-- ") { - s.r.incN(3) - s.r.incAsLongAs(func(ch rune) bool { - return ch != '\n' - }) - return s.scan() + if strings.HasPrefix(s.r.s[pos.Offset:], "--") { + remainLen := len(s.r.s[pos.Offset:]) + if remainLen == 2 || (remainLen > 2 && unicode.IsSpace(rune(s.r.s[pos.Offset+2]))) { + s.r.incAsLongAs(func(ch rune) bool { + return ch != '\n' + }) + return s.scan() + } } if strings.HasPrefix(s.r.s[pos.Offset:], "->>") { tok = juss diff --git a/parser/lexer_test.go b/parser/lexer_test.go index a720d4acf814f..a9a066665c436 100644 --- a/parser/lexer_test.go +++ b/parser/lexer_test.go @@ -174,6 +174,10 @@ SELECT`, selectKwd}, SELECT`, selectKwd}, {"#comment\n123", intLit}, {"--5", int('-')}, + {"--\nSELECT", selectKwd}, + {"--\tSELECT", 0}, + {"--\r\nSELECT", selectKwd}, + {"--", 0}, } runTest(c, table) }