Better token wrapper for tokenize module.
| 103 | |
| 104 | |
| 105 | class Token: |
| 106 | """Better token wrapper for tokenize module.""" |
| 107 | |
| 108 | def __init__( |
| 109 | self, |
| 110 | kind: int, |
| 111 | value: Any, |
| 112 | start: tuple[int, int], |
| 113 | end: tuple[int, int], |
| 114 | source: str, |
| 115 | ) -> None: |
| 116 | self.kind = kind |
| 117 | self.value = value |
| 118 | self.start = start |
| 119 | self.end = end |
| 120 | self.source = source |
| 121 | |
| 122 | def __eq__(self, other: object) -> bool: |
| 123 | if isinstance(other, int): |
| 124 | return self.kind == other |
| 125 | elif isinstance(other, str): |
| 126 | return self.value == other |
| 127 | elif isinstance(other, (list, tuple)): |
| 128 | return [self.kind, self.value] == list(other) |
| 129 | elif other is None: |
| 130 | return False |
| 131 | else: |
| 132 | raise ValueError('Unknown value: %r' % other) |
| 133 | |
| 134 | def __hash__(self) -> int: |
| 135 | return hash((self.kind, self.value, self.start, self.end, self.source)) |
| 136 | |
| 137 | def match(self, *conditions: Any) -> bool: |
| 138 | return any(self == candidate for candidate in conditions) |
| 139 | |
| 140 | def __repr__(self) -> str: |
| 141 | return f'<Token kind={tokenize.tok_name[self.kind]!r} value={self.value.strip()!r}>' |
| 142 | |
| 143 | |
| 144 | class TokenProcessor: |
no outgoing calls
no test coverage detected