Skip to content

Commit

Permalink
all of XBGF is redone in Rascal: fairly straightforward mapping from …
Browse files Browse the repository at this point in the history
…Prolog
  • Loading branch information
grammarware committed Jun 4, 2012
1 parent 9a46d95 commit adb3f81
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 213 deletions.
27 changes: 26 additions & 1 deletion shared/rascal/src/diff/GDT.rsc
Expand Up @@ -7,6 +7,31 @@ import normal::BGF;
import List;
import IO;

tuple[bool,list[BGFExpression],list[BGFExpression]] tryMatchChoices(list[BGFExpression] L1, list[BGFExpression] L2) = tryMatchChoices([],L1,[],L2,false);
tuple[bool,list[BGFExpression],list[BGFExpression]] tryMatchChoices(list[BGFExpression] es1, [], list[BGFExpression] es2, list[BGFExpression] L2, false) = <false,[],[]>;
tuple[bool,list[BGFExpression],list[BGFExpression]] tryMatchChoices(list[BGFExpression] es1, [], list[BGFExpression] es2, list[BGFExpression] L2, true) = <true,es1,es2+L2>;
tuple[bool,list[BGFExpression],list[BGFExpression]] tryMatchChoices(list[BGFExpression] es1, list[BGFExpression] L1, list[BGFExpression] es2, [], bool hit) = <false,[],[]>;
tuple[bool,list[BGFExpression],list[BGFExpression]] tryMatchChoices(list[BGFExpression] es1, list[BGFExpression] L1, list[BGFExpression] es2, list[BGFExpression] L2, bool hit)
{
println("tryMatchChoices(<es1>,<L1>,<es2>,<L2>,<hit>");
for (y <- L2)
{
for (x <- L1)
if (eqE(x,y))
{
println("-\> tryMatchChoices(<es1>,<L1-x>,<es2>,<L2-y>,true");
return tryMatchChoices(es1,L1-x,es2,L2-y,true);
}
if (hit)
es2 += y;
else
es1 += y;
L2 -= y;
}
return <false,[],[]>;
}


// expression equality
public bool eqE(choice([BGFExpression e1]), choice([BGFExpression e2])) = eqE(e1,e2);
public bool eqE(choice(L1), choice(L2))
Expand All @@ -30,7 +55,7 @@ public bool eqE(BGFExpression e1, BGFExpression e2) = e1 == e2; // default


public bool eqP(production(str l,str x, BGFExpression e1), production(l,x, BGFExpression e2)) = eqE(e1,e2);
public bool eqP(BGFExpression p1, BGFExpression p2) = p1 == p2;
public bool eqP(BGFProduction p1, BGFProduction p2) = p1 == p2;

// generic differ, returns unmatched production rules
tuple[list[BGFProduction],list[BGFProduction]] gdt(list[BGFProduction] ps1, list[BGFProduction] ps2)
Expand Down
12 changes: 6 additions & 6 deletions shared/rascal/src/io/ReadXBGF.rsc
Expand Up @@ -29,8 +29,8 @@ XBGFCommand mapxbgf(Node el)
case element(_,"anonymize",[prod]): return anonymize(mapprod(prod));
case element(_,"appear",[prod]): return appear(mapprod(prod));
case element(_,"chain",[prod]): return chain(mapprod(prod));
// clone(str x, str y, XBGFContext w)
// concatT(list[str] xs, str y, XBGFContext w)
// clone(str x, str y, XBGFScope w)
// concatT(list[str] xs, str y, XBGFScope w)
case element(_,"concretize",[prod]): return concretize(mapprod(prod));
case element(_,"deanonymize",[prod]): return deanonymize(mapprod(prod));
case element(_,"define",ps): return define([mapprod(p) | p <- ps]);
Expand Down Expand Up @@ -77,11 +77,11 @@ XBGFCommand mapxbgf(Node el)
// also, the current structure is too hard to match in a one-liner in Rascal
//case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),ps*,element(none(),"label",[charData(str s)])]): return splitN(s,[mapprod(p) | p <- ps],mapcontext(element(none(),"label",[charData(s)])));
//case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),ps*]): return splitN(s,[mapprod(p) | p <- ps],globally());
// splitN(str x, list[BGFProduction] ps, XBGFContext w)
case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),p]): return splitN(s,[mapprod(p)],globally());
// splitN(str x, list[BGFProduction] ps, XBGFScope w)
case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),p]): return splitN(s,[mapprod(p)],nowhere());
case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),p,w]): return splitN(s,[mapprod(p)],mapcontext(w));
// TODO: not implemented anywhere
// splitT(str x, list[str] ys, XBGFContext w)
// splitT(str x, list[str] ys, XBGFScope w)
case element(_,"unchain",[prod]): return unchain(mapprod(prod));
case element(_,"undefine",xs): return undefine([s | element(none(),"nonterminal",[charData(s)]) <- xs]);
case element(_,"unfold",[element(none(),"nonterminal",[charData(str s)])]): return unfold(s,globally());
Expand All @@ -108,7 +108,7 @@ XBGFCommand mapxbgf(Node el)
throw "ERROR with:\n<el>";
}

