]> git.saurik.com Git - cycript.git/blob - jammed.py
Avoid crashing when the user types a mismatched }.
[cycript.git] / jammed.py
1 #!/usr/bin/python
2
3 import re
4 import sys
5
6 tables = dict()
7 defines = dict()
8
9 with open(sys.argv[1], 'r') as file:
10 while True:
11 line = file.readline()
12 if line == '':
13 break
14
15 define = re.match('#define ([A-Za-z_]*) ([0-9]+)', line)
16 if define != None:
17 defines[define.group(1)] = int(define.group(2))
18 continue
19
20 if not line.startswith('static yyconst '):
21 continue
22
23 end = line.index('[')
24 begin = line.rindex(' ', 0, end)
25 name = line[begin+1:end]
26
27 code = ''
28 while True:
29 line = file.readline()
30 code += line
31 if line.find(';') != -1:
32 break
33
34 code = code.replace('{', '[')
35 code = code.replace('}', ']')
36 code = code.replace(';', ' ')
37 tables[name] = eval(code)
38
39 yy_nxt = tables['yy_nxt']
40 yy_accept = tables['yy_accept']
41 yy_ec = tables['yy_ec']
42
43 YY_NUM_RULES = defines['YY_NUM_RULES']
44 try:
45 jammed = yy_accept.index(YY_NUM_RULES)
46 except ValueError:
47 sys.exit(0)
48
49 equivs = dict()
50 for ordinal, equiv in enumerate(yy_ec):
51 equivs[equiv] = equivs.get(equiv, '') + chr(ordinal)
52
53 starts = set(range(1, len(yy_nxt)))
54 for source, table in enumerate(yy_nxt):
55 if source == 0:
56 continue
57 for equiv, target in enumerate(table):
58 if target in starts:
59 starts.remove(target)
60
61 after = dict()
62 after[jammed] = [[]]
63
64 while True:
65 finish = dict()
66 for start in starts:
67 suffix = after.get(start)
68 if suffix == None:
69 continue
70 finish[start] = [[equivs[c] for c in s] for s in suffix]
71 if len(finish) != 0:
72 print finish
73 break
74
75 before = after
76 after = dict()
77
78 for source, table in enumerate(yy_nxt):
79 if source == 0:
80 continue
81 string = []
82 for equiv, target in enumerate(table):
83 suffix = before.get(target)
84 if suffix == None:
85 continue
86 string.extend([[equiv] + s for s in suffix])
87 if len(string) == 0:
88 continue
89 after[source] = string
90
91 sys.exit(1)