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