blob: 96704ec1ab3c5580b56d2d11b488960530918539 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 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
|