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)
| 4520 | |
| 4521 | |
| 4522 | def _GetTextInside(text, start_pattern): |
| 4523 | r"""Retrieves all the text between matching open and close parentheses. |
| 4524 | |
| 4525 | Given a string of lines and a regular expression string, retrieve all the text |
| 4526 | following the expression and between opening punctuation symbols like |
| 4527 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 4528 | occurrences of the punctuations, so for the text like |
| 4529 | printf(a(), b(c())); |
| 4530 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 4531 | start_pattern must match string having an open punctuation symbol at the end. |
| 4532 | |
| 4533 | Args: |
| 4534 | text: The lines to extract text. Its comments and strings must be elided. |
| 4535 | It can be single line and can span multiple lines. |
| 4536 | start_pattern: The regexp string indicating where to start extracting |
| 4537 | the text. |
| 4538 | Returns: |
| 4539 | The extracted text. |
| 4540 | None if either the opening string or ending punctuation could not be found. |
| 4541 | """ |
| 4542 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
| 4543 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 4544 | |
| 4545 | # Give opening punctuations to get the matching close-punctuations. |
| 4546 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
| 4547 | closing_punctuation = set(matching_punctuation.values()) |
| 4548 | |
| 4549 | # Find the position to start extracting text. |
| 4550 | match = re.search(start_pattern, text, re.M) |
| 4551 | if not match: # start_pattern not found in text. |
| 4552 | return None |
| 4553 | start_position = match.end(0) |
| 4554 | |
| 4555 | assert start_position > 0, ( |
| 4556 | 'start_pattern must ends with an opening punctuation.') |
| 4557 | assert text[start_position - 1] in matching_punctuation, ( |
| 4558 | 'start_pattern must ends with an opening punctuation.') |
| 4559 | # Stack of closing punctuations we expect to have in text after position. |
| 4560 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 4561 | position = start_position |
| 4562 | while punctuation_stack and position < len(text): |
| 4563 | if text[position] == punctuation_stack[-1]: |
| 4564 | punctuation_stack.pop() |
| 4565 | elif text[position] in closing_punctuation: |
| 4566 | # A closing punctuation without matching opening punctuations. |
| 4567 | return None |
| 4568 | elif text[position] in matching_punctuation: |
| 4569 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 4570 | position += 1 |
| 4571 | if punctuation_stack: |
| 4572 | # Opening punctuations left without matching close-punctuations. |
| 4573 | return None |
| 4574 | # punctuations match. |
| 4575 | return text[start_position:position - 1] |
| 4576 | |
| 4577 | |
| 4578 | # Patterns for matching call-by-reference parameters. |
no test coverage detected