summaryrefslogtreecommitdiff
path: root/projects/hackc/tokens.py
diff options
context:
space:
mode:
Diffstat (limited to 'projects/hackc/tokens.py')
-rw-r--r--projects/hackc/tokens.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/projects/hackc/tokens.py b/projects/hackc/tokens.py
index 50c4173..1ed94ae 100644
--- a/projects/hackc/tokens.py
+++ b/projects/hackc/tokens.py
@@ -26,6 +26,7 @@ KEYWORDS = [
SYMBOLS = "{}()[].,;+-*/&|<>=~"
TOKEN_TYPES = ["keyword", "symbol", "integer", "string", "identifier"]
+
class Token:
def __init__(self, type: str, token: str, line_no: int, column: int):
"""A token in JACK."""
@@ -34,6 +35,18 @@ class Token:
self.line_no = line_no
self.column = column
+ def __len__(self) -> int:
+ return self.length()
+
+ def __eq__(self, other) -> bool:
+ if type(other) == str:
+ return self.token == other
+ if type(other) == Token:
+ return self.token == other.token
+
+ def __str__(self) -> str:
+ return self.token
+
@classmethod
def from_line(cls, line: str, line_no: int, column: int, extensions=[]):
"""Extract first token from line and return it as an instance of Token."""