-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ParentNodeTransformer is now ParentChildNodeTransformer and adds link…
…s to node children
- Loading branch information
Showing
8 changed files
with
54 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
import ast | ||
|
||
|
||
class ParentNodeTransformer(object): | ||
class ParentChildNodeTransformer(object): | ||
|
||
def visit(self, node): | ||
self._prepare_node(node) | ||
for field, value in ast.iter_fields(node): | ||
self._process_field(node, field, value) | ||
return node | ||
|
||
@staticmethod | ||
def _prepare_node(node): | ||
if not hasattr(node, 'parent'): | ||
node.parent = None | ||
node.parents = [] | ||
for field, value in ast.iter_fields(node): | ||
if isinstance(value, list): | ||
for index, item in enumerate(value): | ||
if isinstance(item, ast.AST): | ||
self.visit(item) | ||
self._set_parnt_fields(item, node, field, index) | ||
elif isinstance(value, ast.AST): | ||
self.visit(value) | ||
self._set_parnt_fields(value, node, field) | ||
return node | ||
if not hasattr(node, 'children'): | ||
node.children = [] | ||
|
||
def _set_parnt_fields(self, node, parent, field, index=None): | ||
node.parent = parent | ||
node.parents.append(parent) | ||
node.parent_field = field | ||
node.parent_field_index = index | ||
def _process_field(self, node, field, value): | ||
if isinstance(value, list): | ||
for index, item in enumerate(value): | ||
if isinstance(item, ast.AST): | ||
self._process_child(item, node, field, index) | ||
elif isinstance(value, ast.AST): | ||
self._process_child(value, node, field) | ||
|
||
def _process_child(self, child, parent, field_name, index=None): | ||
self.visit(child) | ||
child.parent = parent | ||
child.parents.append(parent) | ||
child.parent_field = field_name | ||
child.parent_field_index = index | ||
child.parent.children.append(child) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters