|
4 | 4 |
|
5 | 5 | from __future__ import print_function
|
6 | 6 |
|
7 |
| -from compiler.ast import Const |
8 |
| -from compiler.ast import Dict |
9 |
| -from compiler.ast import Discard |
10 |
| -from compiler.ast import List |
11 |
| -from compiler.ast import Module |
12 |
| -from compiler.ast import Node |
13 |
| -from compiler.ast import Stmt |
14 |
| -import compiler |
| 7 | +import ast |
| 8 | + |
15 | 9 | import gyp.common
|
16 | 10 | import gyp.simple_copy
|
17 | 11 | import multiprocessing
|
@@ -184,43 +178,39 @@ def CheckedEval(file_contents):
|
184 | 178 | Note that this is slower than eval() is.
|
185 | 179 | """
|
186 | 180 |
|
187 |
| - ast = compiler.parse(file_contents) |
188 |
| - assert isinstance(ast, Module) |
189 |
| - c1 = ast.getChildren() |
190 |
| - assert c1[0] is None |
191 |
| - assert isinstance(c1[1], Stmt) |
192 |
| - c2 = c1[1].getChildren() |
193 |
| - assert isinstance(c2[0], Discard) |
194 |
| - c3 = c2[0].getChildren() |
195 |
| - assert len(c3) == 1 |
196 |
| - return CheckNode(c3[0], []) |
| 181 | + syntax_tree = ast.parse(file_contents) |
| 182 | + assert isinstance(syntax_tree, ast.Module) |
| 183 | + c1 = syntax_tree.body |
| 184 | + assert len(c1) == 1 |
| 185 | + c2 = c1[0] |
| 186 | + assert isinstance(c2, ast.Expr) |
| 187 | + return CheckNode(c2.value, []) |
197 | 188 |
|
198 | 189 |
|
199 | 190 | def CheckNode(node, keypath):
|
200 |
| - if isinstance(node, Dict): |
| 191 | + if isinstance(node, ast.Dict): |
201 | 192 | c = node.getChildren()
|
202 | 193 | dict = {}
|
203 |
| - for n in range(0, len(c), 2): |
204 |
| - assert isinstance(c[n], Const) |
205 |
| - key = c[n].getChildren()[0] |
| 194 | + for key, value in zip(node.keys, node.values): |
| 195 | + assert isinstance(key, ast.Str) |
| 196 | + key = key.s |
206 | 197 | if key in dict:
|
207 | 198 | raise GypError("Key '" + key + "' repeated at level " +
|
208 | 199 | repr(len(keypath) + 1) + " with key path '" +
|
209 | 200 | '.'.join(keypath) + "'")
|
210 | 201 | kp = list(keypath) # Make a copy of the list for descending this node.
|
211 | 202 | kp.append(key)
|
212 |
| - dict[key] = CheckNode(c[n + 1], kp) |
| 203 | + dict[key] = CheckNode(value, kp) |
213 | 204 | return dict
|
214 |
| - elif isinstance(node, List): |
215 |
| - c = node.getChildren() |
| 205 | + elif isinstance(node, ast.List): |
216 | 206 | children = []
|
217 |
| - for index, child in enumerate(c): |
| 207 | + for index, child in enumerate(node.elts): |
218 | 208 | kp = list(keypath) # Copy list.
|
219 | 209 | kp.append(repr(index))
|
220 | 210 | children.append(CheckNode(child, kp))
|
221 | 211 | return children
|
222 |
| - elif isinstance(node, Const): |
223 |
| - return node.getChildren()[0] |
| 212 | + elif isinstance(node, ast.Str): |
| 213 | + return node.s |
224 | 214 | else:
|
225 | 215 | raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) +
|
226 | 216 | "': " + repr(node))
|
|
0 commit comments