Skip to content

Commit

Permalink
now round-tripping always works perfectly modulo layout, and even the…
Browse files Browse the repository at this point in the history
…n closely resembles the original style of textual MegaL
  • Loading branch information
grammarware committed Oct 3, 2012
1 parent 652ba8a commit 7f090c3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 100 deletions.
85 changes: 46 additions & 39 deletions topics/mega/src/backend/MegaExport.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,89 @@ str exportmega(megamodel(str name, str desc, list[str] incs, list[MegaDeclaratio
'
'<for(str i <- incs){>include <i> .
'<}>
'<for(MegaDeclaration d <- decls){><exportdeclR(d,rels)> .
'<for(MegaDeclaration d <- decls){><exportdeclR(d,rels)>
'<}>
'<for(MegaRelation r <- rels, r notin processed){><exportrelR(r,rels)> .
'<for(MegaRelation r <- rels, r notin processed){><exportrelR(r,rels)>
'<}>";
str exportdecl(artifact(MegaMod m, str id, bool plus)) = "<exportmod(m)>Artifact<if(plus){>+<}> <id>";
str exportdecl(file(MegaMod m, str id, bool plus)) = "<exportmod(m)>File<if(plus){>+<}> <id>";
str exportdecl(language(MegaMod m, str id, bool plus)) = "<exportmod(m)>Language<if(plus){>+<}> <id>";
str exportdecl(technology(MegaMod m, str id, bool plus)) = "<exportmod(m)>Technology<if(plus){>+<}> <id>";
str exportdecl(fragment(MegaMod m, str id, bool plus)) = "<exportmod(m)>Fragment<if(plus){>+<}> <id>";
str exportdecl(objectGraph(MegaMod m, str id, bool plus)) = "<exportmod(m)>ObjectGraph<if(plus){>+<}> <id>";
str exportdecl(program(MegaMod m, str id, bool plus)) = "<exportmod(m)>Program<if(plus){>+<}> <id>";
str exportdecl(library(MegaMod m, str id, bool plus)) = "<exportmod(m)>Library<if(plus){>+<}> <id>";
str exportdecl(function(MegaMod m, str id, bool plus)) = "<exportmod(m)>Function<if(plus){>+<}> <id>";
str exportdecl(artifact(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Artifact<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(file(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>File<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(language(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Language<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(technology(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Technology<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(fragment(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Fragment<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(objectGraph(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>ObjectGraph<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(program(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Program<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(library(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Library<if(plus){>+<}> <id><displayDot(comment)>";
str exportdecl(function(MegaMod m, str id, bool plus, str comment)) = "<exportmod(m)>Function<if(plus){>+<}> <id><displayDot(comment)>";
default str exportdecl(MegaDeclaration d) = "UNKNOWN DECL";
str displayDot("") = " .";
default str displayDot(str c) = " . -- <c>";
str exportmod(local()) = "local ";
str exportmod(variable()) = "variable ";
str exportmod(MegaMod m) = "";
str exportrel(subsetOf(str x, str y)) = "<x> subsetOf <y>";
str exportrel(elementOf(str x, str y)) = "<x> elementOf <y>";
str exportrel(partOf(str x, str y)) = "<x> partOf <y>";
str exportrel(correspondsTo(str x, str y)) = "<x> correspondsTo <y>";
str exportrel(dependsOn(str x, str y)) = "<x> dependsOn <y>";
str exportrel(refersTo(str x, str y)) = "<x> refersTo <y>";
str exportrel(conformsTo(str x, str y)) = "<x> conformsTo <y>";
str exportrel(realizationOf(str x, str y)) = "<x> realizationOf <y>";
str exportrel(descriptionOf(str x, str y)) = "<x> descriptionOf <y>";
str exportrel(definitionOf(str x, str y)) = "<x> definitionOf <y>";
str exportrel(inputOf(str x, str y)) = "<x> inputOf <y>";
str exportrel(hasOutput(str x, str y)) = "<x> hasOutput <y>";
str exportrel(domainOf(str x, str y)) = "<x> domainOf <y>";
str exportrel(hasRange(str x, str y)) = "<x> hasRange <y>";
str exportrel(subsetOf(str x, str y, str comment)) = "<x> subsetOf <y><displayDot(comment)>";
str exportrel(elementOf(str x, str y, str comment)) = "<x> elementOf <y><displayDot(comment)>";
str exportrel(partOf(str x, str y, str comment)) = "<x> partOf <y><displayDot(comment)>";
str exportrel(correspondsTo(str x, str y, str comment)) = "<x> correspondsTo <y><displayDot(comment)>";
str exportrel(dependsOn(str x, str y, str comment)) = "<x> dependsOn <y><displayDot(comment)>";
str exportrel(refersTo(str x, str y, str comment)) = "<x> refersTo <y><displayDot(comment)>";
str exportrel(conformsTo(str x, str y, str comment)) = "<x> conformsTo <y><displayDot(comment)>";
str exportrel(realizationOf(str x, str y, str comment)) = "<x> realizationOf <y><displayDot(comment)>";
str exportrel(descriptionOf(str x, str y, str comment)) = "<x> descriptionOf <y><displayDot(comment)>";
str exportrel(definitionOf(str x, str y, str comment)) = "<x> definitionOf <y><displayDot(comment)>";
str exportrel(inputOf(str x, str y, str comment)) = "<x> inputOf <y><displayDot(comment)>";
str exportrel(hasOutput(str x, str y, str comment)) = "<x> hasOutput <y><displayDot(comment)>";
str exportrel(domainOf(str x, str y, str comment)) = "<x> domainOf <y><displayDot(comment)>";
str exportrel(hasRange(str x, str y, str comment)) = "<x> hasRange <y><displayDot(comment)>";
default str exportrel(MegaRelation r) = "UNKNOWN REL";
str exportdeclR(MegaDeclaration d, list[MegaRelation] rels)
{
str s = "";
if (d.comment != "") return exportdecl(d); // if there is a comment, we need to stand on our own
for (r <- rels, r.x == d.id)
if (domainOf(a,b) := r, /hasRange(b,str c) := rels)
if (domainOf(a,b,_) := r, /hasRange(b,str c,dot) := rels)
{
s = " : <a> -\> <c>";
s = " : <a> -\> <c><displayDot(dot)>";
processed += r;
processed += hasRange(b,c);
processed += hasRange(b,c,dot);
break;
}
elseif (hasRange(a,b) := r, /domainOf(c,a) := rels)
elseif (hasRange(a,b,_) := r, /domainOf(c,a,dot) := rels)
{
s = " : <c> -\> <b>";
s = " : <c> -\> <b><displayDot(dot)>";
processed += r;
processed += domainOf(c,a);
processed += domainOf(c,a,dot);
break;
}
else
{
s = replaceFirst(exportrel(r),"<r.x> "," ");
s = exportrel(r);
processed += r;
break;
}
return exportdecl(d)+s;
if (s=="")
return exportdecl(d);
else
return replaceFirst(exportdecl(d)," .","")+replaceFirst(s,"<d.id>","");
}
str exportrelR(MegaRelation r, list[MegaRelation] rels)
{
if (inputOf(str a, str b) := r, /hasOutput(b,str c) := rels)
if (inputOf(str a, str b,_) := r, /hasOutput(b,str c, str d) := rels)
{
processed += r;
processed += hasOutput(b,c);
return "<b>(<a>) |-\> <c>";
processed += hasOutput(b,c,d);
return "<b>(<a>) |-\> <c><displayDot(d)>";
}
elseif (hasOutput(a,b) := r, /inputOf(c,a) := rels)
elseif (hasOutput(a,b,_) := r, /inputOf(c,a,d) := rels)
{
processed += r;
processed += inputOf(c,a);
return "<a>(<c>) |-\> <b>";
processed += inputOf(c,a,d);
return "<a>(<c>) |-\> <b><displayDot(d)>";
}
else
return exportrel(r);
Expand Down
74 changes: 38 additions & 36 deletions topics/mega/src/backend/MegaImplode.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ list[str] collectIncludes(MegaInclude* ins) = ["<i.name>" | MegaInclude i <- ins
list[MegaDeclaration] collectDecls(MegaDecl+ ds) = [*mapdecl(d) | MegaDecl d <- ds];
list[MegaRelation] collectRels(MegaDecl+ ds) = [*maprel(d) | MegaDecl d <- ds];
list[MegaDeclaration] mapdecl((MegaDecl)`<MegaModifier? mm><MegaEntity e><MegaDot d>`) = [map1decl(e,mapmod(mm),"<e.id>")];
list[MegaDeclaration] mapdecl((MegaDecl)`<MegaModifier? mm><MegaEntity e><MegaDot d>`) = [map1decl(e,mapmod(mm),"<e.id>",getComment(d))];
//list[MegaDeclaration] mapdecl((MegaDecl)`<MegaModifier? mm><MegaEntity e><MegaDot d>`) = [map2decl(mapmod(mm),"<e.id>")];
default list[MegaDeclaration] mapdecl(MegaDecl d) = [];
list[MegaRelation] maprel((MegaDecl)`<MegaModifier? _><MegaArtifact _><MaybePlus plus><ID x><MegaBin b><ID y><MegaDot d>`) = [map1rel("<b>","<x>","<y>")];
list[MegaRelation] maprel((MegaDecl)`<MegaModifier? _>Function<MaybePlus plus><ID x>:<ID y>-><ID z><MegaDot d>`) = [domainOf("<y>","<x>"),hasRange("<x>","<z>")];
list[MegaRelation] maprel((MegaDecl)`<ID x><MegaBin b><ID y><MegaDot d>`) = [map1rel("<b>","<x>","<y>")];
list[MegaRelation] maprel((MegaDecl)`<ID x>:<ID y>-><ID z><MegaDot d>`) = [domainOf("<y>","<x>"),hasRange("<x>","<z>")];
list[MegaRelation] maprel((MegaDecl)`<ID x>(<ID y>)|-><ID z><MegaDot d>`) = [inputOf("<y>","<x>"),hasOutput("<x>","<z>")];
list[MegaRelation] maprel((MegaDecl)`<MegaModifier? _><MegaArtifact _><MaybePlus plus><ID x><MegaBin b><ID y><MegaDot d>`) = [map1rel("<b>","<x>","<y>",getComment(d))];
list[MegaRelation] maprel((MegaDecl)`<MegaModifier? _>Function<MaybePlus plus><ID x>:<ID y>-><ID z><MegaDot d>`) = [domainOf("<y>","<x>",getComment(d)),hasRange("<x>","<z>",getComment(d))];
list[MegaRelation] maprel((MegaDecl)`<ID x><MegaBin b><ID y><MegaDot d>`) = [map1rel("<b>","<x>","<y>",getComment(d))];
list[MegaRelation] maprel((MegaDecl)`<ID x>:<ID y>-><ID z><MegaDot d>`) = [domainOf("<y>","<x>",getComment(d)),hasRange("<x>","<z>",getComment(d))];
list[MegaRelation] maprel((MegaDecl)`<ID x>(<ID y>)|-><ID z><MegaDot d>`) = [inputOf("<y>","<x>",getComment(d)),hasOutput("<x>","<z>",getComment(d))];
default list[MegaRelation] maprel(MegaDecl d) = [];
str getComment(MegaDot d) = replaceFirst("<d.c>","-- ","");
MegaMod mapmod(MegaModifier? m)
{
switch("<m>")
Expand All @@ -44,42 +46,42 @@ bool isplus((MaybePlus)`+`) = true;
bool isplus((MaybePlus)``) = false;
default bool isplus(MaybePlus _) = false; // insurance
MegaRelation map1rel("\<", str x, str y) = subsetOf(x,y);
MegaRelation map1rel("subsetOf", str x, str y) = subsetOf(x,y);
MegaRelation map1rel(":", str x, str y) = elementOf(x,y);
MegaRelation map1rel("elementOf", str x, str y) = elementOf(x,y);
MegaRelation map1rel("@", str x, str y) = partOf(x,y);
MegaRelation map1rel("partOf", str x, str y) = partOf(x,y);
MegaRelation map1rel("=", str x, str y) = correspondsTo(x,y);
MegaRelation map1rel("correspondsTo", str x, str y) = correspondsTo(x,y);
MegaRelation map1rel("~\>", str x, str y) = refersTo(x,y);
MegaRelation map1rel("dependsOn", str x, str y) = dependsOn(x,y);
MegaRelation map1rel("refersTo", str x, str y) = refersTo(x,y);
MegaRelation map1rel("-|", str x, str y) = conformsTo(x,y);
MegaRelation map1rel("conformsTo", str x, str y) = conformsTo(x,y);
MegaRelation map1rel("=\>", str x, str y) = definitionOf(x,y);
MegaRelation map1rel("realizationOf", str x, str y) = realizationOf(x,y);
MegaRelation map1rel("descriptionOf", str x, str y) = descriptionOf(x,y);
MegaRelation map1rel("definitionOf", str x, str y) = definitionOf(x,y);
default MegaRelation map1rel(str b, str x, str y) = subsetOf(x,y); // no error report
MegaRelation map1rel("\<", str x, str y, str comment) = subsetOf(x,y,comment);
MegaRelation map1rel("subsetOf", str x, str y, str comment) = subsetOf(x,y,comment);
MegaRelation map1rel(":", str x, str y, str comment) = elementOf(x,y,comment);
MegaRelation map1rel("elementOf", str x, str y, str comment) = elementOf(x,y,comment);
MegaRelation map1rel("@", str x, str y, str comment) = partOf(x,y,comment);
MegaRelation map1rel("partOf", str x, str y, str comment) = partOf(x,y,comment);
MegaRelation map1rel("=", str x, str y, str comment) = correspondsTo(x,y,comment);
MegaRelation map1rel("correspondsTo", str x, str y, str comment) = correspondsTo(x,y,comment);
MegaRelation map1rel("~\>", str x, str y, str comment) = refersTo(x,y,comment);
MegaRelation map1rel("dependsOn", str x, str y, str comment) = dependsOn(x,y,comment);
MegaRelation map1rel("refersTo", str x, str y, str comment) = refersTo(x,y,comment);
MegaRelation map1rel("-|", str x, str y, str comment) = conformsTo(x,y,comment);
MegaRelation map1rel("conformsTo", str x, str y, str comment) = conformsTo(x,y,comment);
MegaRelation map1rel("=\>", str x, str y, str comment) = definitionOf(x,y,comment);
MegaRelation map1rel("realizationOf", str x, str y, str comment) = realizationOf(x,y,comment);
MegaRelation map1rel("descriptionOf", str x, str y, str comment) = descriptionOf(x,y,comment);
MegaRelation map1rel("definitionOf", str x, str y, str comment) = definitionOf(x,y,comment);
default MegaRelation map1rel(str b, str x, str y, str comment) = subsetOf(x,y,comment); // no error report
MegaDeclaration map1decl((MegaEntity)`<MegaArtifact a><MaybePlus plus><ID x><MegaBin _><ID _>`,MegaMod m, str id)
= map1decl((MegaEntity)`<a><plus><x>`, m, id);
MegaDeclaration map1decl((MegaEntity)`<MegaArtifact a><MaybePlus plus><ID x><MegaBin _><ID _>`,MegaMod m, str id, str comment)
= map1decl((MegaEntity)`<a><plus><x>`, m, id, comment);
MegaDeclaration map1decl((MegaEntity)`<MegaArtifact a><MaybePlus plus><ID _>`,MegaMod m, str id)
MegaDeclaration map1decl((MegaEntity)`<MegaArtifact a><MaybePlus plus><ID _>`,MegaMod m, str id, str comment)
{
switch("<a>")
{
case "Artifact": return artifact(m,id,isplus(plus));
case "File": return file(m,id,isplus(plus));
case "Language": return language(m,id,isplus(plus));
case "Technology": return technology(m,id,isplus(plus));
case "Fragment": return fragment(m,id,isplus(plus));
case "ObjectGraph": return objectGraph(m,id,isplus(plus));
case "Program": return program(m,id,isplus(plus));
case "Library": return library(m,id,isplus(plus));
case "Artifact": return artifact(m,id,isplus(plus),comment);
case "File": return file(m,id,isplus(plus),comment);
case "Language": return language(m,id,isplus(plus),comment);
case "Technology": return technology(m,id,isplus(plus),comment);
case "Fragment": return fragment(m,id,isplus(plus),comment);
case "ObjectGraph": return objectGraph(m,id,isplus(plus),comment);
case "Program": return program(m,id,isplus(plus),comment);
case "Library": return library(m,id,isplus(plus),comment);
default: println("ERROR in <a>.");
}
}
MegaDeclaration map1decl((MegaEntity)`Function<MaybePlus plus><ID _><MegaFun? _>`,MegaMod m, str id) = function(m,id,isplus(plus)); //"
MegaDeclaration map1decl((MegaEntity)`Function<MaybePlus plus><ID _><MegaFun? _>`,MegaMod m, str id, str comment) = function(m,id,isplus(plus),comment); //"
default MegaDeclaration map1decl(MegaEntity e,MegaMod m, str id) = println("ERROR in <e>");
47 changes: 23 additions & 24 deletions topics/mega/src/structure/MegaADT.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ module structure::MegaADT

data AMegaModel = megamodel(str name, str desc, list[str] incs, list[MegaDeclaration] decls, list[MegaRelation] rels);
data MegaDeclaration
= artifact(MegaMod m, str id, bool plus)
| file(MegaMod m, str id, bool plus)
| language(MegaMod m, str id, bool plus)
| technology(MegaMod m, str id, bool plus)
| fragment(MegaMod m, str id, bool plus)
| objectGraph(MegaMod m, str id, bool plus)
| program(MegaMod m, str id, bool plus)
| library(MegaMod m, str id, bool plus)
| function(MegaMod m, str id, bool plus)
= artifact(MegaMod m, str id, bool plus, str comment)
| file(MegaMod m, str id, bool plus, str comment)
| language(MegaMod m, str id, bool plus, str comment)
| technology(MegaMod m, str id, bool plus, str comment)
| fragment(MegaMod m, str id, bool plus, str comment)
| objectGraph(MegaMod m, str id, bool plus, str comment)
| program(MegaMod m, str id, bool plus, str comment)
| library(MegaMod m, str id, bool plus, str comment)
| function(MegaMod m, str id, bool plus, str comment)
;
data MegaMod = local() | variable() | nomod();
data MegaRelation
= subsetOf(str x, str y)
| elementOf(str x, str y)
| partOf(str x, str y)
| correspondsTo(str x, str y)
| dependsOn(str x, str y)
| refersTo(str x, str y)
| conformsTo(str x, str y)
| realizationOf(str x, str y)
| descriptionOf(str x, str y)
| definitionOf(str x, str y)
| mapsTo(str f, str x, str y)
| inputOf(str x, str y)
| hasOutput(str x, str y)
| domainOf(str x, str y)
| hasRange(str x, str y)
= subsetOf(str x, str y, str comment)
| elementOf(str x, str y, str comment)
| partOf(str x, str y, str comment)
| correspondsTo(str x, str y, str comment)
| dependsOn(str x, str y, str comment)
| refersTo(str x, str y, str comment)
| conformsTo(str x, str y, str comment)
| realizationOf(str x, str y, str comment)
| descriptionOf(str x, str y, str comment)
| definitionOf(str x, str y, str comment)
| inputOf(str x, str y, str comment)
| hasOutput(str x, str y, str comment)
| domainOf(str x, str y, str comment)
| hasRange(str x, str y, str comment)
;
2 changes: 1 addition & 1 deletion topics/mega/src/structure/MegaGrammar.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ lexical MegaDesc = @category="Comment" "{-" MegaDescEl* s "-}";
lexical MegaDescEl = ![\-] | [\-] !>> [}];
layout L = LAYOUT* !>> [\ \t\n\r]; // !>> "--";
lexical LAYOUT = [\ \t\n\r];
syntax MegaDot = "." MegaComment? ;
syntax MegaDot = "." MegaComment? c ;
lexical MegaComment = @category="Comment" "--" ![\n]* $ ;
syntax MaybePlus = "+"?;

0 comments on commit 7f090c3

Please sign in to comment.