MCPcopy
hub / github.com/fluentpython/example-code-2e / read_from_tokens

Function read_from_tokens

18-with-match/lispy/py3.9/lis.py:33–47  ·  view source on GitHub ↗

Read an expression from a sequence of tokens.

(tokens: list[str])

Source from the content-addressed store, hash-verified

31 return s.replace('(', ' ( ').replace(')', ' ) ').split()
32
33def read_from_tokens(tokens: list[str]) -> Expression:
34 "Read an expression from a sequence of tokens."
35 if len(tokens) == 0:
36 raise SyntaxError('unexpected EOF while reading')
37 token = tokens.pop(0)
38 if '(' == token:
39 exp = []
40 while tokens[0] != ')':
41 exp.append(read_from_tokens(tokens))
42 tokens.pop(0) # discard ')'
43 return exp
44 elif ')' == token:
45 raise SyntaxError('unexpected )')
46 else:
47 return parse_atom(token)
48
49def parse_atom(token: str) -> Atom:
50 "Numbers become numbers; every other token is a symbol."

Callers 2

parseFunction · 0.70
runFunction · 0.70

Calls 3

popMethod · 0.80
parse_atomFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected