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 proper
(text, start_pattern)
| 5807 | |
| 5808 | |
| 5809 | def _GetTextInside(text, start_pattern): |
| 5810 | r"""Retrieves all the text between matching open and close parentheses. |
| 5811 | |
| 5812 | Given a string of lines and a regular expression string, retrieve all the text |
| 5813 | following the expression and between opening punctuation symbols like |
| 5814 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 5815 | occurrences of the punctuation, so for the text like |
| 5816 | printf(a(), b(c())); |
| 5817 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 5818 | start_pattern must match string having an open punctuation symbol at the end. |
| 5819 | |
| 5820 | Args: |
| 5821 | text: The lines to extract text. Its comments and strings must be elided. |
| 5822 | It can be single line and can span multiple lines. |
| 5823 | start_pattern: The regexp string indicating where to start extracting |
| 5824 | the text. |
| 5825 | Returns: |
| 5826 | The extracted text. |
| 5827 | None if either the opening string or ending punctuation could not be found. |
| 5828 | """ |
| 5829 | # TODO(google): Audit cpplint.py to see what places could be profitably |
| 5830 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 5831 | |
| 5832 | # Give opening punctuation to get the matching close-punctuation. |
| 5833 | matching_punctuation = {"(": ")", "{": "}", "[": "]"} |
| 5834 | closing_punctuation = set(dict.values(matching_punctuation)) |
| 5835 | |
| 5836 | # Find the position to start extracting text. |
| 5837 | match = re.search(start_pattern, text, re.MULTILINE) |
| 5838 | if not match: # start_pattern not found in text. |
| 5839 | return None |
| 5840 | start_position = match.end(0) |
| 5841 | |
| 5842 | assert start_position > 0, "start_pattern must ends with an opening punctuation." |
| 5843 | assert text[start_position - 1] in matching_punctuation, ( |
| 5844 | "start_pattern must ends with an opening punctuation." |
| 5845 | ) |
| 5846 | # Stack of closing punctuation we expect to have in text after position. |
| 5847 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 5848 | position = start_position |
| 5849 | while punctuation_stack and position < len(text): |
| 5850 | if text[position] == punctuation_stack[-1]: |
| 5851 | punctuation_stack.pop() |
| 5852 | elif text[position] in closing_punctuation: |
| 5853 | # A closing punctuation without matching opening punctuation. |
| 5854 | return None |
| 5855 | elif text[position] in matching_punctuation: |
| 5856 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 5857 | position += 1 |
| 5858 | if punctuation_stack: |
| 5859 | # Opening punctuation left without matching close-punctuation. |
| 5860 | return None |
| 5861 | # punctuation match. |
| 5862 | return text[start_position : position - 1] |
| 5863 | |
| 5864 | |
| 5865 | # Patterns for matching call-by-reference parameters. |