Lines Matching refs:node
1 """Utility functions, node construction macros, etc."""
8 from .pytree import Leaf, Node
14 ### Common node-construction "macros"
18 return Node(syms.argument,
35 return Node(syms.atom,
43 """A node tuple for obj.attr"""
44 return [obj, Node(syms.trailer, [Dot(), attr])]
56 node = Node(syms.trailer, [lparen.clone(), rparen.clone()])
58 node.insert_child(1, Node(syms.arglist, args))
59 return node
63 node = Node(syms.power, [func_name, ArgList(args)])
65 node.prefix = prefix
66 return node
81 return Node(syms.trailer, [Leaf(token.LBRACE, u"["),
106 inner_args.append(Node(syms.comp_if, [if_leaf, test]))
107 inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
108 return Node(syms.atom,
128 Node(syms.import_as_names, name_leafs)]
129 imp = Node(syms.import_from, children)
134 ### Determine whether a node represents a given literal
137 def is_tuple(node):
138 """Does the node represent a tuple literal?"""
139 if isinstance(node, Node) and node.children == [LParen(), RParen()]:
141 return (isinstance(node, Node)
142 and len(node.children) == 3
143 and isinstance(node.children[0], Leaf)
144 and isinstance(node.children[1], Node)
145 and isinstance(node.children[2], Leaf)
146 and node.children[0].value == u"("
147 and node.children[2].value == u")")
149 def is_list(node):
150 """Does the node represent a list literal?"""
151 return (isinstance(node, Node)
152 and len(node.children) > 1
153 and isinstance(node.children[0], Leaf)
154 and isinstance(node.children[-1], Leaf)
155 and node.children[0].value == u"["
156 and node.children[-1].value == u"]")
163 def parenthesize(node):
164 return Node(syms.atom, [LParen(), node, RParen()])
189 p0 = """for_stmt< 'for' any 'in' node=any ':' any* >
190 | comp_for< 'for' any 'in' node=any any* >
196 trailer< '(' node=any ')' >
203 trailer< '(' arglist<node=any any*> ')' >
208 def in_special_context(node):
209 """ Returns true if node is in an environment where all that is required
221 for pattern, parent in zip(patterns, attr_chain(node, "parent")):
223 if pattern.match(parent, results) and results["node"] is node:
227 def is_probably_builtin(node):
231 prev = node.prev_sibling
235 parent = node.parent
238 if parent.type == syms.expr_stmt and parent.children[0] is node:
244 parent.children[0] is node
250 def find_indentation(node):
251 """Find the indentation of *node*."""
252 while node is not None:
253 if node.type == syms.suite and len(node.children) > 2:
254 indent = node.children[1]
257 node = node.parent
264 def make_suite(node):
265 if node.type == syms.suite:
266 return node
267 node = node.clone()
268 parent, node.parent = node.parent, None
269 suite = Node(syms.suite, [node])
273 def find_root(node):
276 while node.type != syms.file_input:
277 node = node.parent
278 if not node:
279 raise ValueError("root found before file_input node was found.")
280 return node
282 def does_tree_import(package, name, node):
284 top level of the tree which node belongs to.
287 binding = find_binding(name, find_root(node), package)
290 def is_import(node):
291 """Returns true if the node is an import statement."""
292 return node.type in (syms.import_name, syms.import_from)
294 def touch_import(package, name, node):
297 def is_import_stmt(node):
298 return (node.type == syms.simple_stmt and node.children and
299 is_import(node.children[0]))
301 root = find_root(node)
309 for idx, node in enumerate(root.children):
310 if not is_import_stmt(node):
321 for idx, node in enumerate(root.children):
322 if (node.type == syms.simple_stmt and node.children and
323 node.children[0].type == token.STRING):
328 import_ = Node(syms.import_name, [
336 root.insert_child(insert_pos, Node(syms.simple_stmt, children))
340 def find_binding(name, node, package=None):
341 """ Returns the node which binds variable name, otherwise None.
345 for child in node.children:
383 def _find(name, node):
384 nodes = [node]
386 node = nodes.pop()
387 if node.type > 256 and node.type not in _block_syms:
388 nodes.extend(node.children)
389 elif node.type == token.NAME and node.value == name:
390 return node
393 def _is_import_binding(node, name, package=None):
394 """ Will reuturn node if node will import name, or node
398 if node.type == syms.import_name and not package:
399 imp = node.children[1]
404 return node
406 return node
410 return node
412 return node
413 elif node.type == syms.import_from:
416 if package and unicode(node.children[1]).strip() != package:
418 n = node.children[3]
423 return node
427 return node
429 return node
431 return node