/
AbstractAST.java
210 lines (171 loc) · 5.8 KB
/
AbstractAST.java
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
/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Tijs van der Storm - Tijs.van.der.Storm@cwi.nl
* * Paul Klint - Paul.Klint@cwi.nl - CWI
* * Mark Hills - Mark.Hills@cwi.nl (CWI)
* * Arnold Lankamp - Arnold.Lankamp@cwi.nl
* * Michael Steindorfer - Michael.Steindorfer@cwi.nl - CWI
*******************************************************************************/
package org.rascalmpl.ast;
import java.util.Map;
import org.eclipse.imp.pdb.facts.IConstructor;
import org.eclipse.imp.pdb.facts.ISourceLocation;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.IValueFactory;
import org.eclipse.imp.pdb.facts.type.Type;
import org.eclipse.imp.pdb.facts.type.TypeFactory;
import org.rascalmpl.interpreter.AssignableEvaluator;
import org.rascalmpl.interpreter.IEvaluator;
import org.rascalmpl.interpreter.IEvaluatorContext;
import org.rascalmpl.interpreter.asserts.NotYetImplemented;
import org.rascalmpl.interpreter.env.Environment;
import org.rascalmpl.interpreter.matching.IBooleanResult;
import org.rascalmpl.interpreter.matching.IMatchingResult;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.result.ResultFactory;
import org.rascalmpl.interpreter.staticErrors.UnsupportedPattern;
import org.rascalmpl.interpreter.types.RascalTypeFactory;
import org.rascalmpl.values.ValueFactoryFactory;
public abstract class AbstractAST implements IVisitable {
protected ISourceLocation src;
protected Map<String, IValue> annotations;
protected ASTStatistics stats = new ASTStatistics();
protected Type _type = null;
protected final TypeFactory TF = TypeFactory.getInstance();
protected final RascalTypeFactory RTF = RascalTypeFactory.getInstance();
protected final IValueFactory VF = ValueFactoryFactory.getValueFactory();
protected IMatchingResult matcher;
AbstractAST() {
}
AbstractAST(IConstructor node) {
}
public Type _getType() {
return _type;
}
public AbstractAST findNode(int offset) {
if (src.getOffset() <= offset
&& offset < src.getOffset() + src.getLength()) {
return this;
}
return null;
}
public void setSourceLocation(ISourceLocation src) {
this.src = src;
}
public void setAnnotations(Map<String, IValue> annotations) {
this.annotations = annotations;
}
public Map<String, IValue> getAnnotations() {
return annotations;
}
public static <T extends IValue> Result<T> makeResult(Type declaredType, IValue value, IEvaluatorContext ctx) {
return ResultFactory.makeResult(declaredType, value, ctx);
}
public static Result<IValue> nothing() {
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
public void _setType(Type nonterminalType) {
if (_type != null && (! _type.equals(nonterminalType))) {
// For debugging purposes
System.err.println("In _setType, found two unequal types: " + _type.toString() + " and " + nonterminalType.toString());
}
this._type = nonterminalType;
}
public <T> T accept(IASTVisitor<T> v) {
return null;
}
public ISourceLocation getLocation() {
return src;
}
public ASTStatistics getStats() {
return stats;
}
public void setStats(ASTStatistics stats) {
this.stats = stats;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if (getClass() == obj.getClass()) {
if (obj == this) {
return true;
}
AbstractAST other = (AbstractAST) obj;
if (other.src == src) {
return true;
}
return other.src.isEqual(src);
}
return false;
}
@Deprecated
public IConstructor getTree() {
throw new NotYetImplemented(this);
}
@Override
public int hashCode() {
return src.hashCode();
}
@Override
@Deprecated
/**
* @deprecated because this does not print the actual source code of the AST anymore! Use getString() instead on lexicals.
* For debugging purposes
*/
public String toString() {
return src + ":" + getClass();
}
public Result<IValue> interpret(IEvaluator<Result<IValue>> eval) {
throw new NotYetImplemented(this);
}
public Result<IValue> assignment(AssignableEvaluator eval) {
throw new NotYetImplemented(this);
}
public String declareSyntax(IEvaluator<Result<IValue>> eval, boolean withImports) {
throw new NotYetImplemented(this);
}
/**
* Computes internal type representations for type literals and patterns.
*/
public Type typeOf(Environment env) {
throw new NotYetImplemented(this);
}
public Type __evaluate(org.rascalmpl.interpreter.BasicTypeEvaluator eval) {
throw new NotYetImplemented(this);
}
/**
* Recursively build a matching data-structure, use getMatcher if you are just a client of IMatchingResult.
*/
public IMatchingResult buildMatcher(IEvaluatorContext eval) {
throw new UnsupportedPattern(toString(), this);
}
public IMatchingResult getMatcher(IEvaluatorContext eval) {
return buildMatcher(eval);
}
/**
* Recursively build a back-tracking data-structure, use getBacktracker if you are just a client of IBooleanResult
*/
public IBooleanResult buildBacktracker(IEvaluatorContext eval) {
throw new NotYetImplemented(this);
}
public IBooleanResult getBacktracker(IEvaluatorContext ctx) {
return buildBacktracker(ctx);
}
/**
* If the debugger can suspend (i.e. break) before interpretation.
* @return <code>true</code> if suspension is supported, otherwise <code>false</code>
*/
public boolean isBreakable() {
return annotations != null
&& annotations.containsKey("breakable")
&& annotations.get("breakable").equals(VF.bool(true));
}
}