def node_cmp(cls, tree, node_id): ''' Compares first node of cst tree with node_id. @param tree: CST @param node_id: integer representing a py_symbol or a py_token @return: - 0 if node_id is tree-root. - -1 if tree is a py_token or node_id cannot be the node_id of any subtree. - 1 otherwise ''' tree_id = tree[0] % LANGLET_ID_OFFSET node_id = node_id % LANGLET_ID_OFFSET if tree_id == node_id: # do we still want this? it makes nodes of two different langlets return 0 # comparable. elif is_token(tree_id): # is token return -1 if cls.activated: if is_symbol(node_id): try: s0 = hierarchy[tree_id] # global ??? s1 = hierarchy[node_id] if s0 > s1: return -1 except KeyError: return 1 else: return 1 else: return 1
def node_cmp(cls, tree, node_id): ''' Compares first node of cst tree with node_id. @param tree: CST @param node_id: integer representing a py_symbol or a py_token @return: - 0 if node_id is tree-root. - -1 if tree is a py_token or node_id cannot be the node_id of any subtree. - 1 otherwise ''' tree_id = tree[0] % LANGLET_ID_OFFSET node_id = node_id % LANGLET_ID_OFFSET if tree_id == node_id: # do we still want this? it makes nodes of two different langlets return 0 # comparable. elif is_token(tree_id): # is token return -1 if cls.activated: if is_symbol(node_id): try: s0 = hierarchy[tree_id] # global ??? s1 = hierarchy[node_id] if s0>s1: return -1 except KeyError: return 1 else: return 1 else: return 1
def find_node(tree, nid, depth=MAX_DEPTH, exclude=()): ''' Finds one node of a given node id. ( Non-recursive depth first search implementation. ) ''' if is_token(tree[0]): if nid == tree[0]: return tree else: return dq = deque() for sub in tree[1:]: dq.append((sub, depth - 1)) while dq: node, depth = dq.popleft() s = node[0] if s == nid: return node elif is_symbol(s) and s not in exclude and depth > 0: subnodes = zip(node[:0:-1], [depth - 1] * (len(node) - 1)) dq.extendleft(subnodes)
def find_node(tree, nid, depth = MAX_DEPTH, exclude = ()): ''' Finds one node of a given node id. ( Non-recursive depth first search implementation. ) ''' if is_token(tree[0]): if nid == tree[0]: return tree else: return dq = deque() for sub in tree[1:]: dq.append((sub, depth-1)) while dq: node, depth = dq.popleft() s = node[0] if s == nid: return node elif is_symbol(s) and s not in exclude and depth>0: subnodes = zip(node[:0:-1], [depth-1]*(len(node)-1)) dq.extendleft(subnodes)