From af5290b41b0788f5aef3bd57fc697824256948fc Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 26 Apr 2026 12:27:49 +0200 Subject: [PATCH] Meta: Extend the python lexer with some more APIs Add a few GenericLexer-style helpers to lexer.py: * tell() * ignore() * next_is() --- Meta/Utils/lexer.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Meta/Utils/lexer.py b/Meta/Utils/lexer.py index b61e4fe2ef..15725458c1 100644 --- a/Meta/Utils/lexer.py +++ b/Meta/Utils/lexer.py @@ -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