1"""General tools which don't depend on other parts of Parsimonious"""
2
3import ast
4
5
6class StrAndRepr(object):
7 """Mix-in which gives the class the same __repr__ and __str__."""
8
9 def __repr__(self):
10 return self.__str__()
11
12
13def evaluate_string(string):
14 """Piggyback on Python's string support so we can have backslash escaping
15 and niceties like \n, \t, etc.
16
17 This also supports:
18 1. b"strings", allowing grammars to parse bytestrings, in addition to str.
19 2. r"strings" to simplify regexes.
20 """
21 return ast.literal_eval(string)
22
23
24class Token(StrAndRepr):
25 """A class to represent tokens, for use with TokenGrammars
26
27 You will likely want to subclass this to hold additional information, like
28 the characters that you lexed to create this token. Alternately, feel free
29 to create your own class from scratch. The only contract is that tokens
30 must have a ``type`` attr.
31
32 """
33 __slots__ = ['type']
34
35 def __init__(self, type):
36 self.type = type
37
38 def __str__(self):
39 return '<Token "%s">' % (self.type,)
40
41 def __eq__(self, other):
42 return self.type == other.type