aboutsummaryrefslogtreecommitdiff
path: root/mammoth/styles/parser/token_iterator.py
diff options
context:
space:
mode:
Diffstat (limited to 'mammoth/styles/parser/token_iterator.py')
-rw-r--r--mammoth/styles/parser/token_iterator.py22
1 files changed, 20 insertions, 2 deletions
diff --git a/mammoth/styles/parser/token_iterator.py b/mammoth/styles/parser/token_iterator.py
index 810e50b..c6876e9 100644
--- a/mammoth/styles/parser/token_iterator.py
+++ b/mammoth/styles/parser/token_iterator.py
@@ -1,8 +1,13 @@
+# TODO: check indices
+
class TokenIterator(object):
def __init__(self, tokens):
self._tokens = tokens
self._index = 0
+ def peek_token_type(self):
+ return self._tokens[self._index].type
+
def next_value(self, token_type):
return self._next(token_type).value
@@ -14,9 +19,9 @@ class TokenIterator(object):
else:
raise self._unexpected_token_type(token_type, token)
- def skip(self, token_type, token_value):
+ def skip(self, token_type, token_value=None):
token = self._tokens[self._index]
- if token.type == token_type and token.value == token_value:
+ if token.type == token_type and (token_value is None or token.value == token_value):
self._index += 1
return True
else:
@@ -29,3 +34,16 @@ class TokenIterator(object):
return True
else:
return False
+
+ def try_skip_many(self, tokens):
+ start = self._index
+ for token_type, token_value in tokens:
+ token = self._tokens[self._index]
+ if not (token.type == token_type and (token_value is None or token.value == token_value)):
+ self._index = start
+ return False
+ else:
+ self._index += 1
+
+ return True
+