1.1 KiB
1.1 KiB
JOURNAL — phase lex
Build session
Design decisions
Tokenis adataclasswithkind: strandvalue: Union[int, float, None]. Operator tokens havevalue=None;NUMBERtokens carry their parsed numeric value (int for integers, float when.present).LexErroris a plainExceptionsubclass defined in the module.- Used
remodule with_NUMBER_RE = re.compile(r'\d+\.?\d*|\.\d+')to match integers, floats-with-integer-part, and leading-dot floats. _SINGLEdict maps single chars to token kinds.
Test run output
python -m unittest -q
----------------------------------------------------------------------
Ran 17 tests in 0.000s
OK
Verify command 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', None), ('LPAREN', None), ('NUMBER', 1), ('MINUS', None), ('NUMBER', 2), ('RPAREN', None), ('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