diff options
author | William Carroll <wpcarro@gmail.com> | 2020-11-14T13·58+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-11-14T14·00+0000 |
commit | c841527f616ffb4a6cd64f941421cf42f54b7d00 (patch) | |
tree | 09264aa849444fa1e648691464e7890427211afb /scratch/facebook/moderate/parser.py | |
parent | bfd2180e6bb0cdbcc3214096854416265b2ad8f4 (diff) |
Write encoded XML parser and pretty-printer
Write a function that reads a string of compressed XML and outputs the decompressed version. Note to self: Now that I'm growing more comfortable writing parsers, I'd like to become equally comfortable writing pretty-printers.
Diffstat (limited to 'scratch/facebook/moderate/parser.py')
-rw-r--r-- | scratch/facebook/moderate/parser.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/scratch/facebook/moderate/parser.py b/scratch/facebook/moderate/parser.py new file mode 100644 index 000000000000..57dfb058c037 --- /dev/null +++ b/scratch/facebook/moderate/parser.py @@ -0,0 +1,37 @@ +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 next(self): + return self.tokens[self.i + 1] + + 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())) + return self.prev() + + def expect_predicate(self, predicate): + if predicate(self.curr()): + return self.consume() + raise Exception("Expected token \"{}\" to pass predicate, but it did not".format(self.curr())) + + def exhausted(self): + return self.i >= len(self.tokens) |