Find position at the matching start of current expression. This is almost the reverse of FindEndOfExpressionInLine, but note that the input position and returned position differs by 1. Args: line: a CleansedLines line. endpos: start searching at this position. stack: nesting stac
(line, endpos, stack)
| 1510 | |
| 1511 | |
| 1512 | def FindStartOfExpressionInLine(line, endpos, stack): |
| 1513 | """Find position at the matching start of current expression. |
| 1514 | |
| 1515 | This is almost the reverse of FindEndOfExpressionInLine, but note |
| 1516 | that the input position and returned position differs by 1. |
| 1517 | |
| 1518 | Args: |
| 1519 | line: a CleansedLines line. |
| 1520 | endpos: start searching at this position. |
| 1521 | stack: nesting stack at endpos. |
| 1522 | |
| 1523 | Returns: |
| 1524 | On finding matching start: (index at matching start, None) |
| 1525 | On finding an unclosed expression: (-1, None) |
| 1526 | Otherwise: (-1, new stack at beginning of this line) |
| 1527 | """ |
| 1528 | i = endpos |
| 1529 | while i >= 0: |
| 1530 | char = line[i] |
| 1531 | if char in ')]}': |
| 1532 | # Found end of expression, push to expression stack |
| 1533 | stack.append(char) |
| 1534 | elif char == '>': |
| 1535 | # Found potential end of template argument list. |
| 1536 | # |
| 1537 | # Ignore it if it's a "->" or ">=" or "operator>" |
| 1538 | if (i > 0 and |
| 1539 | (line[i - 1] == '-' or |
| 1540 | Match(r'\s>=\s', line[i - 1:]) or |
| 1541 | Search(r'\boperator\s*$', line[0:i]))): |
| 1542 | i -= 1 |
| 1543 | else: |
| 1544 | stack.append('>') |
| 1545 | elif char == '<': |
| 1546 | # Found potential start of template argument list |
| 1547 | if i > 0 and line[i - 1] == '<': |
| 1548 | # Left shift operator |
| 1549 | i -= 1 |
| 1550 | else: |
| 1551 | # If there is a matching '>', we can pop the expression stack. |
| 1552 | # Otherwise, ignore this '<' since it must be an operator. |
| 1553 | if stack and stack[-1] == '>': |
| 1554 | stack.pop() |
| 1555 | if not stack: |
| 1556 | return (i, None) |
| 1557 | elif char in '([{': |
| 1558 | # Found start of expression. |
| 1559 | # |
| 1560 | # If there are any unmatched '>' on the stack, they must be |
| 1561 | # operators. Remove those. |
| 1562 | while stack and stack[-1] == '>': |
| 1563 | stack.pop() |
| 1564 | if not stack: |
| 1565 | return (-1, None) |
| 1566 | if ((char == '(' and stack[-1] == ')') or |
| 1567 | (char == '[' and stack[-1] == ']') or |
| 1568 | (char == '{' and stack[-1] == '}')): |
| 1569 | stack.pop() |
no test coverage detected