01: package java_cup;
02:
03: import java_cup.runtime.Symbol;
04: import java.lang.reflect.Field;
05: import java.lang.reflect.Modifier;
06:
07: public class ErrorManager {
08: private static ErrorManager errorManager;
09: private int errors = 0;
10: private int warnings = 0;
11: private int fatals = 0;
12:
13: public int getFatalCount() {
14: return fatals;
15: }
16:
17: public int getErrorCount() {
18: return errors;
19: }
20:
21: public int getWarningCount() {
22: return warnings;
23: }
24:
25: static {
26: errorManager = new ErrorManager();
27: }
28:
29: public static ErrorManager getManager() {
30: return errorManager;
31: }
32:
33: private ErrorManager() {
34: }
35:
36: //TODO: migrate to java.util.logging
37: /**
38: * Error message format:
39: * ERRORLEVEL at (LINE/COLUMN)@SYMBOL: MESSAGE
40: * ERRORLEVEL : MESSAGE
41: **/
42: public void emit_fatal(String message) {
43: System.err.println("Fatal : " + message);
44: fatals++;
45: }
46:
47: public void emit_fatal(String message, Symbol sym) {
48: //System.err.println("Fatal at ("+sym.left+"/"+sym.right+")@"+convSymbol(sym)+" : "+message);
49: System.err.println("Fatal: " + message + " @ " + sym);
50: fatals++;
51: }
52:
53: public void emit_warning(String message) {
54: System.err.println("Warning : " + message);
55: warnings++;
56: }
57:
58: public void emit_warning(String message, Symbol sym) {
59: // System.err.println("Warning at ("+sym.left+"/"+sym.right+")@"+convSymbol(sym)+" : "+message);
60: System.err.println("Fatal: " + message + " @ " + sym);
61: warnings++;
62: }
63:
64: public void emit_error(String message) {
65: System.err.println("Error : " + message);
66: errors++;
67: }
68:
69: public void emit_error(String message, Symbol sym) {
70: // System.err.println("Error at ("+sym.left+"/"+sym.right+")@"+convSymbol(sym)+" : "+message);
71: System.err.println("Error: " + message + " @ " + sym);
72: errors++;
73: }
74:
75: private static String convSymbol(Symbol symbol) {
76: String result = (symbol.value == null) ? "" : " (\""
77: + symbol.value.toString() + "\")";
78: Field[] fields = sym.class.getFields();
79: for (int i = 0; i < fields.length; i++) {
80: if (!Modifier.isPublic(fields[i].getModifiers()))
81: continue;
82: try {
83: if (fields[i].getInt(null) == symbol.sym)
84: return fields[i].getName() + result;
85: } catch (Exception ex) {
86: }
87: }
88: return symbol.toString() + result;
89: }
90:
91: }
|