@@ 4752-4805 (lines=54) @@ | ||
4749 | ||
4750 | ||
4751 | ||
4752 | def _GetTextInside(text, start_pattern): |
|
4753 | r"""Retrieves all the text between matching open and close parentheses. |
|
4754 | ||
4755 | Given a string of lines and a regular expression string, retrieve all the text |
|
4756 | following the expression and between opening punctuation symbols like |
|
4757 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
|
4758 | occurrences of the punctuations, so for the text like |
|
4759 | printf(a(), b(c())); |
|
4760 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
|
4761 | start_pattern must match string having an open punctuation symbol at the end. |
|
4762 | ||
4763 | Args: |
|
4764 | text: The lines to extract text. Its comments and strings must be elided. |
|
4765 | It can be single line and can span multiple lines. |
|
4766 | start_pattern: The regexp string indicating where to start extracting |
|
4767 | the text. |
|
4768 | Returns: |
|
4769 | The extracted text. |
|
4770 | None if either the opening string or ending punctuation could not be found. |
|
4771 | """ |
|
4772 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
|
4773 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
|
4774 | ||
4775 | # Give opening punctuations to get the matching close-punctuations. |
|
4776 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
|
4777 | closing_punctuation = set(itervalues(matching_punctuation)) |
|
4778 | ||
4779 | # Find the position to start extracting text. |
|
4780 | match = re.search(start_pattern, text, re.M) |
|
4781 | if not match: # start_pattern not found in text. |
|
4782 | return None |
|
4783 | start_position = match.end(0) |
|
4784 | ||
4785 | assert start_position > 0, ( |
|
4786 | 'start_pattern must ends with an opening punctuation.') |
|
4787 | assert text[start_position - 1] in matching_punctuation, ( |
|
4788 | 'start_pattern must ends with an opening punctuation.') |
|
4789 | # Stack of closing punctuations we expect to have in text after position. |
|
4790 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
|
4791 | position = start_position |
|
4792 | while punctuation_stack and position < len(text): |
|
4793 | if text[position] == punctuation_stack[-1]: |
|
4794 | punctuation_stack.pop() |
|
4795 | elif text[position] in closing_punctuation: |
|
4796 | # A closing punctuation without matching opening punctuations. |
|
4797 | return None |
|
4798 | elif text[position] in matching_punctuation: |
|
4799 | punctuation_stack.append(matching_punctuation[text[position]]) |
|
4800 | position += 1 |
|
4801 | if punctuation_stack: |
|
4802 | # Opening punctuations left without matching close-punctuations. |
|
4803 | return None |
|
4804 | # punctuations match. |
|
4805 | return text[start_position:position - 1] |
|
4806 | ||
4807 | ||
4808 | # Patterns for matching call-by-reference parameters. |
@@ 4752-4805 (lines=54) @@ | ||
4749 | ||
4750 | ||
4751 | ||
4752 | def _GetTextInside(text, start_pattern): |
|
4753 | r"""Retrieves all the text between matching open and close parentheses. |
|
4754 | ||
4755 | Given a string of lines and a regular expression string, retrieve all the text |
|
4756 | following the expression and between opening punctuation symbols like |
|
4757 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
|
4758 | occurrences of the punctuations, so for the text like |
|
4759 | printf(a(), b(c())); |
|
4760 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
|
4761 | start_pattern must match string having an open punctuation symbol at the end. |
|
4762 | ||
4763 | Args: |
|
4764 | text: The lines to extract text. Its comments and strings must be elided. |
|
4765 | It can be single line and can span multiple lines. |
|
4766 | start_pattern: The regexp string indicating where to start extracting |
|
4767 | the text. |
|
4768 | Returns: |
|
4769 | The extracted text. |
|
4770 | None if either the opening string or ending punctuation could not be found. |
|
4771 | """ |
|
4772 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
|
4773 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
|
4774 | ||
4775 | # Give opening punctuations to get the matching close-punctuations. |
|
4776 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
|
4777 | closing_punctuation = set(itervalues(matching_punctuation)) |
|
4778 | ||
4779 | # Find the position to start extracting text. |
|
4780 | match = re.search(start_pattern, text, re.M) |
|
4781 | if not match: # start_pattern not found in text. |
|
4782 | return None |
|
4783 | start_position = match.end(0) |
|
4784 | ||
4785 | assert start_position > 0, ( |
|
4786 | 'start_pattern must ends with an opening punctuation.') |
|
4787 | assert text[start_position - 1] in matching_punctuation, ( |
|
4788 | 'start_pattern must ends with an opening punctuation.') |
|
4789 | # Stack of closing punctuations we expect to have in text after position. |
|
4790 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
|
4791 | position = start_position |
|
4792 | while punctuation_stack and position < len(text): |
|
4793 | if text[position] == punctuation_stack[-1]: |
|
4794 | punctuation_stack.pop() |
|
4795 | elif text[position] in closing_punctuation: |
|
4796 | # A closing punctuation without matching opening punctuations. |
|
4797 | return None |
|
4798 | elif text[position] in matching_punctuation: |
|
4799 | punctuation_stack.append(matching_punctuation[text[position]]) |
|
4800 | position += 1 |
|
4801 | if punctuation_stack: |
|
4802 | # Opening punctuations left without matching close-punctuations. |
|
4803 | return None |
|
4804 | # punctuations match. |
|
4805 | return text[start_position:position - 1] |
|
4806 | ||
4807 | ||
4808 | # Patterns for matching call-by-reference parameters. |