~ubuntu-branches/ubuntu/gutsy/jflex/gutsy

« back to all changes in this revision

Viewing changes to src/JFlex/Out.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2002-02-16 13:38:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020216133821-5wsdprpt9xl7ondr
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 * JFlex 1.3.5                                                             *
 
3
 * Copyright (C) 1998-2001  Gerwin Klein <lsf@jflex.de>                    *
 
4
 * All rights reserved.                                                    *
 
5
 *                                                                         *
 
6
 * This program is free software; you can redistribute it and/or modify    *
 
7
 * it under the terms of the GNU General Public License. See the file      *
 
8
 * COPYRIGHT for more information.                                         *
 
9
 *                                                                         *
 
10
 * This program is distributed in the hope that it will be useful,         *
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 
13
 * GNU General Public License for more details.                            *
 
14
 *                                                                         *
 
15
 * You should have received a copy of the GNU General Public License along *
 
16
 * with this program; if not, write to the Free Software Foundation, Inc., *
 
17
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 
18
 *                                                                         *
 
19
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
20
 
 
21
package JFlex;
 
22
 
 
23
 
 
24
import java.io.*;
 
25
import java.awt.TextArea;
 
26
 
 
27
 
 
28
/**
 
29
 * In this class all output to the java console is filtered.
 
30
 *
 
31
 * Use the switches VERBOSE, TIME and DUMP at compile time to determine
 
32
 * the verbosity of JFlex output. There is no switch for
 
33
 * suppressing error messages. VERBOSE and TIME can be overridden 
 
34
 * by command line paramters.
 
35
 *
 
36
 * Redirects output to a TextArea in GUI mode.
 
37
 *
 
38
 * Counts error and warning messages.
 
39
 *
 
40
 * <UL>
 
41
 * <LI>
 
42
 * VERBOSE should be switched on in all normal cases. 
 
43
 *         It is used for standard progress messages to the user.
 
44
 * </LI><LI>
 
45
 * TIME    can be set to true for performance measurements if 
 
46
 *         time statistics for the different stages of generation
 
47
 *         are to be printed.
 
48
 * </LI><LI>       
 
49
 * DUMP    this one gives you all the information you want (or not).
 
50
 *         BUT: prepare to wait.
 
51
 *         If only dump-information from specific classes is
 
52
 *         needed, compile all classes with DUMP=false first,
 
53
 *         then recompile this class and all the classes that
 
54
 *         are to dump their information with DUMP=true.
 
55
 * </LI>
 
56
 * </UL>
 
57
 *
 
58
 * @author Gerwin Klein
 
59
 * @version JFlex 1.3.5, $Revision: 1.29 $, $Date: 2001/10/08 10:08:03 $
 
60
 */
 
