Check whether the next token has the provided name. By default, if the check succeeds, the token *must* be read before another check. If `peek` is set to `True`, the token is not loaded and would need to be checked again.
(self, name: str, *, peek: bool = False)
| 111 | self.read() |
| 112 | |
| 113 | def check(self, name: str, *, peek: bool = False) -> bool: |
| 114 | """Check whether the next token has the provided name. |
| 115 | |
| 116 | By default, if the check succeeds, the token *must* be read before |
| 117 | another check. If `peek` is set to `True`, the token is not loaded and |
| 118 | would need to be checked again. |
| 119 | """ |
| 120 | assert ( |
| 121 | self.next_token is None |
| 122 | ), f"Cannot check for {name!r}, already have {self.next_token!r}" |
| 123 | assert name in self.rules, f"Unknown token name: {name!r}" |
| 124 | |
| 125 | expression = self.rules[name] |
| 126 | |
| 127 | match = expression.match(self.source, self.position) |
| 128 | if match is None: |
| 129 | return False |
| 130 | if not peek: |
| 131 | self.next_token = Token(name, match[0], self.position) |
| 132 | return True |
| 133 | |
| 134 | def expect(self, name: str, *, expected: str) -> Token: |
| 135 | """Expect a certain token name next, failing with a syntax error otherwise. |
no test coverage detected