|
3 | 3 | # Usage: abitype.py < old_code > new_code |
4 | 4 | import re, sys |
5 | 5 |
|
6 | | -############ Simplistic C scanner ################################## |
7 | | -tokenizer = re.compile( |
8 | | - r"(?P<preproc>#.*\n)" |
9 | | - r"|(?P<comment>/\*.*?\*/)" |
10 | | - r"|(?P<ident>[a-zA-Z_][a-zA-Z0-9_]*)" |
11 | | - r"|(?P<ws>[ \t\n]+)" |
12 | | - r"|(?P<other>.)", |
13 | | - re.MULTILINE) |
14 | | - |
15 | | -tokens = [] |
16 | | -source = sys.stdin.read() |
17 | | -pos = 0 |
18 | | -while pos != len(source): |
19 | | - m = tokenizer.match(source, pos) |
20 | | - tokens.append([m.lastgroup, m.group()]) |
21 | | - pos += len(tokens[-1][1]) |
22 | | - if tokens[-1][0] == 'preproc': |
23 | | - # continuation lines are considered |
24 | | - # only in preprocess statements |
25 | | - while tokens[-1][1].endswith('\\\n'): |
26 | | - nl = source.find('\n', pos) |
27 | | - if nl == -1: |
28 | | - line = source[pos:] |
29 | | - else: |
30 | | - line = source[pos:nl+1] |
31 | | - tokens[-1][1] += line |
32 | | - pos += len(line) |
33 | | - |
34 | 6 | ###### Replacement of PyTypeObject static instances ############## |
35 | 7 |
|
36 | 8 | # classify each token, giving it a one-letter code: |
@@ -79,7 +51,7 @@ def get_fields(start, real_end): |
79 | 51 | while tokens[pos][0] in ('ws', 'comment'): |
80 | 52 | pos += 1 |
81 | 53 | if tokens[pos][1] != 'PyVarObject_HEAD_INIT': |
82 | | - raise Exception, '%s has no PyVarObject_HEAD_INIT' % name |
| 54 | + raise Exception('%s has no PyVarObject_HEAD_INIT' % name) |
83 | 55 | while tokens[pos][1] != ')': |
84 | 56 | pos += 1 |
85 | 57 | pos += 1 |
@@ -183,18 +155,48 @@ def make_slots(name, fields): |
183 | 155 | return '\n'.join(res) |
184 | 156 |
|
185 | 157 |
|
186 | | -# Main loop: replace all static PyTypeObjects until |
187 | | -# there are none left. |
188 | | -while 1: |
189 | | - c = classify() |
190 | | - m = re.search('(SW)?TWIW?=W?{.*?};', c) |
191 | | - if not m: |
192 | | - break |
193 | | - start = m.start() |
194 | | - end = m.end() |
195 | | - name, fields = get_fields(start, m) |
196 | | - tokens[start:end] = [('',make_slots(name, fields))] |
| 158 | +if __name__ == '__main__': |
| 159 | + |
| 160 | + ############ Simplistic C scanner ################################## |
| 161 | + tokenizer = re.compile( |
| 162 | + r"(?P<preproc>#.*\n)" |
| 163 | + r"|(?P<comment>/\*.*?\*/)" |
| 164 | + r"|(?P<ident>[a-zA-Z_][a-zA-Z0-9_]*)" |
| 165 | + r"|(?P<ws>[ \t\n]+)" |
| 166 | + r"|(?P<other>.)", |
| 167 | + re.MULTILINE) |
| 168 | + |
| 169 | + tokens = [] |
| 170 | + source = sys.stdin.read() |
| 171 | + pos = 0 |
| 172 | + while pos != len(source): |
| 173 | + m = tokenizer.match(source, pos) |
| 174 | + tokens.append([m.lastgroup, m.group()]) |
| 175 | + pos += len(tokens[-1][1]) |
| 176 | + if tokens[-1][0] == 'preproc': |
| 177 | + # continuation lines are considered |
| 178 | + # only in preprocess statements |
| 179 | + while tokens[-1][1].endswith('\\\n'): |
| 180 | + nl = source.find('\n', pos) |
| 181 | + if nl == -1: |
| 182 | + line = source[pos:] |
| 183 | + else: |
| 184 | + line = source[pos:nl+1] |
| 185 | + tokens[-1][1] += line |
| 186 | + pos += len(line) |
| 187 | + |
| 188 | + # Main loop: replace all static PyTypeObjects until |
| 189 | + # there are none left. |
| 190 | + while 1: |
| 191 | + c = classify() |
| 192 | + m = re.search('(SW)?TWIW?=W?{.*?};', c) |
| 193 | + if not m: |
| 194 | + break |
| 195 | + start = m.start() |
| 196 | + end = m.end() |
| 197 | + name, fields = get_fields(start, m) |
| 198 | + tokens[start:end] = [('',make_slots(name, fields))] |
197 | 199 |
|
198 | | -# Output result to stdout |
199 | | -for t, v in tokens: |
200 | | - sys.stdout.write(v) |
| 200 | + # Output result to stdout |
| 201 | + for t, v in tokens: |
| 202 | + sys.stdout.write(v) |
0 commit comments