61
public final class Out implements ErrorMessages {
 
62
 
 
63
  /** platform dependent newline sequence */
 
64
  public static final String NL = System.getProperty("line.separator");
 
65
  
 
66
  /**
 
67
   * If VERBOSE is false, no progress output will be generated
 
68
   */
 
69
  public static boolean VERBOSE     = true;
 
70
 
 
71
  /**
 
72
   * If TIME is true, jflex will print time statistics about the generation process
 
73
   */
 
74
  public static boolean TIME        = false;
 
75
 
 
76
  /**
 
77
   * If DUMP is true, you will be flooded with information (e.g. dfa tables).
 
78
   */
 
79
  public static boolean DUMP        = false;
 
80
 
 
81
  /**
 
82
   * If DOT is true, jflex will write graphviz .dot files for generated automata
 
83
   */
 
84
  public static boolean DOT         = false;
 
85
    
 
86
  /**
 
87
   * If DEBUG is true, additional verbose debug information is produced
 
88
   */
 
89
  public final static boolean DEBUG = false;
 
90
 
 
91
  /** count total warnings */
 
92
  public static int warnings;
 
93
 
 
94
  /** count total errors */
 
95
  public static int errors;
 
96
 
 
97
  /** output device */
 
98
  private static StdOutWriter out = new StdOutWriter();
 
99
 
 
100
 
 
101
  /**
 
102
   * Switches to GUI mode if <code>text</code> is not <code>null</code>
 
103
   *
 
104
   * @param text  the message TextArea of the JFlex GUI
 
105
   */
 
106
  public static void setGUIMode(TextArea text) {
 
107
    out.setGUIMode(text);
 
108
  }
 
109
 
 
110
  /**
 
111
   * All parts of JFlex, that want to report something about 
 
112
   * time statistic should use this method for their output.
 
113
   *
 
114
   * @param message  the message to be printed
 
115
   */
 
116
  public static void time(String message) {
 
117
    if (TIME) out.println(message);
 
118
  }
 
119
  
 
120
 
 
121
  /**
 
122
   * All parts of JFlex, that want to report generation progress
 
123
   * should use this method for their output.
 
124
   *
 
125
   * @param message  the message to be printed
 
126
   */
 
127
  public static void println(String message) {
 
128
    if (VERBOSE) out.println(message);
 
129
  }
 
130
 
 
131
 
 
132
  /**
 
133
   * All parts of JFlex, that want to report generation progress
 
134
   * should use this method for their output.
 
135
   *
 
136
   * @param message  the message to be printed
 
137
   */
 
138
  public static void print(String message) {
 
139
    if (VERBOSE) out.print(message);
 
140
  }
 
141
 
 
142
  /**
 
143
   * Dump debug information to System.out
 
144
   *
 
145
   * Use like this 
 
146
   *
 
147
   * <code>if (Out.DEBUG) Out.debug(message)</code>
 
148
   *
 
149
   * to save performance during normal operation (when DEBUG
 
150
   * is turned off).
 
151
   */
 
152
  public static void debug(String message) {
 
153
    if (DEBUG) System.out.println(message); 
 
154
  }
 
155
 
 
156
 
 
157
  /**
 
158
   * All parts of JFlex, that want to provide dump information
 
159
   * should use this method for their output.
 
160
   *
 
161
   * @message the message to be printed 
 
162
   */
 
163
  public static void dump(String message) {
 
164
    if (DUMP) out.println(message);
 
165
  }
 
166
 
 
167
  
 
168
  /**
 
169
   * All parts of JFlex, that want to report error messages
 
170
   * should use this method for their output.
 
171
   *
 
172
   * @message  the message to be printed
 
173
   */
 
174
  private static void err(String message) {
 
175
    out.println(message);
 
176
  }
 
177
  
 
178
  
 
179
  /**
 
180
   * throws a GeneratorException if there are any errors recorded
 
181
   */
 
182
  public static void checkErrors() {
 
183
    if (errors > 0) throw new GeneratorException();
 
184
  }
 
185
  
 
186
 
 
187
  /**
 
188
   * print error and warning statistics
 
189
   */
 
190
  public static void statistics() {    
 
191
    StringBuffer line = new StringBuffer(errors+" error");
 
192
    if (errors != 1) line.append("s");
 
193
 
 
194
    line.append(", "+warnings+" warning");
 
195
    if (warnings != 1) line.append("s");
 
196
 
 
197
    line.append(".");
 
198
    err(line.toString());
 
199
  }
 
200
 
 
201
 
 
202
  /**
 
203
   * reset error and warning counters
 
204
   */
 
205
  public static void resetCounters() {
 
206
    errors = 0;
 
207
    warnings = 0;
 
208
  }
 
209
 
 
210
  
 
211
  /**
 
212
   * print a warning without position information
 
213
   *
 
214
   * @param message   the warning message
 
215
   */  
 
216
  public static void warning(String message) {
 
217
    warnings++;
 
218
 
 
219
    err(NL+"Warning : "+message);
 
220
  }
 
221
 
 
222
 
 
223
  /**
 
224
   * print a warning with line information
 
225
   *
 
226
   * @param message  code of the warning message
 
227
   * @param line     the line information
 
228
   *
 
229
   * @see ErrorMessages
 
230
   */
 
231
  public static void warning(int message, int line) {
 
232
    warnings++;
 
233
 
 
234
    String msg = NL+"Warning";
 
235
    if (line > 0) msg = msg+" in line "+(line+1);
 
236
 
 
237
    err(msg+": "+messages[message]);
 
238
  }
 
239
 
 
240
 
 
241
  /**
 
242
   * print warning message with location information
 
243
   *
 
244
   * @param file     the file the warning is issued for
 
245
   * @param message  the code of the message to print
 
246
   * @param line     the line number of the position
 
247
   * @param column   the column of the position
 
248
   */
 
249
  public static void warning(File file, int message, int line, int column) {
 
250
 
 
251
    String msg = NL+"Warning";
 
252
    if (file != null) msg += " in file \""+file+"\"";
 
253
    if (line >= 0) msg = msg+" (line "+(line+1)+")";
 
254
 
 
255
    try {
 
256
      err(msg+": "+NL+messages[message]);
 
257
    }
 
258
    catch (ArrayIndexOutOfBoundsException e) {
 
259
      err(msg);
 
260
    }
 
261
 
 
262
    warnings++;
 
263
 
 
264
    if (line >= 0) {
 
265
      if (column >= 0)
 
266
        showPosition(file, line, column);
 
267
      else 
 
268
        showPosition(file, line);
 
269
    }
 
270
  }
 
271
 
 
272
  
 
273
  /**
 
274
   * print error message (string)
 
275
   *
 
276
   * @param message  the message to print
 
277
   */
 
278
  public static void error(String message) {
 
279
    errors++;
 
280
    err(NL+message);
 
281
  }
 
282
 
 
283
 
 
284
  /**
 
285
   * print error message (code)
 
286
   *  
 
287
   * @param message  the code of the error message
 
288
   *
 
289
   * @see ErrorMessages   
 
290
   */ 
 
291
  public static void error(int message) {
 
292
    errors++;
 
293
    err(NL+"Error: "+messages[message] );
 
294
  }
 
295
 
 
296
 
 
297
  /**
 
298
   * IO error message for a file (displays file 
 
299
   * name in parentheses).
 
300
   *
 
301
   * @param message  the code of the error message
 
302
   * @param file     the file it occurred for
 
303
   */
 
304
  public static void error(int message, File file) {
 
305
    errors++;
 
306
    err(NL+"Error: "+messages[message]+" ("+file+")");
 
307
  }
 
308
 
 
309
 
 
310
  /**
 
311
   * print error message with location information
 
312
   *
 
313
   * @param file     the file the error occurred for
 
314
   * @param message  the code of the error message to print
 
315
   * @param line     the line number of error position
 
316
   * @param column   the column of error position
 
317
   */
 
318
  public static void error(File file, int message, int line, int column) {
 
319
 
 
320
    String msg = NL+"Error";
 
321
    if (file != null) msg += " in file \""+file+"\"";
 
322
    if (line >= 0) msg = msg+" (line "+(line+1)+")";
 
323
 
 
324
    try {
 
325
      err(msg+": "+NL+messages[message]);
 
326
    }
 
327
    catch (ArrayIndexOutOfBoundsException e) {
 
328
      err(msg);
 
329
    }
 
330
 
 
331
    errors++;
 
332
 
 
333
    if (line >= 0) {
 
334
      if (column >= 0)
 
335
        showPosition(file, line, column);
 
336
      else 
 
337
        showPosition(file, line);
 
338
    }
 
339
  }
 
340
 
 
341
 
 
342
  /**
 
343
   * prints a line of a file with marked position.
 
344
   *
 
345
   * @param file    the file of which to show the line
 
346
   * @param line    the line to show
 
347
   * @param column  the column in which to show the marker
 
348
   */
 
349
  public static void showPosition(File file, int line, int column) {
 
350
    try {
 
351
      String ln = getLine(file, line);
 
352
      if (ln != null) {
 
353
        err( ln );
 
354
 
 
355
        if (column < 0) return;
 
356
        
 
357
        String t = "^";  
 
358
        for (int i = 0; i < column; i++) t = " "+t;  
 
359
        
 
360
        err(t);
 
361
      }
 
362
    }
 
363
    catch (IOException e) {
 
364
      /* silently ignore IO errors, don't show anything */
 
365
    }
 
366
  }
 
367
 
 
368
 
 
369
  /**
 
370
   * print a line of a file
 
371
   *
 
372
   * @param file  the file to show
 
373
   * @param line  the line number 
 
374
   */
 
375
  public static void showPosition(File file, int line) {
 
376
    try {
 
377
      String ln = getLine(file, line);
 
378
      if (ln != null) err(ln);
 
379
    }
 
380
    catch (IOException e) {
 
381
      /* silently ignore IO errors, don't show anything */
 
382
    }
 
383
  }
 
384
 
 
385
 
 
386
  /**
 
387
   * get one line from a file 
 
388
   *
 
389
   * @param file   the file to read
 
390
   * @param line   the line number to get
 
391
   *
 
392
   * @throw IOException  if any error occurs
 
393
   */
 
394
  private static String getLine(File file, int line) throws IOException {
 
395
    BufferedReader reader = new BufferedReader(new FileReader(file));
 
396
 
 
397
    String msg = "";
 
398
 
 
399
    for (int i = 0; i <= line; i++)
 
400
      msg = reader.readLine();
 
401
 
 
402
    reader.close();
 
403
    
 
404
    return msg;
 
405
  }
 
406
 
 
407
 
 
408
  /**
 
409
   * Print system information (e.g. in case of unexpected exceptions)
 
410
   */
 
411
  public static void printSystemInfo() {
 
412
    err("Java version:  "+System.getProperty("java.version"));
 
413
    err("Runtime name:  "+System.getProperty("java.runtime.name"));
 
414
    err("Vendor:        "+System.getProperty("java.vendor")); 
 
415
    err("VM version:    "+System.getProperty("java.vm.version")); 
 
416
    err("VM vendor:     "+System.getProperty("java.vm.vendor"));
 
417
    err("VM name:       "+System.getProperty("java.vm.name"));
 
418
    err("VM info:       "+System.getProperty("java.vm.info"));
 
419
    err("OS name:       "+System.getProperty("os.name"));
 
420
    err("OS arch:       "+System.getProperty("os.arch"));
 
421
    err("OS version:    "+System.getProperty("os.version"));
 
422
    err("Encoding:      "+System.getProperty("file.encoding"));
 
423
    err("JFlex version: "+Main.version);
 
424
  }
 
425
 
 
426
 
 
427
  /**
 
428
   * Request a bug report for an unexpected Exception/Error.
 
429
   */
 
430
  public static void requestBugReport(Error e) {
 
431
    err("An unexpected error occurred. Please send a report of this to");
 
432
    err("<bugs@jflex.de> and include the following information:");
 
433
    err("");
 
434
    printSystemInfo();
 
435
    err("Exception:");
 
436
    e.printStackTrace(out);
 
437
    err("");
 
438
    err("Please also include a specification (as small as possible)");
 
439
    err("that triggered this error. You may also want to check at");
 
440
    err("http://www.jflex.de if there is a newer version available");
 
441
    err("that doesn't have this problem");
 
442
    err("");
 
443
    err("Thanks for your support.");
 
444
  }
 
445
}