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)
| 4677 | |
| 4678 | |
| 4679 | def _GetTextInside(text, start_pattern): |
| 4680 | r"""Retrieves all the text between matching open and close parentheses. |
| 4681 | |
| 4682 | Given a string of lines and a regular expression string, retrieve all the text |
| 4683 | following the expression and between opening punctuation symbols like |
| 4684 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 4685 | occurrences of the punctuations, so for the text like |
| 4686 | printf(a(), b(c())); |
| 4687 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 4688 | start_pattern must match string having an open punctuation symbol at the end. |
| 4689 | |
| 4690 | Args: |
| 4691 | text: The lines to extract text. Its comments and strings must be elided. |
| 4692 | It can be single line and can span multiple lines. |
| 4693 | start_pattern: The regexp string indicating where to start extracting |
| 4694 | the text. |
| 4695 | Returns: |
| 4696 | The extracted text. |
| 4697 | None if either the opening string or ending punctuation could not be found. |
| 4698 | """ |
| 4699 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
| 4700 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 4701 | |
| 4702 | # Give opening punctuations to get the matching close-punctuations. |
| 4703 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
| 4704 | closing_punctuation = set(matching_punctuation.itervalues()) |
| 4705 | |
| 4706 | # Find the position to start extracting text. |
| 4707 | match = re.search(start_pattern, text, re.M) |
| 4708 | if not match: # start_pattern not found in text. |
| 4709 | return None |
| 4710 | start_position = match.end(0) |
| 4711 | |
| 4712 | assert start_position > 0, ( |
| 4713 | 'start_pattern must ends with an opening punctuation.') |
| 4714 | assert text[start_position - 1] in matching_punctuation, ( |
| 4715 | 'start_pattern must ends with an opening punctuation.') |
| 4716 | # Stack of closing punctuations we expect to have in text after position. |
| 4717 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 4718 | position = start_position |
| 4719 | while punctuation_stack and position < len(text): |
| 4720 | if text[position] == punctuation_stack[-1]: |
| 4721 | punctuation_stack.pop() |
| 4722 | elif text[position] in closing_punctuation: |
| 4723 | # A closing punctuation without matching opening punctuations. |
| 4724 | return None |
| 4725 | elif text[position] in matching_punctuation: |
| 4726 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 4727 | position += 1 |
| 4728 | if punctuation_stack: |
| 4729 | # Opening punctuations left without matching close-punctuations. |
| 4730 | return None |
| 4731 | # punctuations match. |
| 4732 | return text[start_position:position - 1] |
| 4733 | |
| 4734 | |
| 4735 | # Patterns for matching call-by-reference parameters. |
no test coverage detected