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