|
Revision 11450, 0.9 kB
(checked in by radix, 5 years ago)
|
MIT LICENSE: new LICENSE file and new preambles for all .py files. This is not all.
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
def parse(s): |
|---|
| 7 |
s = s.strip() |
|---|
| 8 |
expr = [] |
|---|
| 9 |
while s: |
|---|
| 10 |
if s[0] == '(': |
|---|
| 11 |
newSexp = [] |
|---|
| 12 |
if expr: |
|---|
| 13 |
expr[-1].append(newSexp) |
|---|
| 14 |
expr.append(newSexp) |
|---|
| 15 |
s = s[1:] |
|---|
| 16 |
continue |
|---|
| 17 |
if s[0] == ')': |
|---|
| 18 |
aList = expr.pop() |
|---|
| 19 |
s=s[1:] |
|---|
| 20 |
if not expr: |
|---|
| 21 |
assert not s |
|---|
| 22 |
return aList |
|---|
| 23 |
continue |
|---|
| 24 |
i = 0 |
|---|
| 25 |
while s[i].isdigit(): i+=1 |
|---|
| 26 |
assert i |
|---|
| 27 |
length = int(s[:i]) |
|---|
| 28 |
data = s[i+1:i+1+length] |
|---|
| 29 |
expr[-1].append(data) |
|---|
| 30 |
s=s[i+1+length:] |
|---|
| 31 |
assert 0, "this should not happen" |
|---|
| 32 |
|
|---|
| 33 |
def pack(sexp): |
|---|
| 34 |
s = "" |
|---|
| 35 |
for o in sexp: |
|---|
| 36 |
if type(o) in (type(()), type([])): |
|---|
| 37 |
s+='(' |
|---|
| 38 |
s+=pack(o) |
|---|
| 39 |
s+=')' |
|---|
| 40 |
else: |
|---|
| 41 |
s+='%i:%s' % (len(o), o) |
|---|
| 42 |
return s |
|---|