Returns a list of 2-tuples (condition, value). If an ELSE exists condition is None.
(self, skip_ws=False)
| 571 | M_CLOSE = T.Keyword, 'END' |
| 572 | |
| 573 | def get_cases(self, skip_ws=False): |
| 574 | """Returns a list of 2-tuples (condition, value). |
| 575 | |
| 576 | If an ELSE exists condition is None. |
| 577 | """ |
| 578 | CONDITION = 1 |
| 579 | VALUE = 2 |
| 580 | |
| 581 | ret = [] |
| 582 | mode = CONDITION |
| 583 | |
| 584 | for token in self.tokens: |
| 585 | # Set mode from the current statement |
| 586 | if token.match(T.Keyword, 'CASE'): |
| 587 | continue |
| 588 | |
| 589 | elif skip_ws and token.ttype in T.Whitespace: |
| 590 | continue |
| 591 | |
| 592 | elif token.match(T.Keyword, 'WHEN'): |
| 593 | ret.append(([], [])) |
| 594 | mode = CONDITION |
| 595 | |
| 596 | elif token.match(T.Keyword, 'THEN'): |
| 597 | mode = VALUE |
| 598 | |
| 599 | elif token.match(T.Keyword, 'ELSE'): |
| 600 | ret.append((None, [])) |
| 601 | mode = VALUE |
| 602 | |
| 603 | elif token.match(T.Keyword, 'END'): |
| 604 | mode = None |
| 605 | |
| 606 | # First condition without preceding WHEN |
| 607 | if mode and not ret: |
| 608 | ret.append(([], [])) |
| 609 | |
| 610 | # Append token depending of the current mode |
| 611 | if mode == CONDITION: |
| 612 | ret[-1][0].append(token) |
| 613 | |
| 614 | elif mode == VALUE: |
| 615 | ret[-1][1].append(token) |
| 616 | |
| 617 | # Return cases list |
| 618 | return ret |
| 619 | |
| 620 | |
| 621 | class Function(NameAliasMixin, TokenList): |
no test coverage detected