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)
| 4598 | |
| 4599 | |
| 4600 | def _GetTextInside(text, start_pattern): |
| 4601 | r"""Retrieves all the text between matching open and close parentheses. |
| 4602 | |
| 4603 | Given a string of lines and a regular expression string, retrieve all the text |
| 4604 | following the expression and between opening punctuation symbols like |
| 4605 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 4606 | occurrences of the punctuations, so for the text like |
| 4607 | printf(a(), b(c())); |
| 4608 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 4609 | start_pattern must match string having an open punctuation symbol at the end. |
| 4610 | |
| 4611 | Args: |
| 4612 | text: The lines to extract text. Its comments and strings must be elided. |
| 4613 | It can be single line and can span multiple lines. |
| 4614 | start_pattern: The regexp string indicating where to start extracting |
| 4615 | the text. |
| 4616 | Returns: |
| 4617 | The extracted text. |
| 4618 | None if either the opening string or ending punctuation could not be found. |
| 4619 | """ |
| 4620 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
| 4621 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 4622 | |
| 4623 | # Give opening punctuations to get the matching close-punctuations. |
| 4624 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
| 4625 | closing_punctuation = set(matching_punctuation.itervalues()) |
| 4626 | |
| 4627 | # Find the position to start extracting text. |
| 4628 | match = re.search(start_pattern, text, re.M) |
| 4629 | if not match: # start_pattern not found in text. |
| 4630 | return None |
| 4631 | start_position = match.end(0) |
| 4632 | |
| 4633 | assert start_position > 0, ( |
| 4634 | 'start_pattern must ends with an opening punctuation.') |
| 4635 | assert text[start_position - 1] in matching_punctuation, ( |
| 4636 | 'start_pattern must ends with an opening punctuation.') |
| 4637 | # Stack of closing punctuations we expect to have in text after position. |
| 4638 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 4639 | position = start_position |
| 4640 | while punctuation_stack and position < len(text): |
| 4641 | if text[position] == punctuation_stack[-1]: |
| 4642 | punctuation_stack.pop() |
| 4643 | elif text[position] in closing_punctuation: |
| 4644 | # A closing punctuation without matching opening punctuations. |
| 4645 | return None |
| 4646 | elif text[position] in matching_punctuation: |
| 4647 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 4648 | position += 1 |
| 4649 | if punctuation_stack: |
| 4650 | # Opening punctuations left without matching close-punctuations. |
| 4651 | return None |
| 4652 | # punctuations match. |
| 4653 | return text[start_position:position - 1] |
| 4654 | |
| 4655 | |
| 4656 | # Patterns for matching call-by-reference parameters. |
no test coverage detected