eb412699b6
Seems to successfully handle inputs like... ``` > (-fname:"William" lname:/C.*l/) OR (fname:"William" -lname:"Carroll") ``` Change-Id: I5277cfbc7d102158eab5e1e71b2d95aaf13508fd Reviewed-on: https://cl.tvl.fyi/c/depot/+/5340 Reviewed-by: wpcarro <wpcarro@gmail.com> Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
31 lines
803 B
Python
31 lines
803 B
Python
class Parser(object):
|
|
def __init__(self, tokens):
|
|
self.tokens = tokens
|
|
self.i = 0
|
|
|
|
def exhausted(self):
|
|
return self.i >= len(self.tokens)
|
|
|
|
def peek(self, n=0):
|
|
return self.tokens[self.i + n]
|
|
|
|
def advance(self):
|
|
if not self.exhausted():
|
|
self.i += 1
|
|
return self.peek(n=-1)
|
|
|
|
def match(self, xs):
|
|
if self.peek() in xs:
|
|
self.advance()
|
|
return True
|
|
return False
|
|
|
|
def test(self, predicate):
|
|
return predicate(self.tokens, self.i)
|
|
|
|
def expect(self, predicate):
|
|
if self.exhausted():
|
|
raise Exception("Unexpected EOL")
|
|
if predicate(self.peek()):
|
|
return self.advance()
|
|
raise Exception("Unexpected token: \"{}\"".format(self.peek()))
|