If input points to ( or { or [ or <, finds the position that closes it. If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the linenum/pos that correspond to the closing of the expression. TODO(google): cpplint spends a fair bit of time matching parentheses. Ideally
(clean_lines, linenum, pos)
| 2271 | |
| 2272 | |
| 2273 | def CloseExpression(clean_lines, linenum, pos): |
| 2274 | """If input points to ( or { or [ or <, finds the position that closes it. |
| 2275 | |
| 2276 | If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the |
| 2277 | linenum/pos that correspond to the closing of the expression. |
| 2278 | |
| 2279 | TODO(google): cpplint spends a fair bit of time matching parentheses. |
| 2280 | Ideally we would want to index all opening and closing parentheses once |
| 2281 | and have CloseExpression be just a simple lookup, but due to preprocessor |
| 2282 | tricks, this is not so easy. |
| 2283 | |
| 2284 | Args: |
| 2285 | clean_lines: A CleansedLines instance containing the file. |
| 2286 | linenum: The number of the line to check. |
| 2287 | pos: A position on the line. |
| 2288 | |
| 2289 | Returns: |
| 2290 | A tuple (line, linenum, pos) pointer *past* the closing brace, or |
| 2291 | (line, len(lines), -1) if we never find a close. Note we ignore |
| 2292 | strings and comments when matching; and the line we return is the |
| 2293 | 'cleansed' line at linenum. |
| 2294 | """ |
| 2295 | |
| 2296 | line = clean_lines.elided[linenum] |
| 2297 | if (line[pos] not in "({[<") or re.match(r"<[<=]", line[pos:]): |
| 2298 | return (line, clean_lines.NumLines(), -1) |
| 2299 | |
| 2300 | # Check first line |
| 2301 | (end_pos, stack) = FindEndOfExpressionInLine(line, pos, []) |
| 2302 | if end_pos > -1: |
| 2303 | return (line, linenum, end_pos) |
| 2304 | |
| 2305 | # Continue scanning forward |
| 2306 | while stack and linenum < clean_lines.NumLines() - 1: |
| 2307 | linenum += 1 |
| 2308 | line = clean_lines.elided[linenum] |
| 2309 | (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack) |
| 2310 | if end_pos > -1: |
| 2311 | return (line, linenum, end_pos) |
| 2312 | |
| 2313 | # Did not find end of expression before end of file, give up |
| 2314 | return (line, clean_lines.NumLines(), -1) |
| 2315 | |
| 2316 | |
| 2317 | def FindStartOfExpressionInLine(line, endpos, stack): |
no test coverage detected