"""Lexer for arithmetic expressions.""" from dataclasses import dataclass from typing import Union class LexError(Exception): pass @dataclass class Token: kind: str value: Union[int, float, None] def __repr__(self): return f"{self.kind}({self.value!r})" def tokenize(src: str) -> list: tokens = [] i = 0 while i < len(src): ch = src[i] if ch in ' \t': i += 1 continue if ch.isdigit() or (ch == '.' and i + 1 < len(src) and src[i + 1].isdigit()): j = i while j < len(src) and src[j].isdigit(): j += 1 if j < len(src) and src[j] == '.': j += 1 while j < len(src) and src[j].isdigit(): j += 1 tokens.append(Token('NUMBER', float(src[i:j]))) else: tokens.append(Token('NUMBER', int(src[i:j]))) i = j continue if ch == '+': tokens.append(Token('PLUS', None)) elif ch == '-': tokens.append(Token('MINUS', None)) elif ch == '*': tokens.append(Token('STAR', None)) elif ch == '/': tokens.append(Token('SLASH', None)) elif ch == '(': tokens.append(Token('LPAREN', None)) elif ch == ')': tokens.append(Token('RPAREN', None)) else: raise LexError(f"Invalid character {ch!r} at position {i}") i += 1 tokens.append(Token('EOF', None)) return tokens