turned token class into a dataclass

This commit is contained in:
weckyy702
2021-04-05 20:19:03 +02:00
parent c346df9f6a
commit bb67b3dc0a

View File

@@ -1,7 +1,7 @@
"""Private definitions for Token class used by the Lexer""" """Private definitions for Token class used by the Lexer"""
import re import re
from dataclasses import dataclass, field
from enum import IntEnum from enum import IntEnum
from typing import Union from typing import Union
@@ -44,19 +44,11 @@ class SourceLocation:
def __str__(self) -> str: def __str__(self) -> str:
return f"File {self.filename}, {self.line}:{self.column}" return f"File {self.filename}, {self.line}:{self.column}"
@dataclass(frozen=True, eq=True)
class Token: class Token:
type: Token_type
__slots__ = ["type", "content", "location"] location: SourceLocation = field(compare=False, default=SourceLocation("", 0, 0))
content: str = field(compare=False, default="")
def __init__(self, type: Token_type, location: SourceLocation, content: str) -> None:
self.type = type
self.content = content
self.location = location
def __str__(self) -> str:
if self.content:
return f"{str(self.type)}: {self.content}"
return f"{self.type}"
def make_token(tag: str, location: SourceLocation, type_name_pattern:re.Pattern) -> Token: def make_token(tag: str, location: SourceLocation, type_name_pattern:re.Pattern) -> Token:
"""Construct a token object with the provided tag and source location""" """Construct a token object with the provided tag and source location"""