Skip to content

Commit fbaf4c2

Browse files
committed
Match single line comments at end of string.
The bug was reported for CrunchyFrog: http://code.google.com/p/crunchyfrog/issues/detail?id=88
1 parent 51bd3d5 commit fbaf4c2

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

sqlparse/lexer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ class Lexer:
162162

163163
tokens = {
164164
'root': [
165-
(r'--.*?(\r|\n|\r\n)', tokens.Comment.Single),
165+
(r'--.*?(\r\n|\r|\n)', tokens.Comment.Single),
166+
# $ matches *before* newline, therefore we have two patterns
167+
# to match Comment.Single
168+
(r'--.*?$', tokens.Comment.Single),
166169
(r'(\r|\n|\r\n)', tokens.Newline),
167170
(r'\s+', tokens.Whitespace),
168171
(r'/\*', tokens.Comment.Multiline, 'multiline-comments'),

tests/test_split.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def test_dashcomments(self):
5353
self.assertEqual(len(stmts), 3)
5454
self.ndiffAssertEqual(''.join(unicode(q) for q in stmts), sql)
5555

56+
def test_dashcomments_eol(self):
57+
stmts = sqlparse.parse('select foo; -- comment\n')
58+
self.assertEqual(len(stmts), 1)
59+
stmts = sqlparse.parse('select foo; -- comment\r')
60+
self.assertEqual(len(stmts), 1)
61+
stmts = sqlparse.parse('select foo; -- comment\r\n')
62+
self.assertEqual(len(stmts), 1)
63+
stmts = sqlparse.parse('select foo; -- comment')
64+
self.assertEqual(len(stmts), 1)
65+
5666
def test_begintag(self):
5767
sql = load_file('begintag.sql')
5868
stmts = sqlparse.parse(sql)

0 commit comments

Comments
 (0)