MCPcopy Create free account
hub / github.com/FlyingFeather/DEA-SQL / ASTNode

Class ASTNode

evaluation/ts_old/spider-master/baselines/nl2code/astnode.py:8–261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6from lang.util import typename
7
8class ASTNode(object):
9 def __init__(self, node_type, label=None, value=None, children=None):
10 self.type = node_type
11 self.label = label
12 self.value = value
13
14 if type(self) is not Rule:
15 self.parent = None
16
17 self.children = list()
18
19 if children:
20 if isinstance(children, Iterable):
21 for child in children:
22 self.add_child(child)
23 elif isinstance(children, ASTNode):
24 self.add_child(children)
25 else:
26 raise AttributeError('Wrong type for child nodes')
27
28 assert not (bool(children) and bool(value)), 'terminal node with a value cannot have children'
29
30 @property
31 def is_leaf(self):
32 return len(self.children) == 0
33
34 @property
35 def is_preterminal(self):
36 return len(self.children) == 1 and self.children[0].is_leaf
37
38 @property
39 def size(self):
40 if self.is_leaf:
41 return 1
42
43 node_num = 1
44 for child in self.children:
45 node_num += child.size
46
47 return node_num
48
49 @property
50 def nodes(self):
51 """a generator that returns all the nodes"""
52
53 yield self
54 for child in self.children:
55 for child_n in child.nodes:
56 yield child_n
57
58 @property
59 def as_type_node(self):
60 """return an ASTNode with type information only"""
61 return ASTNode(self.type)
62
63 def __repr__(self):
64 repr_str = ''
65 # if not self.is_leaf:

Callers 15

__getitem__Method · 0.90
sql_ast_to_parse_treeFunction · 0.90
add_rootFunction · 0.90
extract_unary_closureFunction · 0.90
compressed_ast_to_normalFunction · 0.90
unary_link_to_closureFunction · 0.90
python_ast_to_parse_treeFunction · 0.90
add_rootFunction · 0.90
break_value_nodesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected