forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASTNode.java
More file actions
318 lines (272 loc) · 8.52 KB
/
ASTNode.java
File metadata and controls
318 lines (272 loc) · 8.52 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
package org.kframework.kil;
import com.google.common.reflect.TypeToken;
import com.google.inject.name.Names;
import org.kframework.attributes.HasLocation;
import org.kframework.attributes.Location;
import org.kframework.attributes.Source;
import org.kframework.kil.Attribute.Key;
import org.kframework.kil.loader.Constants;
import org.w3c.dom.Element;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Optional;
import java.util.Scanner;
/**
* Base class for K AST. Useful for Visitors and Transformers.
*/
public abstract class ASTNode implements Serializable, HasLocation {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Used on any node for metadata also used on {@link Rule} and {@link Production} for the attribute list.
*/
private Attributes attributes;
private Source source;
private Location location;
/**
* Initializes an ASTNode from XML describing the parse tree
*
* @param elem
* The XML element describing the ASTNode
*/
public ASTNode(Element elem) {
this(getElementLocation(elem), getElementSource(elem));
}
/**
* Retrieves the location from an XML element
*
* @param elem
* @return the location stored in XML or Constants.GENERATED_LOCATION if no location found.
*/
public static Location getElementLocation(Element elem) {
if (elem != null && elem.hasAttribute(Constants.LOC_loc_ATTR)) {
Scanner scanner = new Scanner(elem.getAttribute(Constants.LOC_loc_ATTR)).useDelimiter("[,)]").skip("\\(");
int beginLine = scanner.nextInt();
int beginCol = scanner.nextInt();
int endLine = scanner.nextInt();
int endCol = scanner.nextInt();
return new Location(beginLine, beginCol, endLine, endCol);
}
else
return null;
}
/**
* Retrieves the file name from an XML element
*
* @param elem
* @return the file name stored in XML or Constants.GENERATED_FILENAME if no filename found.
*/
public static Source getElementSource(Element elem) {
return (Source) elem.getUserData(Constants.SOURCE_ATTR);
}
/**
* Copy constructor
*
* @param astNode
*/
public ASTNode(ASTNode astNode) {
copyAttributesFrom(astNode);
location = astNode.location;
source = astNode.source;
}
/**
* Default constructor (generated at runtime)
*/
public ASTNode() {
this(null, null);
}
/**
* Constructor with specified location and filename.
*
* @param loc
* @param file
*/
public ASTNode(Location loc, Source source) {
setLocation(loc);
setSource(source);
}
/**
* Retrieves the location of the current ASTNode.
*
* @return recorded location or null if no recorded location found.
*/
public Location getLocation() {
return location;
}
/**
* Sets the location or removes it if appropriate.
*
* @param loc
*/
public void setLocation(Location location) {
this.location = location;
}
/**
* Retrieves the source of the current ASTNode.
*
* @return recorded source or null if no recorded source found.
*/
public Source getSource() {
return source;
}
/**
* Sets the source or removes it if appropriate.
*
* @param file
*/
public void setSource(Source source) {
this.source = source;
}
public Optional<Location> location() { return Optional.ofNullable(location); }
public Optional<Source> source() { return Optional.ofNullable(source); }
/*
* methods for easy attributes manipulation
*/
/**
* Appends an attribute to the list of attributes.
*
* @param key
* @param val
*/
public void addAttribute(String key, String val) {
addAttribute(Attribute.of(key, val));
}
public <T> void addAttribute(Key<T> key, T val) {
addAttribute(new Attribute<>(key, val));
}
public <T> void addAttribute(Class<T> key, T val) {
addAttribute(Key.get(key), val);
}
public <T> void addAttribute(TypeToken<T> key, T val) {
addAttribute(Key.get(key), val);
}
public <T> void addAttribute(TypeToken<T> key, Annotation annotation, T val) {
addAttribute(Key.get(key, annotation), val);
}
public <T> void addAttribute(TypeToken<T> key, String annotation, T val) {
addAttribute(Key.get(key, Names.named(annotation)), val);
}
public <T> void addAttribute(Class<T> key, Annotation annotation, T val) {
addAttribute(Key.get(key, annotation), val);
}
public <T> void addAttribute(Class<T> key, String annotation, T val) {
addAttribute(Key.get(key, Names.named(annotation)), val);
}
/**
* Appends an attribute to the list of attributes.
*
* @param attr
*/
public void addAttribute(Attribute<?> attr) {
if (attributes == null)
attributes = new Attributes();
attributes.add(attr);
}
/**
* @param key
* @return whether the attribute key occurs in the list of attributes.
*/
public boolean containsAttribute(String key) {
if (attributes == null)
return false;
return attributes.containsKey(Attribute.keyOf(key));
}
/**
* @param key
* @return whether the attribute key occurs in the list of attributes.
*/
public boolean containsAttribute(Key<?> key) {
if (attributes == null)
return false;
return attributes.containsKey(key);
}
/**
* Retrieves the attribute by key from the list of attributes
*
* @param key
* @return a value for key in the list of attributes or the default value.
*/
public String getAttribute(String key) {
return getAttribute(Attribute.keyOf(key));
}
/**
* Retrieves the attribute by key from the list of attributes
*
* @param key
* @return a value for key in the list of attributes or the default value.
*/
public <T> T getAttribute(Key<T> key) {
if (attributes == null)
return null;
final Attribute<T> value = (Attribute<T>) attributes.get(key);
if (value == null)
return null;
return value.getValue();
}
public <T> T getAttribute(Class<T> cls) {
return getAttribute(Key.get(cls));
}
public <T> T getAttribute(Class<T> cls, Annotation annotation) {
return getAttribute(Key.get(cls, annotation));
}
public <T> T getAttribute(Class<T> cls, String annotation) {
return getAttribute(Key.get(cls, Names.named(annotation)));
}
public <T> T getAttribute(TypeToken<T> type) {
return getAttribute(Key.get(type));
}
public <T> T getAttribute(TypeToken<T> type, Annotation annotation) {
return getAttribute(Key.get(type, annotation));
}
public <T> T getAttribute(TypeToken<T> type, String annotation) {
return getAttribute(Key.get(type, Names.named(annotation)));
}
/**
* Updates the value of an attribute in the list of attributes.
*get
* @param key
* @param val
*/
public void putAttribute(String key, String val) {
addAttribute(key, val);
}
public void removeAttribute(String key) {
getAttributes().remove(Attribute.keyOf(key));
}
/**
* @return the attributes object associated to this ASTNode. Constructs one if it is
* not already created.
*/
public Attributes getAttributes() {
if (attributes == null) {
attributes = new Attributes();
}
return attributes;
}
/**
* Copies attributes from another node into this node.
* Use this in preference to {@link ASTNode#getAttributes} where appropriate because
* the latter will create a new object if no attributes exist.
* @param node The ASTNode to copy all attributes from.
*/
public void copyAttributesFrom(ASTNode node) {
if (node.attributes == null)
return;
this.getAttributes().putAll(node.attributes);
}
/**
* Sets the attributes object associated to this ASTNode.
*
* @param attrs
*/
public void setAttributes(Attributes attrs) {
attributes = attrs;
}
/**
* @return a copy of the ASTNode containing the same fields.
*/
public abstract ASTNode shallowCopy();
}