44
55from sqlparse import sql
66from sqlparse import tokens as T
7- from sqlparse .utils import recurse
7+ from sqlparse .utils import recurse , imt , find_matching
88
99
1010def _group_left_right (tlist , ttype , value , cls ,
@@ -47,68 +47,36 @@ def _group_left_right(tlist, ttype, value, cls,
4747 ttype , value )
4848
4949
50- def _find_matching (idx , tlist , start_ttype , start_value , end_ttype , end_value ):
51- depth = 1
52- for tok in tlist .tokens [idx :]:
53- if tok .match (start_ttype , start_value ):
54- depth += 1
55- elif tok .match (end_ttype , end_value ):
56- depth -= 1
57- if depth == 1 :
58- return tok
59- return None
60-
61-
62- def _group_matching (tlist , start_ttype , start_value , end_ttype , end_value ,
63- cls , include_semicolon = False , recurse = False ):
64-
65- [_group_matching (sgroup , start_ttype , start_value , end_ttype , end_value ,
66- cls , include_semicolon ) for sgroup in tlist .get_sublists ()
67- if recurse ]
68- if isinstance (tlist , cls ):
69- idx = 1
70- else :
71- idx = 0
72- token = tlist .token_next_match (idx , start_ttype , start_value )
50+ def _group_matching (tlist , cls ):
51+ """Groups Tokens that have beginning and end. ie. parenthesis, brackets.."""
52+ idx = 1 if imt (tlist , i = cls ) else 0
53+
54+ token = tlist .token_next_by (m = cls .M_OPEN , idx = idx )
7355 while token :
74- tidx = tlist .token_index (token )
75- end = _find_matching (tidx , tlist , start_ttype , start_value ,
76- end_ttype , end_value )
77- if end is None :
78- idx = tidx + 1
79- else :
80- if include_semicolon :
81- next_ = tlist .token_next (tlist .token_index (end ))
82- if next_ and next_ .match (T .Punctuation , ';' ):
83- end = next_
84- group = tlist .group_tokens (cls , tlist .tokens_between (token , end ))
85- _group_matching (group , start_ttype , start_value ,
86- end_ttype , end_value , cls , include_semicolon )
87- idx = tlist .token_index (group ) + 1
88- token = tlist .token_next_match (idx , start_ttype , start_value )
56+ end = find_matching (tlist , token , cls .M_OPEN , cls .M_CLOSE )
57+ if end is not None :
58+ token = tlist .group_tokens (cls , tlist .tokens_between (token , end ))
59+ _group_matching (token , cls )
60+ token = tlist .token_next_by (m = cls .M_OPEN , idx = token )
8961
9062
9163def group_if (tlist ):
92- _group_matching (tlist , T . Keyword , 'IF' , T . Keyword , 'END IF' , sql .If , True )
64+ _group_matching (tlist , sql .If )
9365
9466
9567def group_for (tlist ):
96- _group_matching (tlist , T .Keyword , 'FOR' , T .Keyword , 'END LOOP' ,
97- sql .For , True )
68+ _group_matching (tlist , sql .For )
9869
9970
10071def group_foreach (tlist ):
101- _group_matching (tlist , T .Keyword , 'FOREACH' , T .Keyword , 'END LOOP' ,
102- sql .For , True )
72+ _group_matching (tlist , sql .For )
10373
10474
10575def group_begin (tlist ):
106- _group_matching (tlist , T .Keyword , 'BEGIN' , T .Keyword , 'END' ,
107- sql .Begin , True )
76+ _group_matching (tlist , sql .Begin )
10877
10978
11079def group_as (tlist ):
111-
11280 def _right_valid (token ):
11381 # Currently limited to DML/DDL. Maybe additional more non SQL reserved
11482 # keywords should appear here (see issue8).
@@ -130,7 +98,6 @@ def group_assignment(tlist):
13098
13199
132100def group_comparison (tlist ):
133-
134101 def _parts_valid (token ):
135102 return (token .ttype in (T .String .Symbol , T .String .Single ,
136103 T .Name , T .Number , T .Number .Float ,
@@ -140,13 +107,13 @@ def _parts_valid(token):
140107 sql .Function ))
141108 or (token .ttype is T .Keyword
142109 and token .value .upper () in ['NULL' , ]))
110+
143111 _group_left_right (tlist , T .Operator .Comparison , None , sql .Comparison ,
144112 check_left = _parts_valid , check_right = _parts_valid )
145113
146114
147115def group_case (tlist ):
148- _group_matching (tlist , T .Keyword , 'CASE' , T .Keyword , 'END' , sql .Case ,
149- include_semicolon = True , recurse = True )
116+ _group_matching (tlist , sql .Case )
150117
151118
152119def group_identifier (tlist ):
@@ -222,7 +189,7 @@ def _next_token(tl, i):
222189 and (isinstance (identifier_tokens [0 ], (sql .Function ,
223190 sql .Parenthesis ))
224191 or identifier_tokens [0 ].ttype in (
225- T .Literal .Number .Integer , T .Literal .Number .Float ))):
192+ T .Literal .Number .Integer , T .Literal .Number .Float ))):
226193 group = tlist .group_tokens (sql .Identifier , identifier_tokens )
227194 idx = tlist .token_index (group , start = idx ) + 1
228195 else :
@@ -284,47 +251,11 @@ def group_identifier_list(tlist):
284251
285252
286253def group_brackets (tlist ):
287- """Group parentheses () or square brackets []
288-
289- This is just like _group_matching, but complicated by the fact that
290- round brackets can contain square bracket groups and vice versa
291- """
292-
293- if isinstance (tlist , (sql .Parenthesis , sql .SquareBrackets )):
294- idx = 1
295- else :
296- idx = 0
297-
298- # Find the first opening bracket
299- token = tlist .token_next_match (idx , T .Punctuation , ['(' , '[' ])
300-
301- while token :
302- start_val = token .value # either '(' or '['
303- if start_val == '(' :
304- end_val = ')'
305- group_class = sql .Parenthesis
306- else :
307- end_val = ']'
308- group_class = sql .SquareBrackets
309-
310- tidx = tlist .token_index (token )
311-
312- # Find the corresponding closing bracket
313- end = _find_matching (tidx , tlist , T .Punctuation , start_val ,
314- T .Punctuation , end_val )
315-
316- if end is None :
317- idx = tidx + 1
318- else :
319- group = tlist .group_tokens (group_class ,
320- tlist .tokens_between (token , end ))
254+ _group_matching (tlist , sql .SquareBrackets )
321255
322- # Check for nested bracket groups within this group
323- group_brackets (group )
324- idx = tlist .token_index (group ) + 1
325256
326- # Find the next opening bracket
327- token = tlist . token_next_match ( idx , T . Punctuation , [ '(' , '[' ] )
257+ def group_parenthesis ( tlist ):
258+ _group_matching ( tlist , sql . Parenthesis )
328259
329260
330261@recurse (sql .Comment )
@@ -431,6 +362,7 @@ def group(tlist):
431362 for func in [
432363 group_comments ,
433364 group_brackets ,
365+ group_parenthesis ,
434366 group_functions ,
435367 group_where ,
436368 group_case ,
0 commit comments