summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-15 02:39:54 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-15 13:30:27 -0700
commit0acaa4c57d7169f3903af3c3df4faf95d2cbea84 (patch)
treea524872e8bd498e621f85ff8adf41240709e2596 /sqlparse
parent74b3464d781cbad4c39cd082daa80334aa7aed78 (diff)
downloadsqlparse-0acaa4c57d7169f3903af3c3df4faf95d2cbea84.tar.gz
Reduce calls by _group to get tk idx
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/engine/grouping.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index ae214c2..a74f6f8 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -313,17 +313,28 @@ def _group(tlist, cls, match,
post=None,
extend=True):
"""Groups together tokens that are joined by a middle token. ie. x < y"""
- for token in list(tlist):
+
+ tidx_offset = 0
+ pidx, prev_ = None, None
+ for idx, token in enumerate(list(tlist)):
+ tidx = idx - tidx_offset
+
+ if token.is_whitespace():
+ continue
if token.is_group() and not isinstance(token, cls):
_group(token, cls, match, valid_left, valid_right, post, extend)
+ pidx, prev_ = tidx, token
continue
if not match(token):
+ pidx, prev_ = tidx, token
continue
- tidx = tlist.token_index(token)
- pidx, prev_ = tlist.token_prev(tidx)
nidx, next_ = tlist.token_next(tidx)
if valid_left(prev_) and valid_right(next_):
from_idx, to_idx = post(tlist, pidx, tidx, nidx)
- tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
+ grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
+ tidx_offset += to_idx - from_idx
+ pidx, prev_ = from_idx, grp
+ else:
+ pidx, prev_ = tidx, token