-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTDiff.rsc
More file actions
196 lines (152 loc) · 5.98 KB
/
Copy pathASTDiff.rsc
File metadata and controls
196 lines (152 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module ASTDiff
import lang::xml::DOM;
import lang::json::ast::JSON;
import lang::json::IO;
import Node;
import Type;
import ParseTree;
import IO;
import Node;
import List;
import String;
@javaClass{internals.RascalGumTree}
java str compareAST(str src, str dst);
@javaClass{internals.RascalGumTree}
java str compareASTXml(str src, str dst);
// Rascal AST to GumTree XML
str toGumTree(&T <: node input_ast){
// println("Node: <input_ast>");
Node toGumTreeNode(&T <: node child){
loc temp_loc = typeCast(#loc, getKeywordParameters(child)["src"]);
Node result = element(none(), "tree", []);
result.children += [attribute(none(), "type", getName(child))]
+ [attribute(none(), "length", "<temp_loc.length>")]
+ [attribute(none(), "pos", "<temp_loc.offset>")]
+ [toGumTreeNode(x, length=temp_loc.length, offset=temp_loc.offset)| x <- getChildren(child) && [*_] !:= x]
+ ([]|it + toGumTreeNode(y, length=temp_loc.length, offset=temp_loc.offset)| list[&T] x <- getChildren(child), [*&T _] := x, y <- x)
+ ([]|it + toGumTreeNode(y, length=temp_loc.length, offset=temp_loc.offset)| list[str] x <- getChildren(child), [*str _] := x, y <- x)
;
return result;
}
list[Node] toGumTreeNodeList(list[node] child_list){
return [toGumTreeNode(x)| node x <- child_list];
}
Node toGumTreeNode(str child, int length=0, int offset=0){
Node result = element(none(), "tree", []);
result.children += [attribute(none(), "type", "$token")]
+ [attribute(none(), "length", "<length>")]
+ [attribute(none(), "pos", "<offset>")]
+ [attribute(none(), "label", child)]
;
return result;
}
Node result = element(none(), "tree", []);
loc temp_loc = typeCast(#loc, getKeywordParameters(input_ast)["src"]);
result.children += [attribute(none(), "type", getName(input_ast))]
+ [attribute(none(), "length", "<temp_loc.length>")]
+ [attribute(none(), "pos", "<temp_loc.offset>")]
+ [toGumTreeNode(x)| x <- getChildren(input_ast) && [*_] !:= x]
+ toGumTreeNodeList(getChildren(input_ast)[0])
;
Node t = document(result);
Node collapseToken(Node t){
return top-down visit(t){
case element(_, _,[x, y, z, element(_, _, [attribute(_, _, "$token"), _, _, w])])
=> element(none(), "tree", [x, y, z, w])
};
}
return xmlPretty(collapseToken(t));
}
JSON deserializeActions(str json) = fromJSON(#JSON, json);
// Diff
alias DiffTree = tuple[loc src, loc dest, list[DiffNode] diffNodeList];
data DiffNode
= insertNode(node tree)
| keepNode(node src, node dest)
| removeNode(node tree)
| updateNode(node src, node dest)
| moveNode(node tree)
| matchedNode(node src, node dest)
| emptyNode()
;
list[DiffNode] makeDiffNodeKeep(list[DiffNode] diff_tree_list){
return [keepNode(src, dest) | matchedNode(src, dest) <- diff_tree_list && src := dest];
}
DiffNode makeDiffNodeMatch(JSON json_obj){
switch (json_obj){
case object(x): {
makeDiffNode(x["src"]);
return matchedNode(makeDiffNode(x["src"]), makeDiffNode(x["dest"]));
}
default: return emptyNode();
}
}
DiffNode makeUpdateNode(map[str, JSON] x){
node temp_ = makeDiffNode(x["tree"]);
list[str] children = string(child_) := x["label"]? [child_]:[];
return updateNode(temp_, makeNode(getName(temp_), children));
}
DiffNode makeDiffNode(JSON json_obj){
switch (json_obj){
case object(x): {
if(x["action"] == string("move-tree")){
return moveNode(makeDiffNode(x["tree"]));
} else if (x["action"] == string("delete-node")){
return removeNode(makeDiffNode(x["tree"]));
} else if (x["action"] == string("insert-node")){
return insertNode(makeDiffNode(x["tree"]));
} else{
return makeUpdateNode(x);
}
}
default: return emptyNode();
}
}
DiffTree _diff(
JSON diff_json
, loc src_loc=|unknown:///|
, loc dest_loc=|unknown:///|){
list[DiffNode] match_nodes = [makeDiffNodeMatch(x) | x <- diff_json.properties["matches"].values];
list[DiffNode] keep_nodes = makeDiffNodeKeep(match_nodes);
list[DiffNode] other_nodes = [makeDiffNode(action)| action <- diff_json.properties["actions"].values];
return <src_loc, dest_loc, (keep_nodes + other_nodes)>;
}
node makeDiffNode(string(str x)){
list[str] temp_ = split(" ", x);
list[str] tempLast_ = split(",", replaceFirst(replaceLast(temp_[-1], "]", ""), "[", ""));
list[int] location = [toInt(x) | str x <- tempLast_];
return makeNode(
replaceLast(
temp_[0], ":", "")
, temp_[1..-1]
, keywordParameters = ("location":location)
);
}
DiffTree diff(
type[&T <: Tree] grammar
, type[&U <: node] ast
, str src
, str dest){
node temp_ast_1 = implode(ast, parse(grammar, src));
node temp_ast_2 = implode(ast, parse(grammar, dest));
str result_1 = toGumTree(temp_ast_1);
str result_2 = toGumTree(temp_ast_2);
str compare_ast = compareAST(result_1, result_2);
JSON deserialize_actions = deserializeActions(compare_ast);
// iprintln(deserialize_actions);
return _diff(deserialize_actions, src_loc=|unknown:///|, dest_loc=|unknown:///|);
}
DiffTree diff(
type[&T <: Tree] grammar
, type[&U <: node] ast
, loc src_loc
, loc dest_loc){
node temp_ast_1 = implode(ast, parse(grammar, readFile(src_loc)));
node temp_ast_2 = implode(ast, parse(grammar, readFile(dest_loc)));
str result_1 = toGumTree(temp_ast_1);
str result_2 = toGumTree(temp_ast_2);
str compare_ast = compareAST(result_1, result_2);
JSON deserialize_actions = deserializeActions(compare_ast);
// iprintln("Matches&Actios:\n<deserialize_actions>");
return _diff(deserialize_actions, src_loc=src_loc, dest_loc=dest_loc);
}