Skip to content

Commit

Permalink
Use ast module instead of compiler module for parsing files.
Browse files Browse the repository at this point in the history
This CL is part of the work getting GYP to run under Python 3.
The compiler module we used to parse GYP files was removed in
Python 3, so this CL switches to use the ast module instead,
which exists in both Python 2 and Python 3.

This change is not sufficient to run under Python 3; there's
more work remaining. Things should still work under Python 2
just fine.

This work is derived from [email protected]'s
original work in https://codereview.chromium.org/1454433002/.

Bug: gyp:36

Change-Id: I9a9835560491c3d8cd5426623484dc4a46af1d86
Reviewed-on: https://chromium-review.googlesource.com/c/1360352
Reviewed-by: Mark Mentovai <[email protected]>
  • Loading branch information
dpranke committed Dec 4, 2018
1 parent 9df93ee commit f989ef9
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions pylib/gyp/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from compiler.ast import Const
from compiler.ast import Dict
from compiler.ast import Discard
from compiler.ast import List
from compiler.ast import Module
from compiler.ast import Node
from compiler.ast import Stmt
import compiler
import ast

import gyp.common
import gyp.simple_copy
import multiprocessing
Expand Down Expand Up @@ -183,43 +177,38 @@ def CheckedEval(file_contents):
Note that this is slower than eval() is.
"""

ast = compiler.parse(file_contents)
assert isinstance(ast, Module)
c1 = ast.getChildren()
assert c1[0] is None
assert isinstance(c1[1], Stmt)
c2 = c1[1].getChildren()
assert isinstance(c2[0], Discard)
c3 = c2[0].getChildren()
assert len(c3) == 1
return CheckNode(c3[0], [])
syntax_tree = ast.parse(file_contents)
assert isinstance(syntax_tree, ast.Module)
c1 = syntax_tree.body
assert len(c1) == 1
c2 = c1[0]
assert isinstance(c2, ast.Expr)
return CheckNode(c2.value, [])


def CheckNode(node, keypath):
if isinstance(node, Dict):
c = node.getChildren()
if isinstance(node, ast.Dict):
dict = {}
for n in range(0, len(c), 2):
assert isinstance(c[n], Const)
key = c[n].getChildren()[0]
for key, value in zip(node.keys, node.values):
assert isinstance(key, ast.Str)
key = key.s
if key in dict:
raise GypError("Key '" + key + "' repeated at level " +
repr(len(keypath) + 1) + " with key path '" +
'.'.join(keypath) + "'")
kp = list(keypath) # Make a copy of the list for descending this node.
kp.append(key)
dict[key] = CheckNode(c[n + 1], kp)
dict[key] = CheckNode(value, kp)
return dict
elif isinstance(node, List):
c = node.getChildren()
elif isinstance(node, ast.List):
children = []
for index, child in enumerate(c):
for index, child in enumerate(node.elts):
kp = list(keypath) # Copy list.
kp.append(repr(index))
children.append(CheckNode(child, kp))
return children
elif isinstance(node, Const):
return node.getChildren()[0]
elif isinstance(node, ast.Str):
return node.s
else:
raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) +
"': " + repr(node))
Expand Down

0 comments on commit f989ef9

Please sign in to comment.