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')
55def is_tree_types(t, names):
56 """Check whether t is lark Tree and whether the tree type is name"""
57 if not isinstance(names, list):
58 raise ValueError('name argument should be list')
59 return isinstance(t, Tree) and t.data in names
62def get_ids_in_tree(tree):
64 __id_types = ['hvarref']
65 if not isinstance(tree, Tree):
66 raise ValueError('Only Tree type is accepted')
68 for t in tree.iter_subtrees():
69 if is_tree_types(t, __id_types):
70 assert t.children[0], 'hvarref should only contain one children'
71 res.append(t.children[0])
75def get_ids_in_tree_dfs(tree):
77 # __id_types = ['hvarref']
78 # if not isinstance(tree, Tree):
79 # raise ValueError('Only Tree type is accepted')
81 # for t in tree.iter_subtrees():
82 # if is_tree_types(t, __id_types):
83 # assert t.children[0], 'hvarref should only contain one children'
84 # res.append(t.children[0])
89 dfs_stack.append(tree)
91 while len(dfs_stack) != 0:
93 for idx in range(len(t.children)):
95 if isinstance(nxt, Tree):
97 elif is_tree_types(t, __id_types):
98 assert t.children[0], 'hvarref should only contain one children'
99 res.append(t.children[0])
103def set_ids_in_tree_dfs(tree, ids):
104 __id_types = ['hvarref']
106 dfs_stack.append(tree)
108 while len(dfs_stack) != 0:
110 for idx in range(len(t.children)):
111 nxt = t.children[idx]
112 if isinstance(nxt, Tree):
113 dfs_stack.append(nxt)
114 elif is_tree_types(t, __id_types):
115 t.children[idx] = ids[i](t.children[idx])
119def alternate_ids(tree, ops):
120 """Change the ids within a tree, given operations ops as an array of lambdas"""
121 ids = get_ids_in_tree(tree)
122 if len(ops) != len(ids):
123 raise ValueError('ops should have the same length as ids')
124 for idx, _ in enumerate(ids):