tvl-depot/users/wpcarro/scratch/simple-select/scanner.py
William Carroll 7770ccf0e3 feat(wpcarro/simple-select): Support basic Scanner class
...alongside a small REPL to quickly test the functionality.

Change-Id: I3c2b3f060d82cd49488e00dec9b72f7b23e2b666
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5337
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
2022-02-28 18:55:03 +00:00

27 lines
661 B
Python

# According to Crafting Interpreters, the only two primitives that a
# scanner/lexer needs are peek and advance; other functions (e.g. match) are
# nice-to-haves.
class Scanner(object):
def __init__(self, source):
self.i = 0
self.source = source
def exhausted(self):
return self.i >= len(self.source)
def peek(self, n=0):
return self.source[self.i + n] if self.i + n < len(self.source) else '\0'
def advance(self):
result = self.peek()
self.i += 1
return result
def match(self, x):
if self.exhausted():
return False
if self.peek() == x:
self.advance()
return True
else:
return False