summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-24 21:00:44 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-24 21:00:44 +0000
commit056a2173eeb6d0469fde9a24e1db81d67f8722f5 (patch)
tree5facda1b431753653202827b82581796421c8dcb
parent1b3d07bca1a8237052c0a81ff78b100662c0c19d (diff)
Extract token type constants
-rw-r--r--mammoth/styles/parser/tokeniser.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/mammoth/styles/parser/tokeniser.py b/mammoth/styles/parser/tokeniser.py
index 2e8c2c2..c9eb4cf 100644
--- a/mammoth/styles/parser/tokeniser.py
+++ b/mammoth/styles/parser/tokeniser.py
@@ -5,6 +5,16 @@ import re
Token = collections.namedtuple("Token", ["character_index", "type", "value"])
+class TokenType(object):
+ IDENTIFIER = "identifier"
+ SYMBOL = "symbol"
+ WHITESPACE = "whitespace"
+ STRING = "string"
+ UNTERMINATED_STRING = "unterminated string"
+ INTEGER = "integer"
+
+
+
def regex_tokeniser(rules):
rules = [(token_type, _to_regex(regex)) for token_type, regex in rules]
rules.append(("unknown", re.compile(".")))
@@ -41,10 +51,10 @@ _string_prefix = r"'(?:\\.|[^'])*"
_identifier_character = r"(?:[a-zA-Z\-_]|\\.)"
tokenise = regex_tokeniser([
- ("identifier", _identifier_character + "(?:" + _identifier_character + "|[0-9])*"),
- ("symbol", r":|>|=>|\^=|=|\(|\)|\[|\]|\||!|\."),
- ("whitespace", r"\s+"),
- ("string", _string_prefix + "'"),
- ("unterminated string", _string_prefix),
- ("integer", "([0-9]+)"),
+ (TokenType.IDENTIFIER, _identifier_character + "(?:" + _identifier_character + "|[0-9])*"),
+ (TokenType.SYMBOL, r":|>|=>|\^=|=|\(|\)|\[|\]|\||!|\."),
+ (TokenType.WHITESPACE, r"\s+"),
+ (TokenType.STRING, _string_prefix + "'"),
+ (TokenType.UNTERMINATED_STRING, _string_prefix),
+ (TokenType.INTEGER, "([0-9]+)"),
])