-
Notifications
You must be signed in to change notification settings - Fork 5
/
tokens.py
61 lines (42 loc) · 1.05 KB
/
tokens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Various str wrapper classes for different kinds of tokens."""
class Token:
"Represents a token from scanning."
def __init__(self, token):
self._text = str(token)
def __str__(self):
return self._text
def __eq__(self, rhs):
return rhs == self._text
def __getitem__(self, rhs):
return self._text[rhs]
def __hash__(self):
return hash(self._text)
def __repr__(self):
# Grab just the name of the (sub)class from type(self)
className = str(type(self))[8:-2]
if className[:7] == "tokens.":
className = className[7:]
return f"{className}({self._text})"
class Command(Token):
pass
class Operator(Token):
pass
class Symbol(Token):
pass
class Name(Token):
pass
class Literal(Token):
pass
class Number(Literal):
pass
class String(Literal):
pass
class Pattern(Literal):
pass
class Char(Literal):
pass
class EscapedString(Literal):
pass
class Nil(Literal):
def __init__(self):
self._text = "()"