Skip to content

Commit

Permalink
ΞBGF redone in Rascal (also as a mapping to XBGF)
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jun 16, 2012
1 parent c5e2a32 commit 79f8877
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 2 deletions.
1 change: 1 addition & 0 deletions shared/rascal/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin
.settings
10 changes: 9 additions & 1 deletion shared/rascal/src/extract/RascalADT2BGF.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import syntax::BGF;
import normal::BGF;
import io::WriteBGF;

layout LO = [\ \t\n\r]* !>> [\ \t\n\r];
layout LAYOUTLIST = LAYOUT* !>> [\ \t\n\r/];
lexical LAYOUT
= [\ \t\n\r]
| @category="Comment" "//" ![\n]* $
//| @category="Comment" Nested: "%" ![%\n] "%"
;
// alias CBGFSequence = list[CBGFCommand];
//syntax DataDef = "
syntax DataDef = Name "=" {DataExpr "|"}+ ";";
lexical Name = [a-zA-Z_0-9]+ !>> [a-zA-Z_0-9] ;
syntax DataExpr
Expand Down Expand Up @@ -37,6 +44,7 @@ BGFGrammar process(loc src)
{
str name = trim(split("=",d)[0]);
println("Parsing <name>...");
//println(d);
ps += def2prod(parse(#DataDef,trim(d)));
}
return normalise(syntax::BGF::grammar([],ps));
Expand Down
69 changes: 69 additions & 0 deletions shared/rascal/src/syntax/CBGF.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module syntax::CBGF

import \syntax::BGF;

alias CBGFSequence = list[CBGFCommand];

data CBGFCommand =
abridge_detour(BGFProduction p)
| abstractize_concretize(BGFProduction p) // marked
| addH_removeH(BGFProduction p) // marked
| addV_removeV(BGFProduction p)
| anonymize_deanonymize(BGFProduction p)
| appear_disapper(BGFProduction p) // marked
| chain_unchain(BGFProduction p)
| clone_equate(str x, str y, XBGFScope w)
| concatT_splitT(str y, list[str] xs, XBGFScope w)
| concretize_abstractize(BGFProduction p) // marked
| deanonymize_anonymize(BGFProduction p)
| define_undefine(list[BGFProduction] ps)
| designate_unlabel(BGFProduction p)
| detour_abridge(BGFProduction p)
| deyaccify_yaccify(list[BGFProduction] ps) //!!!
| disappear_appear(BGFProduction p) // marked
//| distribute_distribute(XBGFScope w)
| downgrade_upgrade(BGFProduction p1,BGFProduction p2) // p1 is marked
| eliminate_introduce(list[BGFProduction] ps) //!!!
| equate_clone(str x, str y, XBGFScope w) //!!!
| extract_inline(BGFProduction p, XBGFScope w)
| factor_factor(BGFExpression e1, BGFExpression e2, XBGFScope w)
| fold_unfold(str x, XBGFScope w)
| horizontal_vertical(XBGFScope w)
//| importG(list[BGFProduction] ps)
| inject_project(BGFProduction p) // marked
| inline_extract(BGFProduction p, XBGFScope w) //!!!
| introduce_eliminate(list[BGFProduction] ps)
| iterate_assoc(BGFProduction p)
| assoc_iterate(BGFProduction p)
//| lassoc(BGFProduction p)
| massage_massage(BGFExpression e1, BGFExpression e2, XBGFScope w)
| narrow_widen(BGFExpression e1, BGFExpression e2, XBGFScope w)
| permute_permute(BGFProduction p1, BGFProduction p2) //!!!
| project_inject(BGFProduction p) // marked
//| rassoc(BGFProduction p)
| redefine_redefine(list[BGFProduction] ps1, list[BGFProduction] ps2)
| removeH_addH(BGFProduction p) // marked
| removeV_addV(BGFProduction p)
| renameL_renameL(str x, str y)
| renameN_renameN(str x, str y)
| renameS_renameS(str x, str y, XBGFScope w) // only inlabel(z)
| renameT_renameT(str x, str y)
| replace_replace(BGFExpression e1, BGFExpression e2, XBGFScope w)
| reroot_reroot(list[str] xs1, list[str] xs2)
//| splitN(list[BGFProduction] ps, list[BGFProduction] qs, XBGFScope w)
| splitN_unite(str x, list[BGFProduction] ps, XBGFScope w)
| splitT_concatT(str x, list[str] ys, XBGFScope w)
| unchain_chain(BGFProduction p)
| undefine_define(list[BGFProduction] ps)
| unfold_fold(str x, XBGFScope w)
| unite_splitN(str x, list[BGFProduction] ps, XBGFScope w) //!!!
| unlabel_designate(BGFProduction p) // !!!
| upgrade_downgrade(BGFProduction p1, BGFProduction p2) // p1 is marked
| vertical_horizontal(XBGFScope w)
| widen_narrow(BGFExpression e1, BGFExpression e2, XBGFScope w)
| yaccify_deyaccify(list[BGFProduction] ps)
// legacy
//| atomic(list[XBGFCommand] steps)
//| strip(str a)
;
1 change: 0 additions & 1 deletion shared/rascal/src/syntax/XBGF.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ data XBGFScope =
| innt(str x)
| notinnt(str x)
| comboscope(XBGFScope w1, XBGFScope w2)
// TODO: combination
;
149 changes: 149 additions & 0 deletions shared/rascal/src/transform/CBGF.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module transform::CBGF // should be ΞBGF

import syntax::BGF;
import syntax::XBGF;
import syntax::CBGF;
import transform::library::Util;

// forward execution
public XBGFSequence forward(CBGFSequence cbgf) = [forward(step) | step <- cbgf];

XBGFCommand forward(abridge_detour(BGFProduction p)) = abridge(p);
XBGFCommand forward(abstractize_concretize(BGFProduction p)) = abstractize(p);
XBGFCommand forward(addH_removeH(BGFProduction p)) = addH(p);
XBGFCommand forward(addV_removeV(BGFProduction p)) = addV(p);
XBGFCommand forward(anonymize_deanonymize(BGFProduction p)) = anonymize(p);
XBGFCommand forward(appear_disapper(BGFProduction p)) = appear(p);
XBGFCommand forward(chain_unchain(BGFProduction p)) = chain(p);
XBGFCommand forward(clone_equate(str x, str y, XBGFScope w)) = clone(x,y,w);
XBGFCommand forward(concatT_splitT(str y, list[str] xs, XBGFScope w)) = concatT(xs,y,w);
XBGFCommand forward(concretize_abstractize(BGFProduction p)) = concretize(p);
XBGFCommand forward(deanonymize_anonymize(BGFProduction p)) = deanonymize(p);
XBGFCommand forward(define_undefine(list[BGFProduction] ps)) = define(p);
XBGFCommand forward(designate_unlabel(BGFProduction p)) = designate(p);
XBGFCommand forward(detour_abridge(BGFProduction p)) = detour(p);
XBGFCommand forward(deyaccify_yaccify(list[BGFProduction] ps))
{
if ({str x} := transform::library::Util::definedNs(ps)) return deyaccify(x);
else throw "<ps> must concern one nonterminal";
}
XBGFCommand forward(disappear_appear(BGFProduction p)) = disappear(p);
XBGFCommand forward(downgrade_upgrade(BGFProduction p1,BGFProduction p2)) = downgrade(p1,p2);
XBGFCommand forward(eliminate_introduce(list[BGFProduction] ps))
{
if ({str x} := transform::library::Util::definedNs(ps)) return eliminate(x);
else throw "<ps> must concern one nonterminal";
}
XBGFCommand forward(equate_clone(str x, str y, XBGFScope w)) = equate(x,y);
XBGFCommand forward(extract_inline(BGFProduction p, XBGFScope w)) = extract(p,w);
XBGFCommand forward(factor_factor(BGFExpression e1, BGFExpression e2, XBGFScope w)) = factor(e1,e2,w);
XBGFCommand forward(fold_unfold(str x, XBGFScope w)) = fold(x,w);
XBGFCommand forward(horizontal_vertical(XBGFScope w)) = horizontal(w);
XBGFCommand forward(inject_project(BGFProduction p)) = inject(p);
XBGFCommand forward(inline_extract(BGFProduction p, XBGFScope w)) = inline(p.lhs);
XBGFCommand forward(introduce_eliminate(list[BGFProduction] ps)) = introduce(ps);
XBGFCommand forward(iterate_assoc(BGFProduction p)) = iterate(p);
XBGFCommand forward(assoc_iterate(BGFProduction p)) = rassoc(p); // TODO
XBGFCommand forward(massage_massage(BGFExpression e1, BGFExpression e2, XBGFScope w)) = massage(e1,e2,w);
XBGFCommand forward(narrow_widen(BGFExpression e1, BGFExpression e2, XBGFScope w)) = narrow(e1,e2,w);
XBGFCommand forward(permute_permute(BGFProduction p1,BGFProduction p2)) = permute(p2);
XBGFCommand forward(project_inject(BGFProduction p)) = project(p);
XBGFCommand forward(redefine_redefine(_,list[BGFProduction] ps)) = redefine(ps);
XBGFCommand forward(removeH_addH(BGFProduction p)) = removeH(p);
XBGFCommand forward(removeV_addV(BGFProduction p)) = removeV(p);
XBGFCommand forward(renameL_renameL(str x, str y)) = renameL(x,y);
XBGFCommand forward(renameN_renameN(str x, str y)) = renameN(x,y);
XBGFCommand forward(renameS_renameS(str x, str y, XBGFScope w)) = renameS(x,y,w);
XBGFCommand forward(renameT_renameT(str x, str y)) = renameT(x,y);
XBGFCommand forward(replace_replace(BGFExpression e1, BGFExpression e2, XBGFScope w)) = replace(e1,e2,w);
XBGFCommand forward(reroot_reroot(_,list[str] xs)) = reroot(xs);
XBGFCommand forward(splitN_unite(str x, list[BGFProduction] ps, XBGFScope w)) = splitN(x,ps,w);
XBGFCommand forward(splitT_concatT(str x, list[str] ys, XBGFScope w)) = splitT(x,ys,w);
XBGFCommand forward(unchain_chain(BGFProduction p)) = unchain(p);
XBGFCommand forward(undefine_define(list[BGFProduction] ps)) = undefine(toList(transform::library::Util::definedNs(ps)));
XBGFCommand forward(unfold_fold(str x, XBGFScope w)) = unfold(x,w);
XBGFCommand forward(unite_splitN(str x, list[BGFProduction] ps, XBGFScope w))
{
// TODO: w
// TODO: check that x != y
if ({str y} := transform::library::Util::definedNs(ps)) return unite(x,y);
else throw "<ps> must concern one nonterminal";
}
XBGFCommand forward(unlabel_designate(production(str l,_,_))) = unlabel(l);
XBGFCommand forward(upgrade_downgrade(BGFProduction p1, BGFProduction p2)) = upgrade(p1,p2);
XBGFCommand forward(vertical_horizontal(XBGFScope w)) = vertical(w);
XBGFCommand forward(widen_narrow(BGFExpression e1, BGFExpression e2, XBGFScope w)) = widen(e1,e2,w);
XBGFCommand forward(yaccify_deyaccify(list[BGFProduction] ps)) = yaccify(ps);
default XBGFCommand forward(CBGFCommand step) {throw "Unknown CBGF command: <step>";}

// reverse execution
public XBGFSequence reverse(CBGFSequence cbgf) = [reverse(step) | step <- cbgf];

XBGFCommand reverse(abridge_detour(BGFProduction p)) = detour(p);
XBGFCommand reverse(abstractize_concretize(BGFProduction p)) = concretize(p);
XBGFCommand reverse(addH_removeH(BGFProduction p)) = removeH(p);
XBGFCommand reverse(addV_removeV(BGFProduction p)) = removeV(p);
XBGFCommand reverse(anonymize_deanonymize(BGFProduction p)) = deanonymize(p);
XBGFCommand reverse(appear_disapper(BGFProduction p)) = disappear(p);
XBGFCommand reverse(chain_unchain(BGFProduction p)) = unchain(p);
XBGFCommand reverse(clone_equate(str x, str y, XBGFScope w)) = equate(x,y);
XBGFCommand reverse(concatT_splitT(str y, list[str] xs, XBGFScope w)) = splitT(y,xs,w);
XBGFCommand reverse(concretize_abstractize(BGFProduction p)) = abstractize(p);
XBGFCommand reverse(deanonymize_anonymize(BGFProduction p)) = anonymize(p);
XBGFCommand reverse(define_undefine(list[BGFProduction] ps)) = undefine(toList(transform::library::Util::definedNs(ps)));
XBGFCommand reverse(designate_unlabel(production(str l,_,_))) = unlabel(l);
XBGFCommand reverse(detour_abridge(BGFProduction p)) = abridge(p);
XBGFCommand reverse(deyaccify_yaccify(list[BGFProduction] ps)) = yaccify(ps);
XBGFCommand reverse(disappear_appear(BGFProduction p)) = appear(p);
XBGFCommand reverse(downgrade_upgrade(BGFProduction p1,BGFProduction p2)) = upgrade(p1,p2);
XBGFCommand reverse(eliminate_introduce(list[BGFProduction] ps)) = introduce(ps);
XBGFCommand reverse(equate_clone(str x, str y, XBGFScope w)) = clone(x,y,w);
XBGFCommand reverse(extract_inline(BGFProduction p, XBGFScope w)) = inline(p.lhs);
XBGFCommand reverse(factor_factor(BGFExpression e1, BGFExpression e2, XBGFScope w)) = factor(e2,e1,w);
XBGFCommand reverse(fold_unfold(str x, XBGFScope w)) = unfold(x,w);
XBGFCommand reverse(horizontal_vertical(XBGFScope w)) = vertical(w);
XBGFCommand reverse(inject_project(BGFProduction p)) = project(p);
XBGFCommand reverse(inline_extract(BGFProduction p, XBGFScope w)) = extract(p,w);
XBGFCommand reverse(introduce_eliminate(list[BGFProduction] ps))
{
if ({str x} := transform::library::Util::definedNs(ps)) return eliminate(x);
else throw "<ps> must concern one nonterminal";
}
XBGFCommand reverse(iterate_assoc(BGFProduction p)) = rassoc(p); // TODO
XBGFCommand reverse(assoc_iterate(BGFProduction p)) = iterate(p);
XBGFCommand reverse(massage_massage(BGFExpression e1, BGFExpression e2, XBGFScope w)) = massage(e2,e1,w);
XBGFCommand reverse(narrow_widen(BGFExpression e1, BGFExpression e2, XBGFScope w)) = narrow(e2,e1,w);
XBGFCommand reverse(permute_permute(BGFProduction p,_)) = permute(p);
XBGFCommand reverse(project_inject(BGFProduction p)) = inject(p);
XBGFCommand reverse(redefine_redefine(list[BGFProduction] ps,_)) = redefine(ps);
XBGFCommand reverse(removeH_addH(BGFProduction p)) = addH(p);
XBGFCommand reverse(removeV_addV(BGFProduction p)) = addV(p);
XBGFCommand reverse(renameL_renameL(str x, str y)) = renameL(y,x);
XBGFCommand reverse(renameN_renameN(str x, str y)) = renameN(y,x);
XBGFCommand reverse(renameS_renameS(str x, str y, XBGFScope w)) = renameS(y,x,w);
XBGFCommand reverse(renameT_renameT(str x, str y)) = renameT(y,x);
XBGFCommand reverse(replace_replace(BGFExpression e1, BGFExpression e2, XBGFScope w)) = replace(e2,e1,w);
XBGFCommand reverse(reroot_reroot(list[str] xs,_)) = reroot(xs);
XBGFCommand reverse(splitN_unite(str x, list[BGFProduction] ps, XBGFScope w))
{
// TODO: w
// TODO: check that x != y
if ({str y} := transform::library::Util::definedNs(ps)) return unite(x,y);
else throw "<ps> must concern one nonterminal";
}
XBGFCommand reverse(splitT_concatT(str x, list[str] ys, XBGFScope w)) = splitT(x,ys,w);
XBGFCommand reverse(unchain_chain(BGFProduction p)) = chain(p);
XBGFCommand reverse(undefine_define(list[BGFProduction] ps)) = define(ps);
XBGFCommand reverse(unfold_fold(str x, XBGFScope w)) = fold(x,w);
XBGFCommand reverse(unite_splitN(str x, list[BGFProduction] ps, XBGFScope w)) = splitN(x,ps,w);
XBGFCommand reverse(unlabel_designate(BGFProduction p)) = designate(p);
XBGFCommand reverse(upgrade_downgrade(BGFProduction p1, BGFProduction p2)) = downgrade(p1,p2);
XBGFCommand reverse(vertical_horizontal(XBGFScope w)) = horizontal(w);
XBGFCommand reverse(widen_narrow(BGFExpression e1, BGFExpression e2, XBGFScope w)) = narrow(e2,e1,w);
XBGFCommand reverse(yaccify_deyaccify(list[BGFProduction] ps))
{
if ({str x} := transform::library::Util::definedNs(ps)) return deyaccify(x);
else throw "<ps> must concern one nonterminal";
}
default XBGFCommand reverse(CBGFCommand step) {throw "Unknown CBGF command: <step>";}

0 comments on commit 79f8877

Please sign in to comment.