Package: AbstractParserParticipant

AbstractParserParticipant

nameinstructionbranchcomplexitylinemethod
AbstractParserParticipant()
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%
addError(IFile, String, int)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
addWarning(IFile, String, int)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
findStreamReaderType(IFile)
M: 27 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
parse(IVdmSourceUnit)
M: 36 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
parse(IVdmSourceUnit, String, boolean)
M: 134 C: 0
0%
M: 26 C: 0
0%
M: 14 C: 0
0%
M: 32 C: 0
0%
M: 1 C: 0
0%
processInternalError(Throwable)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setFileMarkers(IFile, AbstractParserParticipant.ParseResult)
M: 82 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
setNatureId(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setProject(IVdmProject)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.core
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.core.parser;
23:
24: import java.io.IOException;
25: import java.util.ArrayList;
26: import java.util.List;
27:
28: import org.eclipse.core.resources.IFile;
29: import org.eclipse.core.resources.IMarker;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.runtime.CoreException;
32: import org.overture.ast.lex.LexLocation;
33: import org.overture.ast.node.INode;
34: import org.overture.ide.core.ICoreConstants;
35: import org.overture.ide.core.VdmCore;
36: import org.overture.ide.core.resources.IVdmProject;
37: import org.overture.ide.core.resources.IVdmSourceUnit;
38: import org.overture.ide.core.utility.FileUtility;
39: import org.overture.parser.lex.BacktrackInputReader.ReaderType;
40: import org.overture.parser.messages.VDMError;
41: import org.overture.parser.messages.VDMWarning;
42:
43: public abstract class AbstractParserParticipant implements ISourceParser
44: {
45:
46:         protected String natureId;
47:         protected IVdmProject project;
48:
49:         public void setNatureId(String natureId)
50:         {
51:                 this.natureId = natureId;
52:         }
53:
54:         public void setProject(IVdmProject project)
55:         {
56:                 this.project = project;
57:         }
58:
59:         /**
60:          * Parses a file and updates file markers and the AstManager with the result
61:          */
62:         public void parse(IVdmSourceUnit file)
63:         {
64:
65:                 ParseResult result;
66:                 try
67:                 {
68:                         LexLocation.resetLocations();
69:                         result = startParse(file, FileUtility.makeString(FileUtility.getContent(file.getFile())), file.getFile().getCharset());
70:                         setFileMarkers(file.getFile(), result);
71:•                        if (result != null && result.getAst() != null)
72:                         {
73:                                 file.reconcile(result.getAst(), result.hasParseErrors());
74:                         }
75:
76:                 } catch (CoreException e)
77:                 {
78:•                        if (VdmCore.DEBUG)
79:                         {
80:                                 VdmCore.log("AbstractParserParticipant:parse IVdmSourceUnit", e);
81:                         }
82:                 }
83:         }
84:
85:         /**
86:          * Parses a file and updates file markers and the AstManager with the result
87:          */
88:         public void parse(IVdmSourceUnit file, String data, boolean forceAstUpdate)
89:         {
90:
91:                 ParseResult result;
92:                 try
93:                 {
94:•                        if (file.getFile().isLinked()
95:•                                        && !file.getFile().getLocation().toFile().exists())
96:                         {
97:                                 FileUtility.deleteMarker(file.getFile(), IMarker.PROBLEM, ICoreConstants.PLUGIN_ID);
98:                                 addError(file.getFile(), "Linked file not found "
99:                                                 + file.getFile(), 0);
100:•                                if (forceAstUpdate)
101:                                 {
102:                                         file.reconcile(null, true);
103:                                 }
104:                         } else
105:                         {
106:                                 LexLocation.resetLocations();
107:                                 result = startParse(file, data, file.getFile().getCharset());
108:                                 setFileMarkers(file.getFile(), result);
109:•                                if (result != null && result.getAst() != null)
110:                                 {
111:•                                        if (forceAstUpdate)
112:                                         {
113:                                                 file.reconcile(result.getAst(), result.hasParseErrors());
114:                                         }
115:                                 }
116:                         }
117:
118:                 } catch (CoreException e)
119:                 {
120:•                        if (e.getStatus().getException() instanceof IOException)
121:                         {
122:                                 FileUtility.deleteMarker(file.getFile(), IMarker.PROBLEM, ICoreConstants.PLUGIN_ID);
123:•                                if (e.getStatus() != null
124:•                                                && e.getStatus().getException() != null)
125:                                 {
126:                                         addError(file.getFile(), e.getStatus().getException().getMessage(), 0);
127:                                 }
128:
129:•                        } else if (VdmCore.DEBUG)
130:                         {
131:                                 VdmCore.log("AbstractParserParticipant:parse IVdmSourceUnit", e);
132:                         }
133:
134:•                        if (forceAstUpdate)
135:                         {
136:                                 file.reconcile(null, true);
137:                         }
138:                 } catch (Exception e)
139:                 {
140:•                        if (VdmCore.DEBUG)
141:                         {
142:                                 VdmCore.log("AbstractParserParticipant:parse IVdmSourceUnit", e);
143:                         }
144:                         FileUtility.deleteMarker(file.getFile(), IMarker.PROBLEM, ICoreConstants.PLUGIN_ID);
145:                         addError(file.getFile(), "Internal error: " + e.getMessage(), 0);
146:•                        if (forceAstUpdate)
147:                         {
148:                                 file.reconcile(null, true);
149:                         }
150:                 }
151:
152:         }
153:
154:         /**
155:          * Adds markers to a file based on the result. if errors occurred the problem markers are first cleared.
156:          *
157:          * @param file
158:          * the file where the markers should be set
159:          * @param result
160:          * the result indicating if parse errors occurred
161:          * @param content
162:          * @throws CoreException
163:          */
164:         private void setFileMarkers(IFile file, ParseResult result)
165:                         throws CoreException
166:         {
167:•                if (file != null)
168:                 {
169:                         FileUtility.deleteMarker(file, IMarker.PROBLEM, ICoreConstants.PLUGIN_ID);
170:•                        if (result.hasParseErrors())
171:                         {
172:                                 file.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
173:                                 int previousErrorNumber = -1;
174:•                                for (VDMError error : result.getErrors())
175:                                 {
176:•                                        if (previousErrorNumber == error.number)
177:                                         {// this check is
178:                                                 // done to avoid
179:                                                 // error fall
180:                                                 // through
181:                                                 continue;
182:                                         } else
183:                                         {
184:                                                 previousErrorNumber = error.number;
185:                                         }
186:
187:                                         FileUtility.addMarker(file, error.toProblemString(), error.location, IMarker.SEVERITY_ERROR, ICoreConstants.PLUGIN_ID, -1);
188:                                 }
189:                         }
190:
191:                         IVdmProject vdmProject = (IVdmProject) project.getAdapter(IVdmProject.class);
192:
193:•                        if (result.getWarnings().size() > 0 && vdmProject != null
194:•                                        && !vdmProject.hasSuppressWarnings())
195:                         {
196:•                                for (VDMWarning warning : result.getWarnings())
197:                                 {
198:                                         FileUtility.addMarker(file, warning.toProblemString(), warning.location, IMarker.SEVERITY_WARNING, ICoreConstants.PLUGIN_ID, -1);
199:                                 }
200:                         }
201:                 }
202:
203:         }
204:
205:         /**
206:          * Delegate method for parsing, the methods addWarning and addError should be used to report
207:          *
208:          * @param file
209:          * the file to be parsed
210:          * @param content
211:          * the content of the file
212:          * @param charset
213:          * the charset of the content
214:          * @return the result of the parse including error report and the AST
215:          */
216:         protected abstract ParseResult startParse(IVdmSourceUnit file,
217:                         String content, String charset);
218:
219:         protected void addWarning(IFile file, String message, int lineNumber)
220:         {
221:                 FileUtility.addMarker(file, message, lineNumber, IMarker.SEVERITY_WARNING, ICoreConstants.PLUGIN_ID);
222:         }
223:
224:         protected void addError(IFile file, String message, int lineNumber)
225:         {
226:                 FileUtility.addMarker(file, message, lineNumber, IMarker.SEVERITY_ERROR, ICoreConstants.PLUGIN_ID);
227:         }
228:
229:         protected void processInternalError(Throwable e)
230:         {
231:                 System.out.println(e.toString());
232:         };
233:
234:         public static ReaderType findStreamReaderType(IFile file)
235:                         throws CoreException
236:         {
237:                 ReaderType streamReaderType = ReaderType.Latex;
238:
239:•                if (file.getFileExtension().endsWith("doc"))
240:                 {
241:                         streamReaderType = ReaderType.Doc;
242:•                } else if (file.getFileExtension().endsWith("docx"))
243:                 {
244:                         streamReaderType = ReaderType.Docx;
245:•                } else if (file.getFileExtension().endsWith("odt"))
246:                 {
247:                         streamReaderType = ReaderType.Odf;
248:                 }
249:                 return streamReaderType;
250:         }
251:
252:         public static class ParseResult
253:         {
254:                 private List<INode> ast = null;
255:                 private List<VDMError> errors = new ArrayList<VDMError>();
256:                 private List<VDMWarning> warnings = new ArrayList<VDMWarning>();
257:                 private Throwable fatalError;
258:
259:                 public ParseResult()
260:                 {
261:
262:                 }
263:
264:                 public boolean hasParseErrors()
265:                 {
266:                         return errors.size() != 0 || fatalError != null;
267:                 }
268:
269:                 public void setAst(List<INode> ast)
270:                 {
271:                         // Assert.isNotNull(ast, "AST cannot be null");
272:                         // Assert.isTrue(ast.size() != 0, "AST cannot be an empty list");
273:                         this.ast = ast;
274:                 }
275:
276:                 public List<INode> getAst()
277:                 {
278:                         return ast;
279:                 }
280:
281:                 public void setWarnings(List<VDMWarning> warnings)
282:                 {
283:                         this.warnings.addAll(warnings);
284:                 }
285:
286:                 public List<VDMWarning> getWarnings()
287:                 {
288:                         return warnings;
289:                 }
290:
291:                 public void setErrors(List<VDMError> errors)
292:                 {
293:                         this.errors.addAll(errors);
294:                 }
295:
296:                 public List<VDMError> getErrors()
297:                 {
298:                         return errors;
299:                 }
300:
301:                 public void setFatalError(Throwable fatalError)
302:                 {
303:                         if (VdmCore.DEBUG)
304:                         {
305:                                 // fatalError.printStackTrace();
306:                         }
307:                         this.fatalError = fatalError;
308:                 }
309:
310:                 public Throwable getFatalError()
311:                 {
312:                         return fatalError;
313:                 }
314:
315:         }
316: }