-
Notifications
You must be signed in to change notification settings - Fork 15
/
Labels.rsc
78 lines (71 loc) · 2.74 KB
/
Labels.rsc
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
@contributor{Vadim Zaytsev - [email protected] - SWAT, CWI}
@wiki{designate,renameL,unlabel,anonymize,deanonymize,renameS}
module transform::library::Labels
import lib::Rascalware;
import language::BGF;
import language::XScope;
import language::XOutcome;
import transform::library::Util;
// true labels
XBGFResult runDesignate(production(str l,str n,BGFExpression e), BGFGrammar g)
{
if (l == "")
return <problemProd("Production rule must me labelled, use unlabel instead",production(l,n,e)),g>;
if (production("",n,e) notin g.prods)
// throw "Production rule defining <n> as <e> not found.";
return <problemProd("Production rule not found, use renameL instead",production("",n,e)),g>;
return <ok(),grammar(g.roots,replaceP(g.prods,production("",n,e),production(l,n,e)))>;
}
XBGFResult runRenameL(str x, str y, BGFGrammar g)
{
if (x == "")
return <problem("Source label must not be empty for renaming, use designate"),g>;
if (y == "")
return <problem("Target label must not be empty for renaming, use unlabel"),g>;
if (len([p | p <- g.prods, production(x, _, _) := p]) != 1)
return <problemStr("Source name for renaming must be uniquely used",x),g>;
if (len([p | p <- g.prods, production(y, _, _) := p]) != 0)
return <problemStr("Target name for renaming must be fresh",y),g>;
<ps1,ps2,ps3> = splitPbyW(g.prods, inlabel(x));
if ([production(x, str n, BGFExpression e)] := ps2)
return <ok(),grammar(g.roots, ps1 + production(y, n, e) + ps3)>;
else
return <problemStr("Label not found or not unique",x),g>; // the latter should never happen
}
XBGFResult runUnlabel(str x, BGFGrammar g)
{
if (x == "")
return <problem("Please specify which label to unlabel"),g>;
<ps1,ps2,ps3> = splitPbyW(g.prods, inlabel(x));
if ([production(str l, str x, BGFExpression e)] := ps2)
return <ok(),grammar(g.roots, ps1 + production("", x, e) + ps3)>;
else
return <problemStr("Label not found or not unique",x),g>; // the latter should never happen
}
// selectors for subexpressions
XBGFResult runAnonymize(BGFProduction p1, BGFGrammar g)
{
p2 = unmark(p1);
p3 = demarkS(p1);
if (!inProds(p2,g.prods))
return <notFoundP(p1),g>;
return <ok(),grammar(g.roots, replaceP(g.prods,p2,p3))>;
}
XBGFResult runDeanonymize(BGFProduction p1, BGFGrammar g)
{
p2 = unmark(p1);
p3 = demarkS(p1);
if (!inProds(p3,g.prods))
return <notFoundP(p1),g>;
return <ok(),grammar(g.roots, replaceP(g.prods,p3,p2))>;
}
XBGFResult runRenameS(str x, str y, XBGFScope w, BGFGrammar g)
{
<ps1,ps2,ps3> = splitPbyW(g.prods, w);
if (/selectable(x,_) !:= ps2)
return <freshName("Source name",x),g>;
if (/selectable(y,_) := ps2)
return <notFreshName("Target name",y),g>;
ps4 = visit(ps2){case selectable(x,BGFExpression e) => selectable(y,e)}
return <ok(),grammar(g.roots, ps1 + ps4 + ps3)>;
}