Commit 8cb3b104 authored by Johannes Mey's avatar Johannes Mey
Browse files

tabs to spaces

parent 63fedff9
......@@ -2,144 +2,144 @@ import java.util.*;
aspect TypeAnalysis {
public abstract TypeUse Component.getTypeUse();
syn TypeDecl TypeUse.decl() = lookupType(getID());
inh TypeDecl TypeUse.lookupType(String name);
eq Program.getChild().lookupType(String name) {
for (TypeDecl td: getTypeDecls()) {
if (td.getID().equals(name)) {
return td;
}
}
return null;
}
syn boolean TypeDecl.isAlreadyDeclared()
= lookupType(getID()) != this;
inh TypeDecl TypeDecl.lookupType(String name);
public abstract TypeUse Component.getTypeUse();
syn TypeDecl TypeUse.decl() = lookupType(getID());
inh TypeDecl TypeUse.lookupType(String name);
eq Program.getChild().lookupType(String name) {
for (TypeDecl td: getTypeDecls()) {
if (td.getID().equals(name)) {
return td;
}
}
return null;
}
syn boolean TypeDecl.isAlreadyDeclared()
= lookupType(getID()) != this;
inh TypeDecl TypeDecl.lookupType(String name);
}
aspect ComponentAnalysis {
syn boolean Component.isTargetOfDirectedRelation() = false;
eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection();
inh boolean RelationComponent.isTargetOfRightDirection();
eq Relation.getRight().isTargetOfRightDirection()
= getDirection() instanceof RightDirection;
eq Program.getChild().isTargetOfRightDirection() = false;
syn String Component.name() = getID();
syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
eq RelationComponent.toTypeDecl() = getTypeUse().decl();
inh TypeDecl Component.enclosingTypeDecl();
eq TypeDecl.getChild().enclosingTypeDecl() = this;
eq Program.getChild().enclosingTypeDecl() = null;
inh RelationComponent RelationComponent.otherSide();
eq Relation.getLeft().otherSide() = getRight();
eq Relation.getRight().otherSide() = getLeft();
eq Program.getChild().otherSide() = null;
syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl();
syn boolean Component.isAlreadyDeclared()
= !isTargetOfDirectedRelation()
&& toTypeDecl() != null
&& lookupComponent(toTypeDecl(), name()) != this;
inh Component Component.lookupComponent(TypeDecl td, String name);
eq Program.getChild().lookupComponent(TypeDecl td, String name)
= lookupComponentSyn(td, name);
syn Component Program.lookupComponentSyn(TypeDecl td, String name) {
// Check super type first to find duplicates (shadowing is not allowed)
if (td.hasSuper() && td.getSuper().decl() != null) {
Component c = lookupComponentSyn(td.getSuper().decl(), name);
if (c != null) return c;
}
for (Component c: td.getComponents()) {
if (c.name().equals(name)) {
return c;
}
}
for (Relation r: getRelations()) {
Component c = r.getLeft().lookup(td, name);
if (c != null) return c;
c = r.getRight().lookup(td, name);
if (c != null) return c;
}
return null;
}
syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
= !isTargetOfDirectedRelation() && toTypeDecl() == td && name().equals(name)
? this
: null;
coll Set<RelationComponent> TypeDecl.relationComponents()
[new HashSet<RelationComponent>()]
root Program;
RelationComponent contributes this
when !isTargetOfDirectedRelation() && toTypeDecl() != null
to TypeDecl.relationComponents()
for toTypeDecl();
syn Set<OneRelationComponent> TypeDecl.oneRelationComponents() {
Set<OneRelationComponent> set = new HashSet<>();
for (RelationComponent rc: relationComponents()) {
if (rc instanceof OneRelationComponent) {
set.add((OneRelationComponent) rc);
}
}
return set;
}
syn boolean Component.isTargetOfDirectedRelation() = false;
eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection();
inh boolean RelationComponent.isTargetOfRightDirection();
eq Relation.getRight().isTargetOfRightDirection()
= getDirection() instanceof RightDirection;
eq Program.getChild().isTargetOfRightDirection() = false;
syn String Component.name() = getID();
syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
eq RelationComponent.toTypeDecl() = getTypeUse().decl();
inh TypeDecl Component.enclosingTypeDecl();
eq TypeDecl.getChild().enclosingTypeDecl() = this;
eq Program.getChild().enclosingTypeDecl() = null;
inh RelationComponent RelationComponent.otherSide();
eq Relation.getLeft().otherSide() = getRight();
eq Relation.getRight().otherSide() = getLeft();
eq Program.getChild().otherSide() = null;
syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl();
syn boolean Component.isAlreadyDeclared()
= !isTargetOfDirectedRelation()
&& toTypeDecl() != null
&& lookupComponent(toTypeDecl(), name()) != this;
inh Component Component.lookupComponent(TypeDecl td, String name);
eq Program.getChild().lookupComponent(TypeDecl td, String name)
= lookupComponentSyn(td, name);
syn Component Program.lookupComponentSyn(TypeDecl td, String name) {
// Check super type first to find duplicates (shadowing is not allowed)
if (td.hasSuper() && td.getSuper().decl() != null) {
Component c = lookupComponentSyn(td.getSuper().decl(), name);
if (c != null) return c;
}
for (Component c: td.getComponents()) {
if (c.name().equals(name)) {
return c;
}
}
for (Relation r: getRelations()) {
Component c = r.getLeft().lookup(td, name);
if (c != null) return c;
c = r.getRight().lookup(td, name);
if (c != null) return c;
}
return null;
}
syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
= !isTargetOfDirectedRelation() && toTypeDecl() == td && name().equals(name)
? this
: null;
coll Set<RelationComponent> TypeDecl.relationComponents()
[new HashSet<RelationComponent>()]
root Program;
RelationComponent contributes this
when !isTargetOfDirectedRelation() && toTypeDecl() != null
to TypeDecl.relationComponents()
for toTypeDecl();
syn Set<OneRelationComponent> TypeDecl.oneRelationComponents() {
Set<OneRelationComponent> set = new HashSet<>();
for (RelationComponent rc: relationComponents()) {
if (rc instanceof OneRelationComponent) {
set.add((OneRelationComponent) rc);
}
}
return set;
}
}
aspect Constructors {
syn Collection<Component> TypeDecl.componentsTransitive() {
ArrayList<Component> list = new ArrayList<>();
if (hasSuper() && getSuper().decl() != null) {
list.addAll(getSuper().decl().componentsTransitive());
}
for (Component c: getComponents()) {
list.add(c);
}
return list;
}
syn boolean TypeDecl.needsConstructor() {
if (componentsTransitive().isEmpty()) {
return false;
}
if (!relationComponents().isEmpty()) {
return true;
}
return hasSuper()
&& getSuper().decl() != null
&& getSuper().decl().needsConstructor();
}
syn Collection<Component> TypeDecl.componentsTransitive() {
ArrayList<Component> list = new ArrayList<>();
if (hasSuper() && getSuper().decl() != null) {
list.addAll(getSuper().decl().componentsTransitive());
}
for (Component c: getComponents()) {
list.add(c);
}
return list;
}
syn boolean TypeDecl.needsConstructor() {
if (componentsTransitive().isEmpty()) {
return false;
}
if (!relationComponents().isEmpty()) {
return true;
}
return hasSuper()
&& getSuper().decl() != null
&& getSuper().decl().needsConstructor();
}
}
aspect Utils {
public String SimpleTypeUse.toString() {
return getID();
}
public String ParameterizedTypeUse.toString() {
StringBuilder sb = new StringBuilder();
sb.append(getID()).append("<");
int i = 0;
for (TypeUse u: getTypeUses()) {
sb.append(u.toString());
if (++i < getNumTypeUse()) {
sb.append(", ");
}
}
sb.append(">");
return sb.toString();
}
public String TypeDecl.toString() {
return getID();
}
public String SimpleTypeUse.toString() {
return getID();
}
public String ParameterizedTypeUse.toString() {
StringBuilder sb = new StringBuilder();
sb.append(getID()).append("<");
int i = 0;
for (TypeUse u: getTypeUses()) {
sb.append(u.toString());
if (++i < getNumTypeUse()) {
sb.append(", ");
}
}
sb.append(">");
return sb.toString();
}
public String TypeDecl.toString() {
return getID();
}
}
This diff is collapsed.
......@@ -6,60 +6,60 @@ import java.lang.reflect.InvocationTargetException;
// _ALL_ AST nodes and can cause collisions in the abstract grammar namespace.
aspect DumpTree {
private static final String ASTNode.DUMP_TREE_INDENT = " ";
private static final String ASTNode.DUMP_TREE_INDENT = " ";
public String ASTNode.dumpTree() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
dumpTree(new PrintStream(bytes));
return bytes.toString();
}
public String ASTNode.dumpTree() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
dumpTree(new PrintStream(bytes));
return bytes.toString();
}
public void ASTNode.dumpTree(PrintStream out) {
dumpTree(out, "");
out.flush();
}
public void ASTNode.dumpTree(PrintStream out) {
dumpTree(out, "");
out.flush();
}
public void ASTNode.dumpTree(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 void ASTNode.dumpTree(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.extraDumpInfo() { return ""; }
public String ASTNode.getTokens() {
java.util.TreeSet<java.lang.reflect.Method> methods = new java.util.TreeSet<>(
new java.util.Comparator<java.lang.reflect.Method>() {
public int compare(java.lang.reflect.Method m1, java.lang.reflect.Method m2) {
return m1.getName().compareTo(m2.getName());
}
});
public String ASTNode.getTokens() {
java.util.TreeSet<java.lang.reflect.Method> methods = new java.util.TreeSet<>(
new java.util.Comparator<java.lang.reflect.Method>() {
public int compare(java.lang.reflect.Method m1, java.lang.reflect.Method m2) {
return m1.getName().compareTo(m2.getName());
}
});
methods.addAll(java.util.Arrays.asList(getClass().getMethods()));
methods.addAll(java.util.Arrays.asList(getClass().getMethods()));
String result = "";
for (java.lang.reflect.Method method : methods) {
ASTNodeAnnotation.Token token = method.getAnnotation(ASTNodeAnnotation.Token.class);
if (token != null) {
try {
result += String.format(" %s=\"%s\"", token.name(), method.invoke(this));
} catch (IllegalAccessException ignored) {
} catch (InvocationTargetException ignored) {
}
}
}
return result;
}
String result = "";
for (java.lang.reflect.Method method : methods) {
ASTNodeAnnotation.Token token = method.getAnnotation(ASTNodeAnnotation.Token.class);
if (token != null) {
try {
result += String.format(" %s=\"%s\"", token.name(), method.invoke(this));
} catch (IllegalAccessException ignored) {
} catch (InvocationTargetException ignored) {
}
}
}
return result;
}
}
\ No newline at end of file
......@@ -3,103 +3,103 @@ import java.util.TreeSet;
import java.util.LinkedList;
aspect Errors {
coll Set<ErrorMessage> Program.errors()
[new TreeSet<ErrorMessage>()]
root Program;
TypeUse contributes error("Type '" + getID() + "' not found")
when decl() == null && !isToken()
to Program.errors();
TypeDecl contributes error("Type '" + getID() + "' is already declared")
when isAlreadyDeclared()
to Program.errors();
Component contributes error("Component '" + name()
+ "' is already declared for type '" + toTypeDecl() + "'")
when isAlreadyDeclared()
to Program.errors();
RelationComponent contributes
error("Role name missing for type '" + toTypeDecl() + "'")
when !isTargetOfDirectedRelation() && name().isEmpty()
to Program.errors();
RelationComponent contributes
error("The target of a directed relation cannot have a role name")
when isTargetOfDirectedRelation() && !getID().isEmpty()
to Program.errors();
RelationComponent contributes
error("The target of a directed relation may only have multiplicity 1")
when isTargetOfDirectedRelation() && !multiplicityOne()
to Program.errors();
coll Set<ErrorMessage> Program.errors()
[new TreeSet<ErrorMessage>()]
root Program;
TypeUse contributes error("Type '" + getID() + "' not found")
when decl() == null && !isToken()
to Program.errors();
TypeDecl contributes error("Type '" + getID() + "' is already declared")
when isAlreadyDeclared()
to Program.errors();
Component contributes error("Component '" + name()
+ "' is already declared for type '" + toTypeDecl() + "'")
when isAlreadyDeclared()
to Program.errors();
RelationComponent contributes
error("Role name missing for type '" + toTypeDecl() + "'")
when !isTargetOfDirectedRelation() && name().isEmpty()
to Program.errors();
RelationComponent contributes
error("The target of a directed relation cannot have a role name")
when isTargetOfDirectedRelation() && !getID().isEmpty()
to Program.errors();
RelationComponent contributes
error("The target of a directed relation may only have multiplicity 1")
when isTargetOfDirectedRelation() && !multiplicityOne()
to Program.errors();
}
aspect HelpAttributes {
inh Program ASTNode.program();
eq Program.getChild().program() = this;
inh boolean TypeUse.isToken();
eq Program.getChild().isToken() = false;
eq TokenComponent.getTypeUse().isToken() = true;
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;
inh Program ASTNode.program();
eq Program.getChild().program() = this;
inh boolean TypeUse.isToken();
eq Program.getChild().isToken() = false;
eq TokenComponent.getTypeUse().isToken() = true;
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;
}
aspect ErrorMessage {
public class ErrorMessage implements Comparable<ErrorMessage> {
private final ASTNode node;
private final int line;
private final int col;
private final String message;
public ErrorMessage(ASTNode node, String message) {
this.node = node;
this.line = node.getStartLine();
this.col = node.getStartColumn();
this.message = message;
}
public ASTNode getNode() {
return node;
}
public int getLine() {
return line;
}
public int getCol() {
return col;
}
public String getMessage() {
return message;
}
public String toString() {
return "Line " + line + ", column " + col + ": " + message;
}
@Override
public int compareTo(ErrorMessage err) {
int n = line - err.line;
if (n != 0) {
return n;
}
n = col-err.col;
if (n != 0) {
return n;
}
return message.compareTo(err.message);
}
}
protected ErrorMessage ASTNode.error(String message) {
return new ErrorMessage(this, message);
}
public class ErrorMessage implements Comparable<ErrorMessage> {
private final ASTNode node;
private final int line;
private final int col;
private final String message;
public ErrorMessage(ASTNode node, String message) {
this.node = node;
this.line = node.getStartLine();
this.col = node.getStartColumn();
this.message = message;
}
public ASTNode getNode() {
return node;
}
public int getLine() {
return line;
}
public int getCol() {
return col;
}
public String getMessage() {
return message;
}
public String toString() {
return "Line " + line + ", column " + col + ": " + message;
}
@Override
public int compareTo(ErrorMessage err) {
int n = line - err.line;
if (n != 0) {
return n;
}
n = col-err.col;
if (n != 0) {
return n;
}
return message.compareTo(err.message);
}
}
protected ErrorMessage ASTNode.error(String message) {
return new ErrorMessage(this, message);
}
}
\ No newline at end of file
......@@ -29,9 +29,9 @@ import org.jastadd.relast.parser.RelAstParser.Terminals;
public static class ScannerError extends Error {
public ScannerError(String message) {
super(message);
}
public ScannerError(String message) {
super(message);
}
}
%}
......@@ -46,42 +46,42 @@ ID = [a-zA-Z$_][a-zA-Z0-9$_]*
%%
<YYINITIAL> {
{WhiteSpace} { /* ignore */ }
{Comment} { /* ignore */ }
"abstract" { return sym(Terminals.ABSTRACT); }
"rel" { return sym(Terminals.RELATION); }
";" { return sym(Terminals.SCOL); }
":" { return sym(Terminals.COL); }
"::=" { return sym(Terminals.ASSIGN); }
"*" { return sym(Terminals.STAR); }
"." { return sym(Terminals.DOT); }
"," { return sym(Terminals.COMMA); }
"<" { return sym(Terminals.LT); }
">" { return sym(Terminals.GT); }
"[" { return sym(Terminals.LBRACKET); }
"]" { return sym(Terminals.RBRACKET); }
"/" { return sym(Terminals.SLASH); }
"?" { return sym(Terminals.QUESTION_MARK); }
"->" { return sym(Terminals.RIGHT); }
"<->" { return sym(Terminals.BIDIRECTIONAL); }
// ID
{ID} { return sym(Terminals.ID); }
\" { stringLitSb.setLength(0); yybegin(STRING); }
<<EOF>> { return sym(Terminals.EOF); }
{WhiteSpace} { /* ignore */ }