Python Lexer Reading Numbers Function Position Error
11:47 13 Apr 2026

(Sorry for my grammar i'm not english)

Context :

I'm currently creating a Lexer for my Interpreted Scripting Language, i'm actually trying to add a function to read numbers to create a Integer Token

Principal Code :

    # Read numbers
    def read_numbers(self):
        all_numbers = ''
        print(self.pos)
        while self.pos <= len(self.text) and self.text[self.pos] in DIGITS :
            char = self.text[self.pos]
            all_numbers += char

            if self.pos + 1 < len(self.text):
                return all_numbers
            else:
                self.advance()
                continue

Here how i create Tokens :

elif char in DIGITS:
    tokens.append(Token(TokenType["NUMBER"], self.read_numbers(), self.line, self.collum))

And here my token class !

@dataclass
class Token:
    type: TokenType # pyright: ignore[reportInvalidTypeForm]
    value: str | None
    line: int
    collum: int

What i expect :

A function that is called when a numbers is detected, The function then read and advance until there no more numbers

What i tried :

I tried to use debug by using print to check the values and using other comparaison method like : self.pos + 1 > len(self.text)

But both try didn't worked

python position lexer