KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > beaver > comp > util > Log


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * This file is part of Beaver Parser Generator. *
3  * Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>. *
4  * All rights reserved. *
5  * See the file "LICENSE" for the terms and conditions for copying, *
6  * distribution and modification of Beaver. *
7  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

8
9 package beaver.comp.util;
10
11 import java.io.PrintStream JavaDoc;
12
13 import beaver.Scanner;
14 import beaver.Symbol;
15 import beaver.comp.io.SrcReader;
16
17 /**
18  * Writes log in the following format:
19  * <pre>
20  * LogLevel:SL,SC-EL,EC: message
21  * </pre>
22  * Where SL, SC - line and column where issue start, EL,EC line and column where issue ends.
23  */

24 public class Log
25 {
26     static public class Record
27     {
28         static public class List
29         {
30             private Record first, last;
31             private int size;
32             
33             public void add(Record rec)
34             {
35                 if (last == null)
36                     first = last = rec;
37                 else
38                     last = last.next = rec;
39                 size++;
40             }
41             
42             public Record start()
43             {
44                 return first;
45             }
46             
47             public int size()
48             {
49                 return size;
50             }
51             
52             public void reset()
53             {
54                 first = last = null;
55                 size = 0;
56             }
57         }
58         
59         Record next;
60         
61         private final int start_pos;
62         private final int end_pos;
63         private final String JavaDoc message;
64
65         Record(int start, int end, String JavaDoc msg)
66         {
67             start_pos = start;
68             end_pos = end;
69             message = msg;
70         }
71         
72         public void report(String JavaDoc type, PrintStream JavaDoc out, SrcReader src_reader)
73         {
74             out.print(src_reader.file.getName());
75             out.print(':');
76             if (start_pos > 0)
77             {
78                 out.print(Symbol.getLine(start_pos));
79                 out.print(',');
80                 out.print(Symbol.getColumn(start_pos));
81                 out.print('-');
82                 out.print(Symbol.getLine(end_pos));
83                 out.print(',');
84                 out.print(Symbol.getColumn(end_pos));
85                 out.print(':');
86             }
87             if (type != null)
88             {
89                 out.print(' ');
90                 out.print(type);
91                 out.print(':');
92             }
93             out.print(' ');
94             out.println(message);
95
96             if (start_pos > 0 )
97             {
98                 int start_line = Symbol.getLine(start_pos);
99                 int end_line = Symbol.getLine(end_pos);
100                 if (start_line == end_line)
101                 {
102                     String JavaDoc line = src_reader.getLine(start_line).replace('\t', ' ');
103                     out.print(line);
104                     int start_column = Symbol.getColumn(start_pos);
105                     int n;
106                     for (n = start_column - 1; n > 0; n--)
107                     {
108                         out.print(' ');
109                     }
110                     out.print('^');
111                     for (n = Symbol.getColumn(end_pos) - start_column - 1; n > 0; n--)
112                     {
113                         out.print('-');
114                     }
115                     if (n == 0)
116                     {
117                         out.print('^');
118                     }
119                     out.println();
120                 }
121             }
122         }
123     }
124     
125     private Record.List errors = new Record.List();
126     private Record.List warnings = new Record.List();
127     private Record.List messages = new Record.List();
128     
129     public void error(Symbol symbol, String JavaDoc msg)
130     {
131         error(symbol.getStart(), symbol.getEnd(), msg);
132     }
133
134     public void error(Scanner.Exception e)
135     {
136         int location = Symbol.makePosition(e.line, e.column);
137         error(location, location, e.getMessage());
138     }
139
140     public void error(int start_pos, int end_pos, String JavaDoc msg)
141     {
142         errors.add(new Record(start_pos, end_pos, msg));
143     }
144     
145     public void error(String JavaDoc msg)
146     {
147         error(0, 0, msg);
148     }
149     
150     public void warning(Symbol symbol, String JavaDoc msg)
151     {
152         warning(symbol.getStart(), symbol.getEnd(), msg);
153     }
154
155     public void warning(int start_pos, int end_pos, String JavaDoc msg)
156     {
157         warnings.add(new Record(start_pos, end_pos, msg));
158     }
159
160     public void warning(String JavaDoc msg)
161     {
162         warning(0, 0, msg);
163     }
164     
165     public void message(String JavaDoc msg)
166     {
167         messages.add(new Record(0, 0, msg));
168     }
169
170     public boolean hasErrors()
171     {
172         return errors.size() > 0;
173     }
174     
175     public void report(String JavaDoc src_name, SrcReader src_reader)
176     {
177         int n_err = errors.size(), n_warn = warnings.size();
178         if (n_err > 0 || n_warn > 0)
179         {
180             PrintStream JavaDoc out = System.err;
181
182             for (Record rec = errors.start(); rec != null; rec = rec.next)
183             {
184                 rec.report("Error", out, src_reader);
185             }
186             errors.reset();
187
188             for (Record rec = warnings.start(); rec != null; rec = rec.next)
189             {
190                 rec.report("Warning", out, src_reader);
191             }
192             warnings.reset();
193
194             if (n_err > 0 || n_warn > 0)
195             {
196                 out.print(src_name);
197                 out.print(": ");
198                 out.print(n_err);
199                 out.print(" error");
200                 if (n_err != 1)
201                     out.print('s');
202                 out.print(", ");
203                 out.print(n_warn);
204                 out.print(" warning");
205                 if (n_warn != 1)
206                     out.print('s');
207                 out.println('.');
208             }
209         }
210         for (Record rec = messages.start(); rec != null; rec = rec.next)
211         {
212             rec.report(null, System.out, src_reader);
213         }
214         messages.reset();
215     }
216 }
217
Popular Tags