Skip to content

Commit cd4a723

Browse files
committed
Don't make parsing of square bracket identifiers too greedy (fixes andialbrecht#583).
1 parent 3e8076d commit cd4a723

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Bug Fixes
2020

2121
* Improved parsing of IN(...) statements (issue566, pr567 by hurcy).
2222
* Preserve line breaks when removing comments (issue484).
23+
* Fix parsing error when using square bracket notation (issue583).
2324

2425

2526
Release 0.3.1 (Feb 29, 2020)

sqlparse/keywords.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def is_keyword(value):
7373
# sqlite names can be escaped with [square brackets]. left bracket
7474
# cannot be preceded by word character or a right bracket --
7575
# otherwise it's probably an array index
76-
(r'(?<![\w\])])(\[[^\]]+\])', tokens.Name),
76+
(r'(?<![\w\])])(\[[^\]\[]+\])', tokens.Name),
7777
(r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
7878
r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
7979
(r'END(\s+IF|\s+LOOP|\s+WHILE)?\b', tokens.Keyword),

tests/test_parse.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def test_parse_square_brackets_notation_isnt_too_greedy():
100100
assert t[0].tokens[-1].get_real_name() == '[bar]'
101101

102102

103+
def test_parse_square_brackets_notation_isnt_too_greedy2():
104+
# see issue583
105+
t = sqlparse.parse('[(foo[i])]')[0].tokens
106+
assert isinstance(t[0], sql.SquareBrackets) # not Identifier!
107+
108+
103109
def test_parse_keyword_like_identifier():
104110
# see issue47
105111
t = sqlparse.parse('foo.key')[0].tokens

0 commit comments

Comments
 (0)