1008 B
1008 B
JOURNAL — Phase lex (Builder)
Implementation
Built calc/lexer.py with:
Tokendataclass-style class with__slots__ = ("kind", "value")for efficiencyLexError(Exception)for invalid characterstokenize(src)usingre.compile(r"\d+\.?\d*|\.\d+")for number matching- Integer if no
.in raw string; float otherwise - Single-char dispatch table
_SINGLEfor operators/parens - Raises
LexErrorwith char + position for unknown characters - Appends
EOFtoken at end
- Integer if no
Test run
$ python -m unittest -q
Ran 21 tests in 0.000s
OK
Cold-verify outputs
$ python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
$ python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
Traceback (most recent call last):
...
calc.lexer.LexError: unexpected character '@' at position 2