XBGFContext mapcontext(Node n)
XBGFScope mapcontext(Node n)
{
switch(n)
{
Expand Down
12 changes: 6 additions & 6 deletions shared/rascal/src/io/WriteXBGF.rsc
Expand Up @@ -30,8 +30,8 @@ Node xbgf2xml(XBGFCommand step)
case anonymize(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"anonymize",[prod2xml(prod)]);
case appear(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"appear",[prod2xml(prod)]);
case chain(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"chain",[prod2xml(prod)]);
// clone(str x, str y, XBGFContext w)
// concatT(list[str] xs, str y, XBGFContext w)
// clone(str x, str y, XBGFScope w)
// concatT(list[str] xs, str y, XBGFScope w)
case concretize(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"concretize",[prod2xml(prod)]);
case deanonymize(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"deanonymize",[prod2xml(prod)]);
case define(list[BGFProduction] ps): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"define",[prod2xml(p) | p <- ps]);
Expand Down Expand Up @@ -79,11 +79,11 @@ Node xbgf2xml(XBGFCommand step)
// also, the current structure is too hard to match in a one-liner in Rascal
//case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),ps*,element(none(),"label",[charData(str s)])]): return splitN(s,[mapprod2xml(prod)(p) | p <- ps],mapcontext(element(none(),"label",[charData(s)])));
//case element(_,"split",[element(none(),"nonterminal",[charData(str s)]),ps*]): return splitN(s,[mapprod2xml(prod)(p) | p <- ps],globally());
// splitN(str x, list[BGFProduction] ps, XBGFContext w)
case splitN(s,[p],globally()): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"split",[element(none(),"nonterminal",[charData(s)]),prod2xml(p)]);
// splitN(str x, list[BGFProduction] ps, XBGFScope w)
case splitN(s,[p],nowhere()): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"split",[element(none(),"nonterminal",[charData(s)]),prod2xml(p)]);
case splitN(s,[p],w): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"split",[element(none(),"nonterminal",[charData(s)]),prod2xml(p),context2xml(w)]);
// TODO: not implemented anywhere
// splitT(str x, list[str] ys, XBGFContext w)
// splitT(str x, list[str] ys, XBGFScope w)
case unchain(prod): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"unchain",[prod2xml(prod)]);
case undefine(xs): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"undefine",[element(none(),"nonterminal",[charData(s)]) | s <- xs]);
case unfold(s,globally()): return element(namespace("xbgf","http://planet-sl.org/xbgf"),"unfold",[element(none(),"nonterminal",[charData(s)])]);
Expand All @@ -104,7 +104,7 @@ Node xbgf2xml(XBGFCommand step)
}
}

