JsonPointer.jrag 8.64 KB
Newer Older
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
aspect JsonPointer {

  refine RefCreatorStubs eq A.createRefToDi1(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToDi2(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToDi3(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi1(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi1(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi2(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi2(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi3(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi3(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi5(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi5(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi6(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi6(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq A.createRefToBi9(B target) = createReferenceTo(target);
  refine RefCreatorStubs eq B.createRefToBi9(A target) = createReferenceTo(target);
  refine RefCreatorStubs eq Root.createRefToD(D target) = createReferenceTo(target);
  refine RefCreatorStubs eq D.createRefToRoot(Root target) = createReferenceTo(target);

  syn String ASTNode.createReferenceTo(ASTNode target) {
      // find common subtree
      java.util.List<ASTNode> myParents = parents();
      java.util.List<ASTNode> targetParents = target.parents();
      int minSize = Math.min(myParents.size(), targetParents.size());
      if (minSize > 0) {
        int commonDepth = 0;
        while (commonDepth < minSize && myParents.get(commonDepth) == targetParents.get(commonDepth)) {
          commonDepth ++;
        }

        int stepsUp = 0;
        for (int i = commonDepth; i < myParents.size(); i++) {
          stepsUp += (myParents.get(i).inList()) ? 3 : 2;
        }

        return stepsUp + "#" + target.jsonPointerFrom(myParents.get(commonDepth-1));
      } else {
        return (myParents.size() == 0) ? target.jsonPointerFrom(this) : myParents.size() + "#";
      }
  }

  syn boolean ASTNode.inList() = (getParent() == null) ? false : inListInh();

  inh boolean ASTNode.inListInh();
  eq Root.getA().inListInh() = true;
  eq Root.getB().inListInh() = true;
  eq Root.getC().inListInh() = false;
  eq C.getD1().inListInh() = false;
  eq C.getD2().inListInh() = false;
  eq C.getD3().inListInh() = true;


  syn ASTNode ASTNode.goUp(int steps) = steps > 0 ? goUpInh(steps) : this;

  inh ASTNode ASTNode.goUpInh(int steps);
  eq Root.getA().goUpInh(int steps) = goUp(steps-3);
  eq Root.getB().goUpInh(int steps) = goUp(steps-3);
  eq Root.getC().goUpInh(int steps) = goUp(steps-2);
  eq C.getD1().goUpInh(int steps) = goUp(steps-2);
  eq C.getD2().goUpInh(int steps) = goUp(steps-2);
  eq C.getD3().goUpInh(int steps) = goUp(steps-3);

  /**
   * return the parent from root to the current node
   */
  syn java.util.List<ASTNode> ASTNode.parents() {
    java.util.List<ASTNode> result = new java.util.ArrayList<>();
    if (getParent() != null) {
      result.addAll(getParent().parents());
      if (!(this instanceof List) && !(this instanceof Opt)) result.add(this);
    } else {
      // add the root
      result.add(this);
    }
    return result;
  }

  syn String ASTNode.jsonPointerFrom(ASTNode p) = (getParent() == null || this == p) ? "" : jsonPointerInh(p);

  inh String ASTNode.jsonPointerInh(ASTNode p);
  eq Root.A(int index).jsonPointerInh(ASTNode p) = this.jsonPointerFrom(p) + "/children/A/" + index;
  eq Root.B(int index).jsonPointerInh(ASTNode p) = this.jsonPointerFrom(p) + "/children/B/" + index;
  eq Root.C().jsonPointerInh(ASTNode p)          = this.jsonPointerFrom(p) + "/children/C";
  eq C.D1().jsonPointerInh(ASTNode p)            = this.jsonPointerFrom(p) + "/children/D1";
  eq C.D2().jsonPointerInh(ASTNode p)            = this.jsonPointerFrom(p) + "/children/D2";
  eq C.D3(int index).jsonPointerInh(ASTNode p)   = this.jsonPointerFrom(p) + "/children/D3/" + index;

  // context-independent name resolution
  refine RefResolverStubs eq ASTNode.globallyResolveRootByToken(String id) = (Root) resolveJsonPointer(id);
  refine RefResolverStubs eq ASTNode.globallyResolveAByToken(String id) = (A) resolveJsonPointer(id);
  refine RefResolverStubs eq ASTNode.globallyResolveBByToken(String id) = (B) resolveJsonPointer(id);
  refine RefResolverStubs eq ASTNode.globallyResolveCByToken(String id) = (C) resolveJsonPointer(id);
  refine RefResolverStubs eq ASTNode.globallyResolveDByToken(String id) = (D) resolveJsonPointer(id);
  refine RefResolverStubs eq ASTNode.globallyResolveNamedElementByToken(String id) = (NamedElement) resolveJsonPointer(id);

  syn ASTNode ASTNode.resolveJsonPointer(String pointer) {
    if (pointer.isEmpty() || pointer.startsWith("/")) {
      return root().resolveJsonPointer(pointer.split("/"),1);
    } else {
      // a lot of assumptions here...
      int depth = Integer.valueOf(pointer.split("#")[0]);
      ASTNode result = goUp(depth);
      return result.resolveJsonPointer(pointer.split("/"),1);
    }
  }

  ASTNode ASTNode.root() {
    if (getParent() == null) return this;
    else return getParent().root();
  }

  syn ASTNode ASTNode.resolveJsonPointer(String[] pointer, int index) {
    if (index < pointer.length) {
      throw new RuntimeException("found wrong child  " + pointer[index + 1] + " in class " + this.getClass().getSimpleName());
    } else {
      throw new RuntimeException("Cannot resolve JSON pointer for class " + this.getClass().getSimpleName());
    }
  }
  eq Root.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        case "A":
          return getA(Integer.valueOf(pointer[index + 2])).resolveJsonPointer(pointer, index + 3);
        case "B":
          return getB(Integer.valueOf(pointer[index + 2])).resolveJsonPointer(pointer, index + 3);
        case "C":
          return getC().resolveJsonPointer(pointer, index + 2);
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
  eq A.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
  eq B.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
  eq C.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        case "D1":
          return getD1().resolveJsonPointer(pointer, index + 2);
        case "D2":
          return getD2().resolveJsonPointer(pointer, index + 2);
        case "D3":
          return getD3(Integer.valueOf(pointer[index + 2])).resolveJsonPointer(pointer, index + 3);
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
  eq D.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
  eq NamedElement.resolveJsonPointer(String[] pointer, int index) {
    if (pointer.length == 0 || pointer.length == index) {
      return this;
    } else if (pointer.length == index + 1) {
      throw new RuntimeException("there is only one child called " + pointer[index]);
    } else {
      switch (pointer[index + 1]) {
        default:
          return super.resolveJsonPointer(pointer, index);
      }
    }
  }
}