diff options
author | William Carroll <wpcarro@gmail.com> | 2022-02-28T20·01-0800 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-03-01T02·07+0000 |
commit | b5f78e433c80f4fc29550c6278bf9ef7a4a7dbe2 (patch) | |
tree | 566080a444735cdbea93a7bf6c531ddce321ff82 | |
parent | 88a3051ae5a0b34670a5f43c6fa30a55ad76f8f1 (diff) |
feat(wpcarro/simple-select): Define Parser class r/3881
Copy-paste from monorepo. Change-Id: Iff51fa9b939fe7dec3952077b5764bcc45d96f24 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5339 Reviewed-by: wpcarro <wpcarro@gmail.com> Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
-rw-r--r-- | users/wpcarro/scratch/simple-select/parser.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/simple-select/parser.py b/users/wpcarro/scratch/simple-select/parser.py new file mode 100644 index 000000000000..407bff61c980 --- /dev/null +++ b/users/wpcarro/scratch/simple-select/parser.py @@ -0,0 +1,28 @@ +class Parser(object): + def __init__(self, tokens): + self.tokens = tokens + self.i = 0 + + def prev(self): + return self.tokens[self.i - 1] + + def curr(self): + return self.tokens[self.i] + + def consume(self): + if not self.exhausted(): + self.i += 1 + return self.prev() + + def match(self, xs): + if not self.exhausted() and self.curr() in xs: + self.consume() + return True + return False + + def expect(self, xs): + if not self.match(xs): + raise Exception("Expected token \"{}\" but received \"{}\"".format(xs, self.curr())) + + def exhausted(self): + return self.i >= len(self.tokens) |