From c8516d0a4e9467d4313af4ba63196bca184812b3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 24 Nov 2016 22:13:00 +0000 Subject: Use recursive descent parser instead of parsimonious --- mammoth/styles/parser/__init__.py | 14 ++++++++++++++ mammoth/styles/parser/errors.py | 2 ++ mammoth/styles/parser/token_iterator.py | 9 ++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 mammoth/styles/parser/errors.py (limited to 'mammoth/styles') 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() + -- cgit v1.2.3