1 /********************************************************************************
2 *
3 * Copyright (c) 2008 Fujitsu Services Ltd.
4 *
5 * Author: Nick Battle
6 *
7 * This file is part of VDMJ.
8 *
9 * VDMJ is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * VDMJ is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21 *
22 ******************************************************************************/
23
24 package org.overturetool.vdmj;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.ObjectInputStream;
29 import java.util.List;
30 import java.util.zip.GZIPInputStream;
31
32 import org.overturetool.vdmj.definitions.BUSClassDefinition;
33 import org.overturetool.vdmj.definitions.CPUClassDefinition;
34 import org.overturetool.vdmj.definitions.ClassList;
35 import org.overturetool.vdmj.lex.Dialect;
36 import org.overturetool.vdmj.lex.LexLocation;
37 import org.overturetool.vdmj.messages.InternalException;
38 import org.overturetool.vdmj.syntax.OvertureReader;
39
40 /***
41 * The main class of the VICE Overture parser/checker/interpreter.
42 */
43
44 public class VDMOV extends VDMPP
45 {
46 public VDMOV()
47 {
48 Settings.dialect = Dialect.VDM_RT;
49 }
50
51 /***
52 * @see org.overturetool.vdmj.VDMJ#parse(java.util.List)
53 */
54
55 @Override
56 public ExitStatus parse(List<File> files)
57 {
58 classes.clear();
59 LexLocation.resetLocations();
60 long convert = 0;
61 int perrs = 0;
62 long duration = 0;
63
64 for (File file: files)
65 {
66 try
67 {
68 if (file.getName().endsWith(".lib"))
69 {
70 FileInputStream fis = new FileInputStream(file);
71 GZIPInputStream gis = new GZIPInputStream(fis);
72 ObjectInputStream ois = new ObjectInputStream(gis);
73
74 ClassList loaded = null;
75 long begin = System.currentTimeMillis();
76
77 try
78 {
79 loaded = (ClassList)ois.readObject();
80 }
81 catch (Exception e)
82 {
83 println(file + " is not a valid VDM++ library");
84 perrs++;
85 continue;
86 }
87 finally
88 {
89 ois.close();
90 }
91
92
93 long end = System.currentTimeMillis();
94 loaded.setLoaded();
95 classes.addAll(loaded);
96 classes.remap();
97
98 infoln("Loaded " + plural(loaded.size(), "class", "es") +
99 " from " + file + " in " + (double)(end-begin)/1000 + " secs");
100 }
101 else
102 {
103 long before = System.currentTimeMillis();
104 OvertureReader reader = new OvertureReader(file, filecharset);
105 long beforeConvert = System.currentTimeMillis();
106 classes.addAll(reader.readClasses());
107 convert += System.currentTimeMillis() - beforeConvert;
108 perrs += reader.getErrorCount();
109 long after = System.currentTimeMillis();
110 duration += (after - before);
111 }
112 }
113 catch (InternalException e)
114 {
115 println(e.toString());
116 perrs++;
117 }
118 catch (Throwable e)
119 {
120 println(e.toString());
121 perrs++;
122 }
123 }
124
125 int n = classes.notLoaded();
126
127 if (n > 0)
128 {
129 info("Overture parsed " + plural(n, "class", "es") + " in " +
130 (double)(duration)/1000 + " secs (" +
131 (double)convert/1000 + " secs AST convert). ");
132 infoln(perrs == 0 ? "No syntax errors" :
133 "Found " + plural(perrs, "syntax error", "s"));
134 }
135
136 return perrs == 0 ? ExitStatus.EXIT_OK : ExitStatus.EXIT_ERRORS;
137 }
138
139 @Override
140 public ExitStatus typeCheck()
141 {
142 try
143 {
144 classes.add(new CPUClassDefinition());
145 classes.add(new BUSClassDefinition());
146 }
147 catch (Exception e)
148 {
149 throw new InternalException(11, "CPU or BUS creation failure");
150 }
151
152 return super.typeCheck();
153 }
154 }