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(unknown): cpplint spends a fair bit of time matching parentheses. Ideally we wou
(clean_lines, linenum, pos)
| 2076 | |
| 2077 | |
| 2078 | def CloseExpression(clean_lines, linenum, pos): |
| 2079 | """If input points to ( or { or [ or <, finds the position that closes it. |
| 2080 | |
| 2081 | If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the |
| 2082 | linenum/pos that correspond to the closing of the expression. |
| 2083 | |
| 2084 | TODO(unknown): cpplint spends a fair bit of time matching parentheses. |
| 2085 | Ideally we would want to index all opening and closing parentheses once |
| 2086 | and have CloseExpression be just a simple lookup, but due to preprocessor |
| 2087 | tricks, this is not so easy. |
| 2088 | |
| 2089 | Args: |
| 2090 | clean_lines: A CleansedLines instance containing the file. |
| 2091 | linenum: The number of the line to check. |
| 2092 | pos: A position on the line. |
| 2093 | |
| 2094 | Returns: |
| 2095 | A tuple (line, linenum, pos) pointer *past* the closing brace, or |
| 2096 | (line, len(lines), -1) if we never find a close. Note we ignore |
| 2097 | strings and comments when matching; and the line we return is the |
| 2098 | 'cleansed' line at linenum. |
| 2099 | """ |
| 2100 | |
| 2101 | line = clean_lines.elided[linenum] |
| 2102 | if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]): |
| 2103 | return (line, clean_lines.NumLines(), -1) |
| 2104 | |
| 2105 | # Check first line |
| 2106 | (end_pos, stack) = FindEndOfExpressionInLine(line, pos, []) |
| 2107 | if end_pos > -1: |
| 2108 | return (line, linenum, end_pos) |
| 2109 | |
| 2110 | # Continue scanning forward |
| 2111 | while stack and linenum < clean_lines.NumLines() - 1: |
| 2112 | linenum += 1 |
| 2113 | line = clean_lines.elided[linenum] |
| 2114 | (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack) |
| 2115 | if end_pos > -1: |
| 2116 | return (line, linenum, end_pos) |
| 2117 | |
| 2118 | # Did not find end of expression before end of file, give up |
| 2119 | return (line, clean_lines.NumLines(), -1) |
| 2120 | |
| 2121 | |
| 2122 | def FindStartOfExpressionInLine(line, endpos, stack): |
no test coverage detected