Commit 0fbf8618 authored by Niklas Fors's avatar Niklas Fors
Browse files

Add code generation for abstract grammar and fix bug

parent b63622ab
...@@ -18,7 +18,17 @@ aspect TypeAnalysis { ...@@ -18,7 +18,17 @@ aspect TypeAnalysis {
} }
aspect ComponentAnalysis { aspect ComponentAnalysis {
syn boolean Component.isDeclaringName() = true;
eq RelationComponent.isDeclaringName() = !isTargetOfRightDirection();
inh boolean RelationComponent.isTargetOfRightDirection();
eq Relation.getRight().isTargetOfRightDirection()
= getDirection() instanceof RightDirection;
eq Program.getChild().isTargetOfRightDirection() = false;
syn String Component.name() { syn String Component.name() {
if (!isDeclaringName()) {
return "";
}
if (!getID().isEmpty()) { if (!getID().isEmpty()) {
return getID(); return getID();
} }
...@@ -32,14 +42,18 @@ aspect ComponentAnalysis { ...@@ -32,14 +42,18 @@ aspect ComponentAnalysis {
eq Program.getChild().otherRelationSideName() eq Program.getChild().otherRelationSideName()
= ""; = "";
syn TypeDecl Component.typeDecl() = enclosingTypeDecl(); syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
eq RelationComponent.typeDecl() = getTypeUse().decl(); eq RelationComponent.toTypeDecl() = getTypeUse().decl();
inh TypeDecl Component.enclosingTypeDecl(); inh TypeDecl Component.enclosingTypeDecl();
eq TypeDecl.getChild().enclosingTypeDecl() = this; eq TypeDecl.getChild().enclosingTypeDecl() = this;
eq Program.getChild().enclosingTypeDecl() = null; eq Program.getChild().enclosingTypeDecl() = null;
inh TypeDecl RelationComponent.ofTypeDecl();
eq Relation.getLeft().ofTypeDecl() = getRight().toTypeDecl();
eq Relation.getRight().ofTypeDecl() = getLeft().toTypeDecl();
eq Program.getChild().ofTypeDecl() = null;
syn boolean Component.isAlreadyDeclared() syn boolean Component.isAlreadyDeclared()
= lookupComponent(typeDecl(), name()) != this; = isDeclaringName() && lookupComponent(toTypeDecl(), name()) != this;
inh Component Component.lookupComponent(TypeDecl td, String name); inh Component Component.lookupComponent(TypeDecl td, String name);
eq Program.getChild().lookupComponent(TypeDecl td, String name) { eq Program.getChild().lookupComponent(TypeDecl td, String name) {
if (td != null) { if (td != null) {
...@@ -50,15 +64,27 @@ aspect ComponentAnalysis { ...@@ -50,15 +64,27 @@ aspect ComponentAnalysis {
} }
} }
for (Relation r: getRelations()) { for (Relation r: getRelations()) {
if (r.getLeft().typeDecl() == td && r.getLeft().name().equals(name)) { Component c = r.getLeft().lookup(td, name);
return r.getLeft(); if (c != null) return c;
} c = r.getRight().lookup(td, name);
if (r.getRight().typeDecl() == td && r.getRight().name().equals(name)) { if (c != null) return c;
return r.getRight();
}
} }
return null; return null;
} }
syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
= isDeclaringName() && toTypeDecl() == td && name().equals(name)
? this
: null;
coll Set<RelationComponent> TypeDecl.relationComponents()
[new HashSet<RelationComponent>()]
root Program;
RelationComponent contributes this
when isDeclaringName() && toTypeDecl() != null
to TypeDecl.relationComponents()
for toTypeDecl();
} }
aspect Utils { aspect Utils {
......
aspect BackendAbstractGrammar {
public String Program.generateAbstractGrammar() {
StringBuilder sb = new StringBuilder();
generateAbstractGrammar(sb);
return sb.toString();
}
public void Program.generateAbstractGrammar(StringBuilder sb) {
for (TypeDecl td: getTypeDecls()) {
td.generateAbstractGrammar(sb);
}
}
public void TypeDecl.generateAbstractGrammar(StringBuilder sb) {
if (getAbstract()) {
sb.append("abstract ");
}
sb.append(getID());
if (hasSuper()) {
sb.append(" : " + getSuper());
}
if (getNumComponent() > 0 || relationComponents().size() > 0) {
sb.append(" ::=");
}
for (Component c: getComponents()) {
sb.append(" ");
sb.append(c.generateAbstractGrammar());
}
for (RelationComponent c: relationComponents()) {
sb.append(" ");
sb.append(c.generateAbstractGrammar());
}
sb.append(";\n");
}
public String Component.generateAbstractGrammar() {
if (getID().equals(getTypeUse().toString())) {
return getTypeUse().toString();
} else {
return getID() + ":" + getTypeUse();
}
}
public String ListComponent.generateAbstractGrammar() {
return super.generateAbstractGrammar() + "*";
}
public String OptComponent.generateAbstractGrammar() {
return "[" + super.generateAbstractGrammar() + "]";
}
public String TokenComponent.generateAbstractGrammar() {
return "<" + getID() + ":" + getTypeUse() + ">";
}
public String RelationComponent.generateAbstractGrammar() {
return "<_impl_" + getID() + ":" + ofTypeDecl() + ">";
}
public String ManyRelationComponent.generateAbstractGrammar() {
return "<_impl_" + getID() + ":MySet<" + ofTypeDecl() + ">>";
}
}
\ No newline at end of file
...@@ -16,7 +16,7 @@ aspect Errors { ...@@ -16,7 +16,7 @@ aspect Errors {
to Program.errors(); to Program.errors();
Component contributes error("Component '" + name() Component contributes error("Component '" + name()
+ "' is already declared for type '" + typeDecl() + "'") + "' is already declared for type '" + toTypeDecl() + "'")
when isAlreadyDeclared() when isAlreadyDeclared()
to Program.errors(); to Program.errors();
} }
......
...@@ -66,7 +66,7 @@ RelationComponent relation_comp = ...@@ -66,7 +66,7 @@ RelationComponent relation_comp =
| LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationComponent(ID, u); :} | LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationComponent(ID, u); :}
| LBRACKET type_use.u RBRACKET {: return new OptionalRelationComponent("", u); :} | LBRACKET type_use.u RBRACKET {: return new OptionalRelationComponent("", u); :}
// Many // Many
| type_use.u STAR DOT ID {: return new ManyRelationComponent(ID, u); :} | type_use.u DOT ID STAR {: return new ManyRelationComponent(ID, u); :}
| type_use.u STAR {: return new ManyRelationComponent("", u); :} | type_use.u STAR {: return new ManyRelationComponent("", u); :}
; ;
......
...@@ -40,7 +40,7 @@ public class Compiler { ...@@ -40,7 +40,7 @@ public class Compiler {
} }
System.exit(1); System.exit(1);
} else { } else {
System.out.println(p.dumpTree()); System.out.println(p.generateAbstractGrammar());
} }
/* } else { /* } else {
if (optionPrintDot.isSet()) { if (optionPrintDot.isSet()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment