Package: TypeCheckerUtil$TypeCheckResult

TypeCheckerUtil$TypeCheckResult

nameinstructionbranchcomplexitylinemethod
TypeCheckerUtil.TypeCheckResult(ParserUtil.ParserResult, Object, List, List)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getErrorString()
M: 16 C: 14
47%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 3
60%
M: 0 C: 1
100%
getWarningString()
M: 16 C: 14
47%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 3
60%
M: 0 C: 1
100%
toString()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM Type Checker
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.typechecker.util;
23:
24: import java.io.File;
25: import java.util.List;
26: import java.util.Vector;
27:
28: import org.overture.ast.analysis.AnalysisException;
29: import org.overture.ast.definitions.PDefinition;
30: import org.overture.ast.definitions.SClassDefinition;
31: import org.overture.ast.expressions.PExp;
32: import org.overture.ast.factory.AstFactoryTC;
33: import org.overture.ast.modules.AModuleModules;
34: import org.overture.ast.typechecker.NameScope;
35: import org.overture.ast.types.PType;
36: import org.overture.ast.util.modules.ModuleList;
37: import org.overture.parser.lex.LexException;
38: import org.overture.parser.messages.VDMError;
39: import org.overture.parser.messages.VDMWarning;
40: import org.overture.parser.syntax.ParserException;
41: import org.overture.parser.util.ParserUtil;
42: import org.overture.parser.util.ParserUtil.ParserResult;
43: import org.overture.typechecker.ClassTypeChecker;
44: import org.overture.typechecker.Environment;
45: import org.overture.typechecker.FlatEnvironment;
46: import org.overture.typechecker.ModuleTypeChecker;
47: import org.overture.typechecker.TypeCheckInfo;
48: import org.overture.typechecker.TypeChecker;
49: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
50: import org.overture.typechecker.assistant.TypeCheckerAssistantFactory;
51: import org.overture.typechecker.visitor.TypeCheckVisitor;
52:
53: public class TypeCheckerUtil
54: {
55:         public static class TypeCheckResult<T>
56:         {
57:                 public final ParserResult<T> parserResult;
58:                 public final List<VDMWarning> warnings;
59:                 public final List<VDMError> errors;
60:                 public final T result;
61:
62:                 public TypeCheckResult(ParserResult<T> parserResult, T result,
63:                                 List<VDMWarning> warnings, List<VDMError> errors)
64:                 {
65:                         this.parserResult = parserResult;
66:                         this.result = result;
67:                         this.warnings = warnings;
68:                         this.errors = errors;
69:                 }
70:
71:                 @Override
72:                 public String toString()
73:                 {
74:                         StringBuilder sb = new StringBuilder();
75:                         sb.append("Parse result:\n" + parserResult);
76:                         sb.append("\n\n\n");
77:                         sb.append("TypeCheck result:\n");
78:                         sb.append("\tErrors:");
79:                         sb.append(getErrorString());
80:                         sb.append("\tWarnings:");
81:                         sb.append(getWarningString());
82:                         return sb.toString();
83:                 }
84:
85:                 public String getErrorString()
86:                 {
87:                         StringBuilder sb = new StringBuilder();
88:
89:•                        for (VDMError err : errors)
90:                         {
91:                                 sb.append("\n\t" + err);
92:                         }
93:
94:                         return sb.toString();
95:                 }
96:
97:                 public String getWarningString()
98:                 {
99:                         StringBuilder sb = new StringBuilder();
100:
101:•                        for (VDMWarning err : warnings)
102:                         {
103:                                 sb.append("\n\t" + err);
104:                         }
105:                         return sb.toString();
106:                 }
107:         }
108:
109:         public static class ExpressionTypeChecker extends TypeChecker
110:         {
111:                 PExp expression;
112:                 Environment env;
113:                 public PType type;
114:
115:                 public ExpressionTypeChecker(PExp expression, Environment env)
116:                 {
117:                         this.expression = expression;
118:                         this.env = env;
119:                 }
120:
121:                 public ExpressionTypeChecker(PExp expression)
122:                 {
123:                         this(expression, new FlatEnvironment(new TypeCheckerAssistantFactory(), new Vector<PDefinition>()));// new
124:                                                                                                                                                                                                                                 // ModuleEnvironment(""));
125:                 }
126:
127:                 @Override
128:                 public void typeCheck()
129:                 {
130:                         try
131:                         {
132:                                 type = expression.apply(new TypeCheckVisitor(), new TypeCheckInfo(assistantFactory, env, NameScope.NAMESANDSTATE));
133:                         } catch (AnalysisException e)
134:                         {
135:                                 // TODO Auto-generated catch block
136:                                 e.printStackTrace();
137:                         }
138:                 }
139:
140:         }
141:
142:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(File file)
143:         {
144:                 return typeCheckSl(file, null);
145:         }
146:
147:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(File file,
148:                         String charset)
149:         {
150:                 ParserResult<List<AModuleModules>> parserResult = ParserUtil.parseSl(file, charset);
151:
152:                 ModuleList modules = new ModuleList(parserResult.result);
153:
154:                 return typeCheck(parserResult, parserResult.result, new ModuleTypeChecker(modules));
155:         }
156:
157:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(
158:                         List<File> file)
159:         {
160:                 return typeCheckSl(file, null);
161:         }
162:
163:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(
164:                         List<File> file, String charset)
165:         {
166:                 ParserResult<List<AModuleModules>> parserResult = ParserUtil.parseSl(file, charset);
167:
168:                 ModuleList modules = new ModuleList(parserResult.result);
169:                 modules.combineDefaults();
170:                 return typeCheck(parserResult, parserResult.result, new ModuleTypeChecker(modules));
171:         }
172:
173:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(
174:                         String content)
175:         {
176:                 return typeCheckSl(content, null);
177:         }
178:
179:         public static TypeCheckResult<List<AModuleModules>> typeCheckSl(
180:                         String content, String charset)
181:         {
182:                 ParserResult<List<AModuleModules>> parserResult = ParserUtil.parseSl(content, charset);
183:                 return typeCheck(parserResult, parserResult.result, new ModuleTypeChecker(parserResult.result));
184:         }
185:
186:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(File file)
187:         {
188:                 return typeCheckPp(file, null);
189:         }
190:
191:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(
192:                         File file, String charset)
193:         {
194:                 ParserResult<List<SClassDefinition>> parserResult = ParserUtil.parseOo(file, charset);
195:                 return typeCheck(parserResult, parserResult.result, new ClassTypeChecker(parserResult.result));
196:         }
197:
198:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(
199:                         List<File> file)
200:         {
201:                 return typeCheckPp(file, null);
202:         }
203:
204:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(
205:                         List<File> file, String charset)
206:         {
207:                 ParserResult<List<SClassDefinition>> parserResult = ParserUtil.parseOo(file, charset);
208:                 return typeCheck(parserResult, parserResult.result, new ClassTypeChecker(parserResult.result));
209:         }
210:
211:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(
212:                         String content)
213:         {
214:                 return typeCheckPp(content, null);
215:         }
216:
217:         public static TypeCheckResult<List<SClassDefinition>> typeCheckPp(
218:                         String content, String charset)
219:         {
220:                 ParserResult<List<SClassDefinition>> parserResult = ParserUtil.parseOo(content, charset);
221:                 return typeCheck(parserResult, parserResult.result, new ClassTypeChecker(parserResult.result));
222:         }
223:
224:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
225:                         List<File> file) throws ParserException, LexException
226:         {
227:                 return typeCheckRt(file, null);
228:         }
229:
230:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
231:                         List<File> file, String charset) throws ParserException,
232:                         LexException
233:         {
234:                 return typeCheckRt(ParserUtil.parseOo(file, charset));
235:         }
236:
237:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(File file)
238:                         throws ParserException, LexException
239:         {
240:                 return typeCheckRt(file, null);
241:         }
242:
243:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
244:                         File file, String charset) throws ParserException, LexException
245:         {
246:                 return typeCheckRt(ParserUtil.parseOo(file, charset));
247:         }
248:
249:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
250:                         String content) throws ParserException, LexException
251:         {
252:                 return typeCheckRt(content, null);
253:         }
254:
255:         public static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
256:                         String content, String charset) throws ParserException,
257:                         LexException
258:         {
259:                 return typeCheckRt(ParserUtil.parseOo(content, charset));
260:         }
261:
262:         protected static TypeCheckResult<List<SClassDefinition>> typeCheckRt(
263:                         ParserResult<List<SClassDefinition>> parserResult)
264:                         throws ParserException, LexException
265:         {
266:                 final ITypeCheckerAssistantFactory af = new TypeCheckerAssistantFactory();
267:                 List<SClassDefinition> classes = new Vector<SClassDefinition>();
268:                 classes.addAll(parserResult.result);
269:                 classes.add(AstFactoryTC.newACpuClassDefinition(af));
270:                 classes.add(AstFactoryTC.newABusClassDefinition(af));
271:                 return typeCheck(parserResult, classes, new ClassTypeChecker(classes, af));
272:         }
273:
274:         public static TypeCheckResult<PExp> typeCheckExpression(String content)
275:                         throws ParserException, LexException
276:         {
277:                 ParserResult<PExp> parserResult = ParserUtil.parseExpression(content);
278:                 return typeCheck(parserResult, parserResult.result, new ExpressionTypeChecker(parserResult.result));
279:         }
280:
281:         public static <P> TypeCheckResult<P> typeCheck(
282:                         ParserResult<P> parserResult, P tcList, TypeChecker tc)
283:         {
284:                 if (parserResult.errors.isEmpty())
285:                 {
286:                         TypeChecker.clearErrors();
287:                         tc.typeCheck();
288:                         return new TypeCheckResult<P>(parserResult, parserResult.result, TypeChecker.getWarnings(), TypeChecker.getErrors());
289:                 }
290:                 return new TypeCheckResult<P>(parserResult, null, new Vector<VDMWarning>(), new Vector<VDMError>());
291:         }
292:
293: }