@@ -45,29 +45,25 @@ def _group_left_right(tlist, m, cls,
4545
4646def _group_matching (tlist , cls ):
4747 """Groups Tokens that have beginning and end."""
48- [_group_matching (sgroup , cls ) for sgroup in tlist .get_sublists ()
49- if not isinstance (sgroup , cls )]
50- idx = 0 # check no longer needed since not recursing.
51-
5248 opens = []
53-
54- while True :
55- try :
56- token = tlist .tokens [idx ]
57- except IndexError :
58- break
49+ for token in list (tlist ):
50+ if token .is_group () and not isinstance (token , cls ):
51+ # Check inside previously grouped (ie. parenthesis) if group
52+ # of differnt type is inside (ie, case). though ideally should
53+ # should check for all open/close tokens at once to avoid recursion
54+ _group_matching (token , cls )
55+ continue
5956
6057 if token .match (* cls .M_OPEN ):
61- opens .append (idx )
58+ opens .append (token )
6259 elif token .match (* cls .M_CLOSE ):
6360 try :
64- open_idx = opens .pop ()
61+ open_token = opens .pop ()
6562 except IndexError :
66- break
67- tlist .group_tokens_between (cls , open_idx , idx )
68- idx = open_idx
69-
70- idx += 1
63+ # this indicates invalid sql and unbalanced tokens.
64+ # instead of break, continue in case other "valid" groups exist
65+ continue
66+ tlist .group_tokens_between (cls , open_token , token )
7167
7268
7369def group_if (tlist ):
0 commit comments