Commit efc50dfa authored by Johannes Mey's avatar Johannes Mey
Browse files

separate relast preprocessor from ros2rag

parent 92454a4a
......@@ -67,7 +67,7 @@ task relast(type: JavaExec) {
args = [
"../libs/relast.jar",
"./src/main/jastadd/RelAst.relast",
"../relast.preprocessor/src/main/jastadd/RelAst.relast",
"./src/main/jastadd/Ros2Rag.relast",
"--listClass=java.util.ArrayList",
"--jastAddList=JastAddList",
......@@ -78,7 +78,7 @@ task relast(type: JavaExec) {
]
inputs.files file("../libs/relast.jar"),
file("src/main/jastadd/RelAST.relast"),
file("../relast.preprocessor/src/main/jastadd/RelAST.relast"),
file("src/main/jastadd/Ros2Rag.relast")
outputs.files file("./src/gen/jastadd/Ros2Rag.ast"),
file("src/gen/jastadd/Ros2Rag.jadd"),
......@@ -93,19 +93,24 @@ jastadd {
module("Ros2Rag") {
java {
basedir "."
include "src/main/**/*.java"
include "src/gen/**/*.java"
basedir ".."
include "relast.preprocessor/main/**/*.java"
include "relast.preprocessor/gen/**/*.java"
include "ros2rag.base/src/main/**/*.java"
include "ros2rag.base/src/gen/**/*.java"
}
jastadd {
basedir "."
include "src/main/jastadd/**/*.ast"
include "src/main/jastadd/**/*.jadd"
include "src/main/jastadd/**/*.jrag"
include "src/gen/jastadd/**/*.ast"
include "src/gen/jastadd/**/*.jadd"
include "src/gen/jastadd/**/*.jrag"
basedir ".."
include "relast.preprocessor/src/main/jastadd/**/*.ast"
include "relast.preprocessor/src/main/jastadd/**/*.jadd"
include "relast.preprocessor/src/main/jastadd/**/*.jrag"
include "ros2rag.base/src/main/jastadd/**/*.ast"
include "ros2rag.base/src/main/jastadd/**/*.jadd"
include "ros2rag.base/src/main/jastadd/**/*.jrag"
include "ros2rag.base/src/gen/jastadd/**/*.ast"
include "ros2rag.base/src/gen/jastadd/**/*.jadd"
include "ros2rag.base/src/gen/jastadd/**/*.jrag"
}
scanner {
......@@ -113,9 +118,10 @@ jastadd {
}
parser {
include "src/main/jastadd/Preamble.parser"
include "src/main/jastadd/RelAst.parser"
include "src/main/jastadd/Ros2Rag.parser"
basedir ".."
include "ros2rag.base/src/main/jastadd/Preamble.parser"
include "relast.preprocessor/src/main/jastadd/RelAst.parser"
include "ros2rag.base/src/main/jastadd/Ros2Rag.parser"
}
}
}
......
aspect DumpTree {
private static final String ASTNode.DUMP_TREE_INDENT = " ";
public String ASTNode.dumpTree() {
java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream();
dumpTree(new java.io.PrintStream(bytes));
return bytes.toString();
}
public void ASTNode.dumpTree(java.io.PrintStream out) {
dumpTree(out, "");
out.flush();
}
public void ASTNode.dumpTree(java.io.PrintStream out, String indent) {
out.print(indent + getClass().getSimpleName());
out.print(getTokens());
String extra = extraDumpInfo();
if (!extra.isEmpty()) {
out.print(" " + extra);
}
out.println();
String childIndent = indent + DUMP_TREE_INDENT;
for (ASTNode<?> child : astChildren()) {
if (child == null) {
out.println(childIndent + "null");
} else {
child.dumpTree(out, childIndent);
}
}
}
public String ASTNode.extraDumpInfo() { return ""; }
public String ASTNode.getTokens() {
java.util.TreeSet<java.lang.reflect.Method> methods = new java.util.TreeSet<>(java.util.Comparator.comparing(java.lang.reflect.Method::getName));
methods.addAll(java.util.Arrays.asList(getClass().getMethods()));
StringBuilder result = new StringBuilder();
for (java.lang.reflect.Method method : methods) {
ASTNodeAnnotation.Token token = method.getAnnotation(ASTNodeAnnotation.Token.class);
if (token != null) {
try {
result.append(String.format(" %s=\"%s\"", token.name(), method.invoke(this)));
} catch (IllegalAccessException ignored) {
} catch (java.lang.reflect.InvocationTargetException ignored) {
}
}
}
return result.toString();
}
}
aspect NameResolution {
refine RefResolverStubs eq ASTNode.globallyResolveTypeDeclByToken(String id) = program().resolveTypeDecl(id);
syn TypeDecl Program.resolveTypeDecl(String name) {
for (TypeDecl decl : typeDecls()) {
if (decl.getName().equals(name)) {
return decl;
}
}
throw new RuntimeException("TypeDecl " + name + " could not be resolved.");
}
refine RefResolverStubs eq ASTNode.globallyResolveTokenComponentByToken(String id) {
// return a TokenComponent. id is of the form 'type_name + "." + token_name'
int dotIndex = id.indexOf(".");
String typeName = id.substring(0, dotIndex);
String tokenName = id.substring(dotIndex + 1);
TypeDecl type = program().resolveTypeDecl(typeName);
// iterate over components and find the matching tokenComponent
for (Component comp : type.getComponentList()) {
if (comp.isTokenComponent() && comp.getName().equals(tokenName)) {
return comp.asTokenComponent();
}
}
System.err.println("Could not resolve TokenComponent '" + id + "'.");
return null;
}
refine RefResolverStubs eq UpdateDefinition.resolveMappingByToken(String id) {
// return a MappingDefinition
for (MappingDefinition mappingDefinition : ros2rag().getMappingDefinitionList()) {
......
aspect Navigation {
// --- program ---
inh Program ASTNode.program();
eq Program.getChild().program() = this;
eq Ros2Rag.getChild().program() = getProgram();
// --- ros2rag
inh Ros2Rag ASTNode.ros2rag();
eq Ros2Rag.getChild().ros2rag() = this;
// --- typeDecls ---
coll java.util.Set<TypeDecl> Program.typeDecls() [new java.util.HashSet<>()] root Program;
TypeDecl contributes this
to Program.typeDecls()
for program();
// --- relations ---
coll java.util.Set<Relation> Program.relations() [new java.util.HashSet<>()] root Program;
Relation contributes this
to Program.relations()
for program();
// --- containingTypeDecl ---
inh TypeDecl Component.containingTypeDecl();
eq TypeDecl.getChild().containingTypeDecl() = this;
// syn boolean RelationComponent.multiplicityOne() = false;
// eq OneRelationComponent.multiplicityOne() = true;
// syn boolean RelationComponent.multiplicityOpt() = false;
// eq OptionalRelationComponent.multiplicityOpt() = true;
// syn boolean RelationComponent.multiplicityMany() = false;
// eq ManyRelationComponent.multiplicityMany() = true;
// --- containedFile ---
inh GrammarFile ASTNode.containedFile();
eq GrammarFile.getChild().containedFile() = this;
// --- isTokenComponent ---
syn boolean Component.isTokenComponent() = false;
eq TokenComponent.isTokenComponent() = true;
// --- asTokenComponent ---
syn TokenComponent Component.asTokenComponent() = null;
eq TokenComponent.asTokenComponent() = this;
}
GrammarFile goal
= declaration.d goal.p {: p.getDeclarationList().insertChild(d, 0); return p; :}
| {: return new GrammarFile(); :}
;
Declaration declaration
= type_decl
| relation
| comment
;
Comment comment
= COMMENT {: return new Comment(COMMENT); :};
TypeDecl type_decl
= ID components_opt.c SCOL
{:
TypeDecl result = new TypeDecl();
result.setName(ID);
result.setAbstract(false);
result.setComponentList(c);
return result;
:}
| ID COL ID.s components_opt.c SCOL
{:
TypeDecl result = new TypeDecl();
result.setName(ID);
result.setAbstract(false);
result.setSuperType(TypeDecl.createRef(s));
result.setComponentList(c);
return result;
:}
| ABSTRACT ID components_opt.c SCOL
{:
TypeDecl result = new TypeDecl();
result.setName(ID);
result.setAbstract(true);
result.setComponentList(c);
return result;
:}
| ABSTRACT ID COL ID.s components_opt.c SCOL
{:
TypeDecl result = new TypeDecl();
result.setName(ID);
result.setAbstract(true);
result.setSuperType(TypeDecl.createRef(s));
result.setComponentList(c);
return result;
:}
;
JastAddList components_opt
= /* empty */ {: return new JastAddList(); :}
| ASSIGN components.l {: return l; :}
;
JastAddList components
= /* empty */ {: return new JastAddList(); :}
| components.l component.c {: return l.add(c); :}
;
Component component
= SLASH actual_component.c SLASH
{:
c.setNTA(true);
return c;
:}
| actual_component
;
Component actual_component
= ID.name COL ID.type STAR
{:
ListComponent result = new ListComponent();
result.setName(name);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| ID.name COL ID.type
{:
NormalComponent result = new NormalComponent();
result.setName(name);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| ID.type STAR
{:
ListComponent result = new ListComponent();
result.setName(type);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| ID.type
{:
NormalComponent result = new NormalComponent();
result.setName(type);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| LBRACKET ID.name COL ID.type RBRACKET
{:
OptComponent result = new OptComponent();
result.setName(name);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| LBRACKET ID.type RBRACKET
{:
OptComponent result = new OptComponent();
result.setName(type);
result.setTypeDecl(TypeDecl.createRef(type));
return result;
:}
| LT ID.name COL java_type_use.type GT
{:
TokenComponent result = new TokenComponent();
result.setName(name);
result.setJavaTypeUse(type);
return result;
:}
| LT ID.type GT
{:
TokenComponent result = new TokenComponent();
result.setName(type);
result.setJavaTypeUse(new SimpleJavaTypeUse(type));
return result;
:}
;
JavaTypeUse java_type_use
= parameterized_java_type_use
| simple_java_type_use
;
ArrayList qualified_name
= ID
| qualified_name DOT ID
;
JastAddList java_type_use_list
= java_type_use.u {: return new JastAddList().add(u); :}
| java_type_use_list.l COMMA java_type_use.u {: return l.add(u); :}
;
SimpleJavaTypeUse simple_java_type_use
= qualified_name.n
{:
return new SimpleJavaTypeUse((String)n.stream().map( x -> ((Symbol)x).value.toString()).collect(java.util.stream.Collectors.joining(".")));
:}
;
ParameterizedJavaTypeUse parameterized_java_type_use
= simple_java_type_use.i LT java_type_use_list.l GT
{:
return new ParameterizedJavaTypeUse(i.getName(), l);
:}
;
Relation relation
= RELATION unnamed_role.a LEFT navigable_role.b SCOL
{:
LeftDirectedRelation result = new LeftDirectedRelation();
result.setSource(b);
result.setTarget(a);
return result;
:}
| RELATION navigable_role.a RIGHT unnamed_role.b SCOL
{:
RightDirectedRelation result = new RightDirectedRelation();
result.setSource(a);
result.setTarget(b);
return result;
:}
| RELATION navigable_role.a BIDIRECTIONAL navigable_role.b SCOL
{:
BidirectionalRelation result = new BidirectionalRelation();
result.setLeft(a);
result.setRight(b);
return result;
:}
;
UnnamedRole unnamed_role
= ID.type
{:
UnnamedRole result = new UnnamedRole();
result.setType(TypeDecl.createRef(type));
return result;
:}
| navigable_role
;
NavigableRole navigable_role
= ID.type DOT ID.name
{:
NormalRole result = new NormalRole();
result.setName(name);
result.setType(TypeDecl.createRef(type));
return result;
:}
| ID.type DOT ID.name STAR
{:
ListRole result = new ListRole();
result.setName(name);
result.setType(TypeDecl.createRef(type));
return result;
:}
| ID.type DOT ID.name QUESTION_MARK
{:
OptRole result = new OptRole();
result.setName(name);
result.setType(TypeDecl.createRef(type));
return result;
:}
;
Program ::= GrammarFile *;
abstract Grammar ::= Declaration*;
GrammarFile : Grammar ::= <FileName> ;
abstract Declaration;
TypeDecl:Declaration ::= <Name> <Abstract:boolean> Component*;
rel TypeDecl.SuperType? <-> TypeDecl.SubType*;
abstract Component ::= <Name> <NTA:boolean>;
abstract TypeComponent : Component;
rel TypeComponent.TypeDecl <-> TypeDecl.PotentialParent*;
NormalComponent : TypeComponent;
ListComponent : TypeComponent;
OptComponent : TypeComponent;
TokenComponent : Component ::= JavaTypeUse;
abstract JavaTypeUse ::= <Name>;
SimpleJavaTypeUse : JavaTypeUse;
ParameterizedJavaTypeUse : JavaTypeUse ::= JavaTypeUse*;
abstract Relation : Declaration;
// rel Source.Name -> Target;
// rel Target <- Source.Name;
abstract DirectedRelation : Relation ::= Source:NavigableRole Target:UnnamedRole ;
LeftDirectedRelation : DirectedRelation;
RightDirectedRelation : DirectedRelation;
// rel Left.LeftName <-> Right.RightName;
BidirectionalRelation : Relation ::= Left:NavigableRole Right:NavigableRole ;
abstract Role;
abstract NavigableRole : Role ::= <Name>;
NormalRole : NavigableRole;
ListRole : NavigableRole;
OptRole : NavigableRole;
UnnamedRole : Role ;
rel Role.Type <-> TypeDecl.Role*;
// comments
Comment : Declaration ::= <Text>;
aspect BackendAbstractGrammar {
public static String ASTNode.listInterface = "java.util.List";
public static String ASTNode.listClass = "java.util.ArrayList";
public static String ASTNode.jastAddListType = "List";
public static String ASTNode.jastAddOptType = "Opt";
public String Program.generateAbstractGrammar() {
StringBuilder sb = new StringBuilder();
generateAbstractGrammar(sb);
return sb.toString();
}
public void Program.generateAbstractGrammar(StringBuilder b) {
for (GrammarFile file : getGrammarFileList()) {
file.generateAbstractGrammar(b);
}
}
public void GrammarFile.generateAbstractGrammar(StringBuilder b) {
b.append("// Grammar for file ").append(getFileName()).append("\n");
super.generateAbstractGrammar(b);
b.append("\n");
}
public void Grammar.generateAbstractGrammar(StringBuilder b) {
for (Declaration decl : getDeclarationList()) {
decl.generateAbstractGrammar(b);
}
}
abstract public void Declaration.generateAbstractGrammar(StringBuilder b);
public void Comment.generateAbstractGrammar(StringBuilder b) {
b.append(getText()).append("\n");
}
public void TypeDecl.generateAbstractGrammar(StringBuilder b) {
if (getAbstract()) {
b.append("abstract ");
}
b.append(getName()).append(" ");
if (hasSuperType()) {
b.append(": ").append(getSuperType().getName()).append(" ");
}
b.append("::=");
for (Component component : getComponentList()) {
b.append(" ");
component.generateAbstractGrammar(b);
}
b.append(";\n");
}
public abstract void Component.generateAbstractGrammar(StringBuilder b);
public void NormalComponent.generateAbstractGrammar(StringBuilder b) {
if (getNTA()) {
b.append("/");
}
if (!getName().equals("")) {
b.append(getName()).append(":");
}
b.append(getTypeDecl().getName());
if (getNTA()) {
b.append("/");
}
}
public void ListComponent.generateAbstractGrammar(StringBuilder b) {
if (getNTA()) {
b.append("/");
}
if (!getName().equals("")) {
b.append(getName()).append(":");
}
b.append(getTypeDecl().getName()).append("*");
if (getNTA()) {
b.append("/");
}
}
public void OptComponent.generateAbstractGrammar(StringBuilder b) {
if (getNTA()) {
b.append("/");
}
b.append("[");
if (!getName().equals("")) {
b.append(getName()).append(":");
}
b.append(getTypeDecl().getName()).append("]");
if (getNTA()) {
b.append("/");
}
}
public void TokenComponent.generateAbstractGrammar(StringBuilder b) {
if (getNTA()) {
b.append("/");
}
b.append("<");
if (!getName().equals("")) {
b.append(getName()).append(":");
}
getJavaTypeUse().generateAbstractGrammar(b);
b.append(">");
if (getNTA()) {
b.append("/");
}
}
abstract public void JavaTypeUse.generateAbstractGrammar(StringBuilder b);
public void SimpleJavaTypeUse.generateAbstractGrammar(StringBuilder b) {
b.append(getName());
}
public void ParameterizedJavaTypeUse.generateAbstractGrammar(StringBuilder b) {
b.append("<");
boolean first = true;
for (JavaTypeUse javaTypeUse : getJavaTypeUseList()) {
if (first) {
first = false;
} else {
b.append(", ");
}
javaTypeUse.generateAbstractGrammar(b);
}
b.append(">");
}
abstract public void Relation.generateAbstractGrammar(StringBuilder b);
public void DirectedRelation.generateAbstractGrammar(StringBuilder b) {
b.append("rel ");
getSource().generateAbstractGrammar(b);
b.append(" -> ");
getTarget().generateAbstractGrammar(b);
b.append(";\n");
}
public void BidirectionalRelation.generateAbstractGrammar(StringBuilder b) {
b.append("rel ");
getLeft().generateAbstractGrammar(b);
b.append(" <-> ");
getRight().generateAbstractGrammar(b);
b.append(";\n");
}
abstract public void Role.generateAbstractGrammar(StringBuilder b);
public void NormalRole.generateAbstractGrammar(StringBuilder b) {
b.append(getType().getName()).append(".").append(getName());
}
public void ListRole.generateAbstractGrammar(StringBuilder b) {
b.append(getType().getName()).append(".").append(getName()).append("*");
}
public void OptRole.generateAbstractGrammar(StringBuilder b) {
b.append(getType().getName()).append(".").append(getName()).append("?");
}
public void UnnamedRole.generateAbstractGrammar(StringBuilder b) {
b.append(getType().getName());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!