31 """a decorator that helps printing out the transformation results"""
33 def wrapper(self, args):
34 print(f'[DBG] {decorated.__name__}: \n{args} \n\n')
35 res = decorated(self, args)
36 print(f'[DBG] returns: {res}\n')
57def is_tree_types(t, names):
58 """Check whether t is lark Tree and whether the tree type is name"""
59 if not isinstance(names, list):
60 raise ValueError('name argument should be list')
61 return isinstance(t, Tree) and t.data in names
64def get_ids_in_tree(tree):
66 __id_types = ['hvarref']
67 if not isinstance(tree, Tree):
68 raise ValueError('Only Tree type is accepted')
70 for t in tree.iter_subtrees():
71 if is_tree_types(t, __id_types):
72 assert t.children[0], 'hvarref should only contain one children'
73 res.append(t.children[0])
76def get_ids_in_tree_types(tree, types=['hvarref']):
79 if not isinstance(tree, Tree):
80 raise ValueError('Only Tree type is accepted')
82 for t in tree.iter_subtrees():
83 if is_tree_types(t, __id_types):
84 assert t.children[0], 'hvarref should only contain one children'
85 res.append(t.children[0])
89def get_tree_types(tree, types=['hvarref']):
92 if not isinstance(tree, Tree):
93 raise ValueError('Only Tree type is accepted')
95 for t in tree.iter_subtrees():
96 if is_tree_types(t, __id_types):
100def get_ids_in_tree_dfs(tree):
102 # __id_types = ['hvarref']
103 # if not isinstance(tree, Tree):
104 # raise ValueError('Only Tree type is accepted')
106 # for t in tree.iter_subtrees():
107 # if is_tree_types(t, __id_types):
108 # assert t.children[0], 'hvarref should only contain one children'
109 # res.append(t.children[0])
114 dfs_stack.append(tree)
116 while len(dfs_stack) != 0:
118 for idx in range(len(t.children)):
119 nxt = t.children[idx]
120 if isinstance(nxt, Tree):
121 dfs_stack.append(nxt)
122 elif is_tree_types(t, __id_types):
123 assert t.children[0], 'hvarref should only contain one children'
124 res.append(t.children[0])
128def set_ids_in_tree_dfs(tree, ids):
129 __id_types = ['hvarref']
131 dfs_stack.append(tree)
133 while len(dfs_stack) != 0:
135 for idx in range(len(t.children)):
136 nxt = t.children[idx]
137 if isinstance(nxt, Tree):
138 dfs_stack.append(nxt)
139 elif is_tree_types(t, __id_types):
140 t.children[idx] = ids[i](t.children[idx])
144def alternate_ids(tree, ops):
145 """Change the ids within a tree, given operations ops as an array of lambdas"""
146 ids = get_ids_in_tree(tree)
147 if len(ops) != len(ids):
148 raise ValueError('ops should have the same length as ids')
149 for idx, _ in enumerate(ids):
152def map_hvarref_ids(tree, ops):
153 """get all and apply mapping function"""
154 __id_types = ['hvarref']
155 if not isinstance(tree, Tree):
156 raise ValueError('Only Tree type is accepted')
159 for t in tree.iter_subtrees():
160 if is_tree_types(t, __id_types):
161 assert len(t.children) == 1, 'hvarref should only contain one children'
162 mapped_token = ops[idx](t.children[0])
163 assert mapped_token, 'mapping function should return a token'
164 t.children[0] = mapped_token
166 # res.append(t.children[0])