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

Function read_from_tokens

18-with-match/lispy/py3.10/lis.py:35–49  ·  view source on GitHub ↗

Read an expression from a sequence of tokens.

(tokens: list[str])

Source from the content-addressed store, hash-verified

33 return s.replace('(', ' ( ').replace(')', ' ) ').split()
34
35def read_from_tokens(tokens: list[str]) -> Expression:
36 "Read an expression from a sequence of tokens."
37 if len(tokens) == 0:
38 raise SyntaxError('unexpected EOF while reading')
39 token = tokens.pop(0)
40 if '(' == token:
41 exp = []
42 while tokens[0] != ')':
43 exp.append(read_from_tokens(tokens))
44 tokens.pop(0) # discard ')'
45 return exp
46 elif ')' == token:
47 raise SyntaxError('unexpected )')
48 else:
49 return parse_atom(token)
50
51def parse_atom(token: str) -> Atom:
52 "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