Errors.jrag 3.73 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
3
4
5
6
7
8
9
import java.util.Set;
import java.util.TreeSet;
import java.util.LinkedList;

aspect Errors {
  coll Set<ErrorMessage> Ros2Rag.errors()
    [new TreeSet<ErrorMessage>()]
    root Ros2Rag;

René Schöne's avatar
René Schöne committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    ReadFromMqttDefinition contributes error("Read definition already defined for " + getToken().getName())
      when isAlreadyDefined()
      to Ros2Rag.errors();

    ReadFromMqttDefinition contributes error("Reading target token must not be an NTA token!")
      when getToken().getNTA()
      to Ros2Rag.errors();

    // if first mapping is null, then suitableDefaultMapping() == null
    ReadFromMqttDefinition contributes error("No suitable default mapping found for type " +
      ((getMappingList().isEmpty())
        ? getToken().getJavaTypeUse().prettyPrint()
        : getMappingList().get(0).getFromType().prettyPrint()))
      when effectiveMappings().get(0) == null
      to Ros2Rag.errors();
René Schöne's avatar
René Schöne committed
25

René Schöne's avatar
René Schöne committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    ReadFromMqttDefinition contributes error("to-type of last mapping must be type of the Token!")
      when getToken().getJavaTypeUse().prettyPrint().equals(
          effectiveMappings().get(effectiveMappings().size() - 1))
      to Ros2Rag.errors();

    WriteToMqttDefinition contributes error("Writing target token must be an NTA token!")
      when !getToken().getNTA()
      to Ros2Rag.errors();

    WriteToMqttDefinition contributes error("Write definition already defined for " + getToken().getName())
      when isAlreadyDefined()
      to Ros2Rag.errors();

    DependencyDefinition contributes error("Dependency definition already defined for " + getSource().containingTypeDecl().getName() + " with name " + getID())
      when isAlreadyDefined()
      to Ros2Rag.errors();

    DependencyDefinition contributes error("The name of a dependency definition must not be equal to a list-node on the source")
      when isAlreadyDefinedAsList()
      to Ros2Rag.errors();

    DependencyDefinition contributes error("There must be a write update definition targeting " + getSource().parentTypeypeAndName() + " for dependency definition " + getID())
      when targetUpdateDefinition() == null
René Schöne's avatar
René Schöne committed
49
50
51
      to Ros2Rag.errors();
}

René Schöne's avatar
René Schöne committed
52
53
54
55
56
57
58
59
60
61
62
aspect ErrorHelpers {
  syn boolean DependencyDefinition.isAlreadyDefinedAsList() {
    for (Component comp : getSource().containingTypeDecl().getComponentList()) {
      if (comp.isListComponent() && comp.getName().equals(this.getID())) {
        return true;
      }
    }
    return false;
  }
  syn String TokenComponent.parentTypeypeAndName() = containingTypeDecl().getName() + "." + getName();
}
René Schöne's avatar
René Schöne committed
63
64
65
66
67
68
69
70
71
72
73

aspect ErrorMessage {
  public class ErrorMessage implements Comparable<ErrorMessage> {
    private final ASTNode node;
    private final String filename;
    private final int line;
    private final int col;
    private final String message;

    public ErrorMessage(ASTNode node, String message) {
      this.node = node;
René Schöne's avatar
René Schöne committed
74
      this.filename = node.containedFileName();
René Schöne's avatar
René Schöne committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      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 filename + " Line " + line + ", column " + col + ": " + message;
    }

    @Override
    public int compareTo(ErrorMessage err) {
      int n = filename.compareTo(err.filename);
      if (n != 0) {
        return n;
      }

      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);
  }
}