Package: VdmSlRuntimeUtil

VdmSlRuntimeUtil

nameinstructionbranchcomplexitylinemethod
VdmSlRuntimeUtil()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseExpression(String, String, String)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
typeCheck(ModuleList, String)
M: 98 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.vdmsl.debug
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.ide.vdmsl.debug.utils;
23:
24: import org.overture.ast.expressions.PExp;
25: import org.overture.ast.lex.Dialect;
26: import org.overture.ast.modules.AModuleModules;
27: import org.overture.ast.typechecker.NameScope;
28: import org.overture.ast.util.modules.ModuleList;
29: import org.overture.parser.lex.LexException;
30: import org.overture.parser.lex.LexTokenReader;
31: import org.overture.parser.messages.Console;
32: import org.overture.parser.messages.VDMErrorsException;
33: import org.overture.parser.syntax.ExpressionReader;
34: import org.overture.parser.syntax.ParserException;
35: import org.overture.typechecker.Environment;
36: import org.overture.typechecker.ModuleEnvironment;
37: import org.overture.typechecker.TypeCheckInfo;
38: import org.overture.typechecker.TypeChecker;
39: import org.overture.typechecker.assistant.TypeCheckerAssistantFactory;
40: import org.overture.typechecker.visitor.TypeCheckVisitor;
41:
42: public class VdmSlRuntimeUtil
43: {
44:         protected final static String STATIC_CALL_SEPERATOR = "`";
45:
46:         public static boolean typeCheck(ModuleList modules, String expression)
47:                         throws VDMErrorsException, ParserException, LexException
48:         {
49:                 //modules.combineDefaults();
50:                 
51:                 String defaultModuleName = null;
52:•                if (expression.contains(STATIC_CALL_SEPERATOR))
53:                 {
54:                         defaultModuleName = expression.substring(0, expression.indexOf(STATIC_CALL_SEPERATOR)); // needed for static
55:                                                                                                                                                                                                         // fn/op check
56:                 }
57:
58:                 AModuleModules defaultModule = null;
59:
60:•                if (defaultModuleName == null || (modules.isEmpty()))
61:                 {
62:                         defaultModule = new AModuleModules();
63:                 } else
64:                 {
65:•                        for (AModuleModules m : modules)
66:                         {
67:•                                if (m.getName().getName().equals(defaultModuleName))
68:                                 {
69:                                         defaultModule = m;
70:                                 }
71:                         }
72:                 }
73:
74:                 TypeCheckVisitor tc = new TypeCheckVisitor();
75:                 TypeChecker.clearErrors();
76:•                if(defaultModule==null)
77:                 {
78:                         return false;//FIXME throw approiate error
79:                 }
80:                 Environment env = new ModuleEnvironment(new TypeCheckerAssistantFactory(), defaultModule);
81:                 PExp expr;
82:
83:                 expr = parseExpression(expression, defaultModule.getName().getName(), defaultModule.getName().getName());
84:
85:                 try
86:                 {
87:                         expr.apply(tc, new TypeCheckInfo(new TypeCheckerAssistantFactory(),env, NameScope.NAMESANDSTATE));
88:                 } catch (Throwable e)
89:                 {
90:                         throw new VDMErrorsException(TypeChecker.getErrors());//FIXME: this might not has any errors if it goes wrong
91:                 }
92:
93:•                if (TypeChecker.getErrorCount() > 0)
94:                 {
95:                         throw new VDMErrorsException(TypeChecker.getErrors());
96:                         // return false;
97:                 }
98:
99:                 return true;
100:         }
101:
102:         private static PExp parseExpression(String expression, String name,
103:                         String defaultModuleName) throws ParserException, LexException
104:         {
105:                 LexTokenReader ltr = new LexTokenReader(expression, Dialect.VDM_SL, Console.charset);
106:                 ExpressionReader reader = new ExpressionReader(ltr);
107:                 reader.setCurrentModule(defaultModuleName);
108:                 return reader.readExpression();
109:         }
110: }