summaryrefslogtreecommitdiff
path: root/sources/scalac/util/Reporter.java
blob: a1fb832e28512293830046df76a7d61ddaab2308 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.util;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import scalac.ApplicationError;

public class Reporter {

    //########################################################################
    // Private state

    private final BufferedReader reader;
    private final PrintWriter writer;

    /** Number of errors issued totally */
    private int errors;
    /** Number of warning issued totally */
    private int warnings;
    /** Number of notes issued totally */
    private int notes;

    //########################################################################
    // Reporter constructors

    public Reporter() {
        this(
            new BufferedReader(new InputStreamReader(System.in)),
            new PrintWriter(System.err, true));
    }

    public Reporter(BufferedReader reader, PrintWriter writer) {
        this.reader = reader;
        this.writer = writer;
        this.prompt = false;
        this.nowarn = false;
        this.verbose = false;
        this.errors = 0;
        this.notes = 0;
    }

    //########################################################################
    // Reporter state

    /** Whether warnings should be issued */
    public boolean nowarn;
    /** Whether notes and information messages should be issued */
    public boolean verbose;
    /** Whether a prompt should be displayed after errors and warnings */
    public boolean prompt;

    //########################################################################
    // Reporter interface - query

    /** Return the number of errors issued totally */
    public int errors() {
        return errors;
    }

    /** Return the number of warnings issued totally */
    public int warnings() {
        return warnings;
    }

    /** Return the number of notes issued totally */
    public int notes() {
        return notes;
    }

    /** Return the number of errors issued totally as a string */
    public String getErrorCountString() {
        return getCountString(errors, "error");
    }

    /** Return the number of warnings issued totally as a string */
    public String getWarningCountString() {
        return getCountString(warnings, "warning");
    }

    /** Return the number of notes issued totally as a string */
    public String getNoteCountString() {
        return getCountString(notes, "note");
    }

    public String getCountString(int count, String what) {
        switch (count) {
        case 0: return "no " + what + "s";
        case 1: return "one " + what;
        case 2: return "two " + what + "s";
        case 3: return "three " + what + "s";
        case 4: return "four " + what + "s";
        default: return count + " " + what + "s";
        }
    }

    //########################################################################
    // Reporter interface - report

    /** Reset all counters */
    public void resetCounters() {
        errors = 0;
        warnings = 0;
        notes = 0;
    }

    /** Issue a message */
    public void report(String message) {
        writer.println(message);
    }

    /** Issue a message */
    public void inform(String message) {
        if (verbose) report(message);
    }

    /** Issue an error */
    public void error(String message) {
        error(message, false);
    }

    /** Issue an error if it is not hidden */
    public void error(String message, boolean hidden) {
        if (!hidden || prompt) report(message);
        if (!hidden) errors++;
        if (prompt) failOnDemand();
    }

    /** Issue a warning */
    public void warning(String message) {
        warning(message, false);
    }

    /** Issue a warning if it is not hidden */
    public void warning(String message, boolean hidden) {
        if (nowarn) return;
        if (!hidden || prompt) report(message);
        if (!hidden) warnings++;
        if (prompt) failOnDemand();
    }

    /** Issue a note */
    public void note(String message) {
        note(message, false);
    }

    /** Issue a note if it is not hidden */
    public void note(String message, boolean hidden) {
        if (!hidden) report(message);
        if (!hidden) notes++;
    }

    public void printSummary() {
        if (errors() > 0) report(getErrorCountString() + " found");
        if (warnings() > 0) report(getWarningCountString() + " found");
        if (notes() > 0) report(getNoteCountString() + " found");
    }

    //########################################################################
    // Reporter interface - fail

    /** Fail only if requested */
    public void failOnDemand() {
        failOnDemand("user abort");
    }

    /** Fail only if requested */
    public void failOnDemand(String message) {
        try {
            while (true) {
                writer.print("r)esume, a)bort: ");
                String line = reader.readLine();
                if (line == null) continue; else line = line.toLowerCase();
                if ("abort".startsWith(line))
                    throw new ApplicationError(message);
                if ("resume".startsWith(line)) return;
            }
        } catch (IOException e) {
            throw new ApplicationError("input read error");
        }
    }

    //########################################################################
}