-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_tapl_parser.py
More file actions
35 lines (30 loc) · 891 Bytes
/
simple_tapl_parser.py
File metadata and controls
35 lines (30 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
Created on 8 Feb 2012
@author: Hens
'''
import cStringIO, tokenize
def atom(next, token):
if token[1] == "(":
out = []
token = next()
while token[1] != ")":
out.append(atom(next, token))
token = next()
if token[1] == ",":
token = next()
return tuple(out)
elif token[0] is tokenize.STRING:
return token[1][1:-1].decode("string-escape")
elif token[0] is tokenize.NUMBER:
try:
return int(token[1], 0)
except ValueError:
return float(token[1])
raise SyntaxError("Malformed expression (%s)" % token[1])
def simple_eval(source):
src=cStringIO.StringIO(source).readline
src = tokenize.generate_tokens(src)
return atom(src.next, src.next())
if __name__=="__main__":
print simple_eval("'hello'")
print simple_eval("1, 34")