aboutsummaryrefslogtreecommitdiff
path: root/mammoth/styles
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-24 22:13:00 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-24 22:15:35 +0000
commitc8516d0a4e9467d4313af4ba63196bca184812b3 (patch)
treedd6235081faf3e3fba7ddda3535d6c48654fb781 /mammoth/styles
parent507700470b46329ea8b1f8191e48fa69a24a8deb (diff)
Use recursive descent parser instead of parsimonious
Diffstat (limited to 'mammoth/styles')
-rw-r--r--mammoth/styles/parser/__init__.py14
-rw-r--r--mammoth/styles/parser/errors.py2
-rw-r--r--mammoth/styles/parser/token_iterator.py9
3 files changed, 24 insertions, 1 deletions
diff --git a/mammoth/styles/parser/__init__.py b/mammoth/styles/parser/__init__.py
index e69de29..2266467 100644
--- a/mammoth/styles/parser/__init__.py
+++ b/mammoth/styles/parser/__init__.py
@@ -0,0 +1,14 @@
+from .errors import LineParseError
+from .style_mapping_parser import parse_style_mapping
+from .tokeniser import tokenise
+from .token_iterator import TokenIterator
+from ... import results
+
+
+def read_style_mapping(string):
+ try:
+ tokens = tokenise(string)
+ return results.success(parse_style_mapping(TokenIterator(tokens)))
+ except LineParseError:
+ warning = "Did not understand this style mapping, so ignored it: " + string
+ return results.Result(None, [results.warning(warning)])
diff --git a/mammoth/styles/parser/errors.py b/mammoth/styles/parser/errors.py
new file mode 100644
index 0000000..ba45e87
--- /dev/null
+++ b/mammoth/styles/parser/errors.py
@@ -0,0 +1,2 @@
+class LineParseError(Exception):
+ pass
diff --git a/mammoth/styles/parser/token_iterator.py b/mammoth/styles/parser/token_iterator.py
index 114ce28..027775b 100644
--- a/mammoth/styles/parser/token_iterator.py
+++ b/mammoth/styles/parser/token_iterator.py
@@ -1,4 +1,8 @@
# TODO: check indices
+# TODO: proper tests for unexpected tokens
+
+from .errors import LineParseError
+
class TokenIterator(object):
def __init__(self, tokens):
@@ -46,4 +50,7 @@ class TokenIterator(object):
self._index += 1
return True
-
+
+ def _unexpected_token_type(self, token_type, token):
+ raise LineParseError()
+