aboutsummaryrefslogtreecommitdiff
path: root/mammoth/styles/parser/token_iterator.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-24 21:38:10 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-24 21:38:10 +0000
commit1de9198f0c69152fe6f474185ac351e9500a4ce8 (patch)
treefa3eb932fa35f796548304a7454fb2eb83717d0e /mammoth/styles/parser/token_iterator.py
parent056a2173eeb6d0469fde9a24e1db81d67f8722f5 (diff)
Implement document matcher parser
Diffstat (limited to 'mammoth/styles/parser/token_iterator.py')
-rw-r--r--mammoth/styles/parser/token_iterator.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/mammoth/styles/parser/token_iterator.py b/mammoth/styles/parser/token_iterator.py
new file mode 100644
index 0000000..810e50b
--- /dev/null
+++ b/mammoth/styles/parser/token_iterator.py
@@ -0,0 +1,31 @@
+class TokenIterator(object):
+ def __init__(self, tokens):
+ self._tokens = tokens
+ self._index = 0
+
+ def next_value(self, token_type):
+ return self._next(token_type).value
+
+ def _next(self, token_type):
+ token = self._tokens[self._index]
+ if token.type == token_type:
+ self._index += 1
+ return token
+ else:
+ raise self._unexpected_token_type(token_type, token)
+
+ def skip(self, token_type, token_value):
+ token = self._tokens[self._index]
+ if token.type == token_type and token.value == token_value:
+ self._index += 1
+ return True
+ else:
+ raise self._unexpected_token_type(token_type, token)
+
+ def try_skip(self, token_type, token_value):
+ token = self._tokens[self._index]
+ if token.type == token_type and token.value == token_value:
+ self._index += 1
+ return True
+ else:
+ return False