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)
| 2131 | |
| 2132 | |
| 2133 | def FindStartOfExpressionInLine(line, endpos, stack): |
| 2134 | """Find position at the matching start of current expression. |
| 2135 | |
| 2136 | This is almost the reverse of FindEndOfExpressionInLine, but note |
| 2137 | that the input position and returned position differs by 1. |
| 2138 | |
| 2139 | Args: |
| 2140 | line: a CleansedLines line. |
| 2141 | endpos: start searching at this position. |
| 2142 | stack: nesting stack at endpos. |
| 2143 | |
| 2144 | Returns: |
| 2145 | On finding matching start: (index at matching start, None) |
| 2146 | On finding an unclosed expression: (-1, None) |
| 2147 | Otherwise: (-1, new stack at beginning of this line) |
| 2148 | """ |
| 2149 | i = endpos |
| 2150 | while i >= 0: |
| 2151 | char = line[i] |
| 2152 | if char in ')]}': |
| 2153 | # Found end of expression, push to expression stack |
| 2154 | stack.append(char) |
| 2155 | elif char == '>': |
| 2156 | # Found potential end of template argument list. |
| 2157 | # |
| 2158 | # Ignore it if it's a "->" or ">=" or "operator>" |
| 2159 | if (i > 0 and |
| 2160 | (line[i - 1] == '-' or |
| 2161 | Match(r'\s>=\s', line[i - 1:]) or |
| 2162 | Search(r'\boperator\s*$', line[0:i]))): |
| 2163 | i -= 1 |
| 2164 | else: |
| 2165 | stack.append('>') |
| 2166 | elif char == '<': |
| 2167 | # Found potential start of template argument list |
| 2168 | if i > 0 and line[i - 1] == '<': |
| 2169 | # Left shift operator |
| 2170 | i -= 1 |
| 2171 | else: |
| 2172 | # If there is a matching '>', we can pop the expression stack. |
| 2173 | # Otherwise, ignore this '<' since it must be an operator. |
| 2174 | if stack and stack[-1] == '>': |
| 2175 | stack.pop() |
| 2176 | if not stack: |
| 2177 | return (i, None) |
| 2178 | elif char in '([{': |
| 2179 | # Found start of expression. |
| 2180 | # |
| 2181 | # If there are any unmatched '>' on the stack, they must be |
| 2182 | # operators. Remove those. |
| 2183 | while stack and stack[-1] == '>': |
| 2184 | stack.pop() |
| 2185 | if not stack: |
| 2186 | return (-1, None) |
| 2187 | if ((char == '(' and stack[-1] == ')') or |
| 2188 | (char == '[' and stack[-1] == ']') or |
| 2189 | (char == '{' and stack[-1] == '}')): |
| 2190 | stack.pop() |
no test coverage detected