]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/ErrorLog.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / errors / ErrorLog.java
1 package org.simantics.scl.compiler.errors;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7
8 public class ErrorLog {
9     ArrayList<CompilationError> errors = new ArrayList<CompilationError>();
10     int errorCount;
11     long exceptionPosition = Locations.NO_LOCATION;
12     
13     public void log(String message) {
14         log(new CompilationError(message));
15     }
16     
17     public void log(CompilationError error) {
18         errors.add(error);
19         if(error.severity == ErrorSeverity.ERROR)
20                 ++errorCount;
21     }
22     
23     public void log(long locatable, String description) {
24         log(new CompilationError(locatable, description));
25     }
26     
27     public void logWarning(long locatable, String description) {
28         log(new CompilationError(locatable, description, ErrorSeverity.WARNING));
29     }
30
31     public void log(Exception e) {
32         long location = Locations.NO_LOCATION;
33         if(e instanceof InternalCompilerError)
34             location = ((InternalCompilerError)e).location;
35         if(location == Locations.NO_LOCATION)
36             location = exceptionPosition;
37         log(new CompilationError(location, e));
38     }
39     
40     public void log(long location, Exception e) {
41         log(new CompilationError(location, e));
42     }
43     
44     public boolean hasNoErrors() {
45         return errorCount == 0;
46     }
47     
48     public boolean hasErrors() {
49         return errorCount > 0;
50     }
51     
52     public boolean hasErrorsOrWarnings() {
53         return !errors.isEmpty();
54     }
55
56     public CompilationError[] getErrors() {
57         Collections.sort(errors);
58         return errors.toArray(new CompilationError[errors.size()]);
59     }
60
61     public void setExceptionPosition(long exceptionPosition) {
62         if(this.exceptionPosition == Locations.NO_LOCATION)
63             this.exceptionPosition = exceptionPosition;
64     }
65
66     public String getErrorsAsString() {
67         Collections.sort(errors);
68         StringBuilder b = new StringBuilder();
69         for(CompilationError error : errors)
70             b.append(error.description).append('\n');
71         return b.toString();
72     }
73     
74     @Override
75     public String toString() {
76         return getErrorsAsString();
77     }
78
79     public int getErrorCount() {
80         return errorCount;
81     }
82 }