From c4417f4ecd678b4a69263d7a56acbd78dcc24723 Mon Sep 17 00:00:00 2001 From: lysu Date: Fri, 7 Sep 2018 10:27:50 +0800 Subject: [PATCH] parser: fix single line comment end with newline (#7612) (#7635) --- 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 70ff48c7fa368..6ceb1e062423d 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 a2fe701a41d1f..e2cb06681584f 100644 --- a/parser/lexer_test.go +++ b/parser/lexer_test.go @@ -166,6 +166,10 @@ SELECT`, selectKwd}, SELECT`, selectKwd}, {"#comment\n123", intLit}, {"--5", int('-')}, + {"--\nSELECT", selectKwd}, + {"--\tSELECT", 0}, + {"--\r\nSELECT", selectKwd}, + {"--", 0}, } runTest(c, table) }