Groups together tokens that are joined by a middle token. i.e. x < y
(tlist, cls, match,
valid_prev=lambda t: True,
valid_next=lambda t: True,
post=None,
extend=True,
recurse=True,
depth=0
)
| 476 | |
| 477 | |
| 478 | def _group(tlist, cls, match, |
| 479 | valid_prev=lambda t: True, |
| 480 | valid_next=lambda t: True, |
| 481 | post=None, |
| 482 | extend=True, |
| 483 | recurse=True, |
| 484 | depth=0 |
| 485 | ): |
| 486 | """Groups together tokens that are joined by a middle token. i.e. x < y""" |
| 487 | if MAX_GROUPING_DEPTH is not None and depth > MAX_GROUPING_DEPTH: |
| 488 | raise SQLParseError( |
| 489 | f"Maximum grouping depth exceeded ({MAX_GROUPING_DEPTH})." |
| 490 | ) |
| 491 | |
| 492 | # Limit the number of tokens to prevent DoS attacks |
| 493 | if MAX_GROUPING_TOKENS is not None \ |
| 494 | and len(tlist.tokens) > MAX_GROUPING_TOKENS: |
| 495 | raise SQLParseError( |
| 496 | f"Maximum number of tokens exceeded ({MAX_GROUPING_TOKENS})." |
| 497 | ) |
| 498 | |
| 499 | tidx_offset = 0 |
| 500 | pidx, prev_ = None, None |
| 501 | token_list = list(tlist) |
| 502 | |
| 503 | for idx, token in enumerate(token_list): |
| 504 | tidx = idx - tidx_offset |
| 505 | if tidx < 0: # tidx shouldn't get negative |
| 506 | continue |
| 507 | |
| 508 | if token.is_whitespace: |
| 509 | continue |
| 510 | |
| 511 | if recurse and token.is_group and not isinstance(token, cls): |
| 512 | _group(token, cls, match, valid_prev, valid_next, |
| 513 | post, extend, True, depth + 1) |
| 514 | |
| 515 | if match(token): |
| 516 | nidx, next_ = tlist.token_next(tidx) |
| 517 | if prev_ and valid_prev(prev_) and valid_next(next_): |
| 518 | from_idx, to_idx = post(tlist, pidx, tidx, nidx) |
| 519 | grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend) |
| 520 | |
| 521 | tidx_offset += to_idx - from_idx |
| 522 | pidx, prev_ = from_idx, grp |
| 523 | continue |
| 524 | |
| 525 | pidx, prev_ = tidx, token |
no test coverage detected
searching dependent graphs…