AbstractGrammar.jadd 5.18 KB
Newer Older
1
2
aspect BackendAbstractGrammar {

3
4
  public static String ASTNode.listInterface = "java.util.List";
  public static String ASTNode.listClass = "java.util.ArrayList";
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  public static String ASTNode.jastAddListType = "List";

  public static boolean ASTNode.resolverHelper = false;
  public static boolean ASTNode.serializer = false;
  public static boolean ASTNode.jsonPointer = false;
  public static boolean ASTNode.manualReferences = false;
  public static boolean ASTNode.useJastAddNames = false;

  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.generateUnresolvedClass(StringBuilder sb) {
    if (getAbstract()) {
      sb.append(ind(1) + "abstract ");
    } else {
      sb.append(ind(1));
    }
31
    sb.append("class " + unresolvedPrefix + getID() + " extends " + getID() + "  implements " + unresolvedPrefix + "Node {\n");
32
33

    sb.append(ind(2) + "private String unresolved$Token;\n");
34
    sb.append(ind(2) + "public String get" + unresolvedPrefix + "Token() {\n");
35
36
    sb.append(ind(3) + "return unresolved$Token;\n");
    sb.append(ind(2) + "}\n");
37
    sb.append(ind(2) + "void set" + unresolvedPrefix + "Token(String token) {\n");
38
39
40
41
    sb.append(ind(3) + "this.unresolved$Token = token;\n");
    sb.append(ind(2) + "}\n");

    sb.append(ind(2) + "private boolean unresolved$ResolveOpposite;\n");
42
    sb.append(ind(2) + "public boolean get" + unresolvedPrefix + "ResolveOpposite() {\n");
43
44
    sb.append(ind(3) + "return unresolved$ResolveOpposite;\n");
    sb.append(ind(2) + "}\n");
45
    sb.append(ind(2) + "void set" + unresolvedPrefix + "ResolveOpposite(boolean resolveOpposite) {\n");
46
47
48
49
50
    sb.append(ind(3) + "this.unresolved$ResolveOpposite = resolveOpposite;\n");
    sb.append(ind(2) + "}\n");

    sb.append(ind(1) + "}\n");

51
    sb.append(ind(1) + unresolvedPrefix + "Node " + getID() + "." + asUnresolvedMethod + "() {\n");
52
53
    sb.append(ind(2) + "return null;\n");
    sb.append(ind(1) + "}\n");
54
    sb.append(ind(1) + unresolvedPrefix + "Node " + unresolvedPrefix + getID() + "." + asUnresolvedMethod + "() {\n");
55
56
57
    sb.append(ind(2) + "return this;\n");
    sb.append(ind(1) + "}\n");

58
    sb.append(ind(1) + "boolean " + getID() + "." + isUnresolvedMethod + "() {\n");
59
60
    sb.append(ind(2) + "return false;\n");
    sb.append(ind(1) + "}\n");
61
    sb.append(ind(1) + "boolean " + unresolvedPrefix + getID() + "." + isUnresolvedMethod + "() {\n");
62
63
64
65
66
67
68
69
70
71
72
73
74
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
122
    sb.append(ind(2) + "return true;\n");
    sb.append(ind(1) + "}\n");
  }

  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 NTAComponent.generateAbstractGrammar() {
    return "/" + super.generateAbstractGrammar() + "/";
  }
  public String NTAListComponent.generateAbstractGrammar() {
    return "/" + super.generateAbstractGrammar() + "*/";
  }
  public String NTAOptComponent.generateAbstractGrammar() {
    return "/[" + super.generateAbstractGrammar() + "]/";
  }
  public String TokenComponent.generateAbstractGrammar() {
    return "<" + getID() + ":" + getTypeUse() + ">";
  }
  public String NTATokenComponent.generateAbstractGrammar() {
    return "/<" + getID() + ":" + getTypeUse() + ">/";
  }

  public String RelationComponent.generateAbstractGrammar() {
    return "<" + getImplAttributeName() + ":" + ofTypeDecl() + ">";
  }
  public String ManyRelationComponent.generateAbstractGrammar() {
123
    return "<" + getImplAttributeName() + ":" + ASTNode.listInterface + "<" + ofTypeDecl() + ">>";
124
125
126
127
128
129
130
131
  }

  public String RelationComponent.getImplAttributeName() {
    return "_impl_" + getID();
  }

  public String RelationComponent.getImplAttributeField() {
    //  tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
132
    return "token" + org.jastadd.ast.AST.ASTNode.convTypeNameToSignature(ofTypeDecl().toString()) + "__impl_" + getID();
133
134
135
136
  }

  public String ManyRelationComponent.getImplAttributeField() {
    //  tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
137
    return "token" + org.jastadd.ast.AST.ASTNode.convTypeNameToSignature(ASTNode.listInterface) + "_" + ofTypeDecl() + "___impl_" + getID();
138
139
  }
}