From 954ba46e16af4e3c9b1302bbae95ebf2a4be2a8b Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sun, 12 Jun 2016 22:07:07 -0700 Subject: Refactor _group_matching --- sqlparse/engine/grouping.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 240ce5e..bf76119 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -45,29 +45,25 @@ def _group_left_right(tlist, m, cls, def _group_matching(tlist, cls): """Groups Tokens that have beginning and end.""" - [_group_matching(sgroup, cls) for sgroup in tlist.get_sublists() - if not isinstance(sgroup, cls)] - idx = 0 # check no longer needed since not recursing. - opens = [] - - while True: - try: - token = tlist.tokens[idx] - except IndexError: - break + for token in list(tlist): + if token.is_group() and not isinstance(token, cls): + # Check inside previously grouped (ie. parenthesis) if group + # of differnt type is inside (ie, case). though ideally should + # should check for all open/close tokens at once to avoid recursion + _group_matching(token, cls) + continue if token.match(*cls.M_OPEN): - opens.append(idx) + opens.append(token) elif token.match(*cls.M_CLOSE): try: - open_idx = opens.pop() + open_token = opens.pop() except IndexError: - break - tlist.group_tokens_between(cls, open_idx, idx) - idx = open_idx - - idx += 1 + # this indicates invalid sql and unbalanced tokens. + # instead of break, continue in case other "valid" groups exist + continue + tlist.group_tokens_between(cls, open_token, token) def group_if(tlist): -- cgit v1.2.1