Meta: Extend the python lexer with some more APIs

Add a few GenericLexer-style helpers to lexer.py:

* tell()
* ignore()
* next_is()
This commit is contained in:
Shannon Booth 2026-04-26 12:27:49 +02:00 committed by Sam Atkins
parent 4a9deb3afa
commit af5290b41b

View file

@ -13,6 +13,9 @@ class Lexer:
self.text = text
self.position = 0
def tell(self) -> int:
return self.position
def is_eof(self) -> bool:
return self.position >= len(self.text)
@ -35,6 +38,9 @@ class Lexer:
return True
return False
def next_is(self, string: str) -> bool:
return self.text.startswith(string, self.position)
def consume_until(self, predicate: Callable[[str], bool]) -> str:
start = self.position
while self.position < len(self.text) and not predicate(self.text[self.position]):
@ -47,6 +53,9 @@ class Lexer:
self.position += 1
return self.text[start : self.position]
def ignore(self, count: int = 1) -> None:
self.position = min(self.position + count, len(self.text))
def ignore_until(self, ch: str) -> None:
while self.position < len(self.text) and self.text[self.position] != ch:
self.position += 1