r"""Retrieves all the text between matching open and close parentheses. Given a string of lines and a regular expression string, retrieve all the text following the expression and between opening punctuation symbols like (, [, or {, and the matching close-punctuation symbol. This properly nes
(text, start_pattern)
| 5149 | |
| 5150 | |
| 5151 | def _GetTextInside(text, start_pattern): |
| 5152 | r"""Retrieves all the text between matching open and close parentheses. |
| 5153 | |
| 5154 | Given a string of lines and a regular expression string, retrieve all the text |
| 5155 | following the expression and between opening punctuation symbols like |
| 5156 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 5157 | occurrences of the punctuations, so for the text like |
| 5158 | printf(a(), b(c())); |
| 5159 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 5160 | start_pattern must match string having an open punctuation symbol at the end. |
| 5161 | |
| 5162 | Args: |
| 5163 | text: The lines to extract text. Its comments and strings must be elided. |
| 5164 | It can be single line and can span multiple lines. |
| 5165 | start_pattern: The regexp string indicating where to start extracting |
| 5166 | the text. |
| 5167 | Returns: |
| 5168 | The extracted text. |
| 5169 | None if either the opening string or ending punctuation could not be found. |
| 5170 | """ |
| 5171 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
| 5172 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 5173 | |
| 5174 | # Give opening punctuations to get the matching close-punctuations. |
| 5175 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
| 5176 | closing_punctuation = set(itervalues(matching_punctuation)) |
| 5177 | |
| 5178 | # Find the position to start extracting text. |
| 5179 | match = re.search(start_pattern, text, re.M) |
| 5180 | if not match: # start_pattern not found in text. |
| 5181 | return None |
| 5182 | start_position = match.end(0) |
| 5183 | |
| 5184 | assert start_position > 0, ( |
| 5185 | 'start_pattern must ends with an opening punctuation.') |
| 5186 | assert text[start_position - 1] in matching_punctuation, ( |
| 5187 | 'start_pattern must ends with an opening punctuation.') |
| 5188 | # Stack of closing punctuations we expect to have in text after position. |
| 5189 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 5190 | position = start_position |
| 5191 | while punctuation_stack and position < len(text): |
| 5192 | if text[position] == punctuation_stack[-1]: |
| 5193 | punctuation_stack.pop() |
| 5194 | elif text[position] in closing_punctuation: |
| 5195 | # A closing punctuation without matching opening punctuations. |
| 5196 | return None |
| 5197 | elif text[position] in matching_punctuation: |
| 5198 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 5199 | position += 1 |
| 5200 | if punctuation_stack: |
| 5201 | # Opening punctuations left without matching close-punctuations. |
| 5202 | return None |
| 5203 | # punctuations match. |
| 5204 | return text[start_position:position - 1] |
| 5205 | |
| 5206 | |
| 5207 | # Patterns for matching call-by-reference parameters. |
no test coverage detected