AbstractGrammar.jadd 2.99 KB
Newer Older
Johannes Mey's avatar
Johannes Mey committed
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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 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());
    }

    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 Relation.generateAbstractGrammar() {
    return "rel "
      + getLeft().generateAbstractGrammar() + " "
      + getDirection().generateAbstractGrammar() + " "
      + getRight().generateAbstractGrammar();
  }

  public String RelationComponent.generateAbstractGrammar() {
    if (getID().isEmpty()) {
      return getTypeUse().toString();
    } else {
      return getTypeUse() + "." +  getID();
    }
  }

  public String OptionalRelationComponent.generateAbstractGrammar() {
    return super.generateAbstractGrammar() + "?";
  }

  public String ManyRelationComponent.generateAbstractGrammar() {
    return super.generateAbstractGrammar() + "*";
  }

  abstract public String Direction.generateAbstractGrammar();
  public String RightDirection.generateAbstractGrammar() {
    return "->";
  }

  public String LeftDirection.generateAbstractGrammar() {
    return "<-";
  }

  public String Bidirectional.generateAbstractGrammar() {
    return "<->";
  }

}