~ashn/monkey-python

5cd72b4f15d806722a1843986bba6278af1d4b0d — ashn 11 months ago 903df4d
Create a new SourceLocation object for each Token
1 files changed, 8 insertions(+), 3 deletions(-)

M monkey.py
M monkey.py => monkey.py +8 -3
@@ 106,9 106,8 @@ class Lexer:
        self, source: str, initial_location: Optional[SourceLocation] = None
    ) -> None:
        self.source: str = source
        # vvv What position does the source "start" being parsed from.
        #     None if the source is being lexed in a location-independent
        #     manner.
        # What position does the source "start" being parsed from.
        # None if the source is being lexed in a location-independent manner.
        self.location: Optional[SourceLocation] = initial_location
        self.position: int = 0
        self.read_position: int = 0


@@ 116,6 115,12 @@ class Lexer:
        self._read_char()

    def next_token(self) -> Token:
        # Create a new copy of the lexer's source location so that each token
        # has a unique own source location object (i.e. separate reference).
        if self.location is not None:
            self.location = SourceLocation(
                self.location.filename, self.location.line
            )
        self._skip_whitespace()

        if self.ch == "=":