Node context2xml(XBGFContext w)
Node context2xml(XBGFScope w)
{
switch(w)
{
Expand Down
40 changes: 22 additions & 18 deletions shared/rascal/src/syntax/XBGF.rsc
Expand Up @@ -13,31 +13,31 @@ data XBGFCommand =
| anonymize(BGFProduction p)
| appear(BGFProduction p) // marked
| chain(BGFProduction p)
| clone(str x, str y, XBGFContext w)
| concatT(list[str] xs, str y, XBGFContext w)
| clone(str x, str y, XBGFScope w)
| concatT(list[str] xs, str y, XBGFScope w)
| concretize(BGFProduction p) // marked
| deanonymize(BGFProduction p)
| define(list[BGFProduction] ps)
| designate(BGFProduction p)
| detour(BGFProduction p)
| deyaccify(str x)
| disappear(BGFProduction p) // marked
| distribute(XBGFContext w)
| distribute(XBGFScope w)
| downgrade(BGFProduction p1,BGFProduction p2) // p1 is marked
| eliminate(str x)
| equate(str x, str y)
| extract(BGFProduction p, XBGFContext w)
| factor(BGFExpression e1, BGFExpression e2, XBGFContext w)
| fold(str x, XBGFContext w)
| horizontal(XBGFContext w)
| extract(BGFProduction p, XBGFScope w)
| factor(BGFExpression e1, BGFExpression e2, XBGFScope w)
| fold(str x, XBGFScope w)
| horizontal(XBGFScope w)
| importG(list[BGFProduction] ps)
| inject(BGFProduction p) // marked
| inline(str x)
| introduce(list[BGFProduction] ps)
| iterate(BGFProduction p)
| lassoc(BGFProduction p)
| massage(BGFExpression e1, BGFExpression e2, XBGFContext w)
| narrow(BGFExpression e1, BGFExpression e2, XBGFContext w)
| massage(BGFExpression e1, BGFExpression e2, XBGFScope w)
| narrow(BGFExpression e1, BGFExpression e2, XBGFScope w)
| permute(BGFProduction p)
| project(BGFProduction p) // marked
| rassoc(BGFProduction p)
Expand All @@ -46,30 +46,34 @@ data XBGFCommand =
| removeV(BGFProduction p)
| renameL(str x, str y)
| renameN(str x, str y)
| renameS(str x, str y, XBGFContext w) // only inlabel(z)
| renameS(str x, str y, XBGFScope w) // only inlabel(z)
| renameT(str x, str y)
| replace(BGFExpression e1, BGFExpression e2, XBGFContext w)
| replace(BGFExpression e1, BGFExpression e2, XBGFScope w)
| reroot(list[str] xs)
//| splitN(list[BGFProduction] ps, list[BGFProduction] qs, XBGFContext w)
| splitN(str x, list[BGFProduction] ps, XBGFContext w)
| splitT(str x, list[str] ys, XBGFContext w)
//| splitN(list[BGFProduction] ps, list[BGFProduction] qs, XBGFScope w)
| splitN(str x, list[BGFProduction] ps, XBGFScope w)
| splitT(str x, list[str] ys, XBGFScope w)
| unchain(BGFProduction p)
| undefine(list[str] xs)
| unfold(str x, XBGFContext w)
| unfold(str x, XBGFScope w)
| unite(str x, str y)
| unlabel(str x) // ???
| upgrade(BGFProduction p1, BGFProduction p2) // p1 is marked
| vertical(XBGFContext w)
| widen(BGFExpression e1, BGFExpression e2, XBGFContext w)
| vertical(XBGFScope w)
| widen(BGFExpression e1, BGFExpression e2, XBGFScope w)
| yaccify(list[BGFProduction] ps)
// legacy
| atomic(list[XBGFCommand] steps)
| strip(str a)
;

data XBGFContext =
data XBGFScope =
globally()
| nowhere()
| inlabel(str l)
| notinlabel(str l)
| innt(str x)
| notinnt(str x)
| comboscope(XBGFScope w1, XBGFScope w2)
// TODO: combination
;

0 comments on commit adb3f81

Please sign in to comment.