Check if expression looks like a type name, returns true if so. Args: clean_lines: A CleansedLines instance containing the file. nesting_state: A NestingState instance which maintains information about the current stack of nested blocks being parsed. expr:
(clean_lines, nesting_state, expr)
| 4556 | |
| 4557 | |
| 4558 | def _IsType(clean_lines, nesting_state, expr): |
| 4559 | """Check if expression looks like a type name, returns true if so. |
| 4560 | |
| 4561 | Args: |
| 4562 | clean_lines: A CleansedLines instance containing the file. |
| 4563 | nesting_state: A NestingState instance which maintains information about |
| 4564 | the current stack of nested blocks being parsed. |
| 4565 | expr: The expression to check. |
| 4566 | Returns: |
| 4567 | True, if token looks like a type. |
| 4568 | """ |
| 4569 | # Keep only the last token in the expression |
| 4570 | if last_word := re.match(r"^.*(\b\S+)$", expr): |
| 4571 | token = last_word.group(1) |
| 4572 | else: |
| 4573 | token = expr |
| 4574 | |
| 4575 | # Match native types and stdint types |
| 4576 | if _TYPES.match(token): |
| 4577 | return True |
| 4578 | |
| 4579 | # Try a bit harder to match templated types. Walk up the nesting |
| 4580 | # stack until we find something that resembles a typename |
| 4581 | # declaration for what we are looking for. |
| 4582 | typename_pattern = r"\b(?:typename|class|struct)\s+" + re.escape(token) + r"\b" |
| 4583 | block_index = len(nesting_state.stack) - 1 |
| 4584 | while block_index >= 0: |
| 4585 | if isinstance(nesting_state.stack[block_index], _NamespaceInfo): |
| 4586 | return False |
| 4587 | |
| 4588 | # Found where the opening brace is. We want to scan from this |
| 4589 | # line up to the beginning of the function, minus a few lines. |
| 4590 | # template <typename Type1, // stop scanning here |
| 4591 | # ...> |
| 4592 | # class C |
| 4593 | # : public ... { // start scanning here |
| 4594 | last_line = nesting_state.stack[block_index].starting_linenum |
| 4595 | |
| 4596 | next_block_start = 0 |
| 4597 | if block_index > 0: |
| 4598 | next_block_start = nesting_state.stack[block_index - 1].starting_linenum |
| 4599 | first_line = last_line |
| 4600 | while first_line >= next_block_start: |
| 4601 | if clean_lines.elided[first_line].find("template") >= 0: |
| 4602 | break |
| 4603 | first_line -= 1 |
| 4604 | if first_line < next_block_start: |
| 4605 | # Didn't find any "template" keyword before reaching the next block, |
| 4606 | # there are probably no template things to check for this block |
| 4607 | block_index -= 1 |
| 4608 | continue |
| 4609 | |
| 4610 | # Look for typename in the specified range |
| 4611 | for i in range(first_line, last_line + 1, 1): |
| 4612 | if re.search(typename_pattern, clean_lines.elided[i]): |
| 4613 | return True |
| 4614 | block_index -= 1 |
| 4615 |