KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > trace > Traces


1 package jfun.parsec.trace;
2
3 import java.io.PrintWriter JavaDoc;
4
5 /**
6  * This class provides some common trace implementations.
7  * <p>
8  * @author Ben Yu
9  * @since version 1.1
10  * May 9, 2006 7:25:19 PM
11  */

12 public class Traces {
13   private static final int LDEADING = 32;
14   private static String JavaDoc getLeadingChars (CharSequence JavaDoc src, int ind, int n) {
15     int len = src.length();
16     if (ind >= len) {
17       return "<EOF>";
18     }
19     if (n + ind >= len) {
20       n = len - ind;
21       return src.subSequence(ind, ind+n).toString();
22     }
23     return src.subSequence (ind, ind+n).toString() + "...";
24   }
25   private static void printStatus(final PrintWriter JavaDoc out, CharSequence JavaDoc src, int ind, int steps, int offset){
26     out.print('[');
27     out.print(getLeadingChars (src, ind, LDEADING));
28     out.println(']');
29     out.println("steps="+steps+", offset="+offset);
30   }
31   private static void printErrorTrace(
32       final int min_steps, final String JavaDoc name, final PrintWriter JavaDoc out, Object JavaDoc exception, CharSequence JavaDoc src, int ind, int steps, int offset) {
33     if(steps < min_steps) return;
34     out.print(name);
35     out.print(": ");
36     if(exception != null){
37       out.println("exception raised.");
38     }
39     printStatus(out, src, ind, steps, offset);
40     out.flush();
41   }
42   private static void printResultTrace(final String JavaDoc name, final PrintWriter JavaDoc out, Object JavaDoc result, CharSequence JavaDoc src, int ind, int steps, int offset) {
43     out.println(name + " => "+ result);
44     printStatus(out, src, ind, steps, offset);
45     out.flush();
46   }
47   /**
48    * Create a Trace object that prints error message to output.
49    * @param name the name in the trace message.
50    * @param out the writer for the output.
51    * @param min_steps the minimal logical steps consumed to trigger the trace message.
52    * @return the Trace object.
53    */

54   public static Trace<Object JavaDoc> printError (final String JavaDoc name, final PrintWriter JavaDoc out, final int min_steps) {
55     return new EmptyTrace<Object JavaDoc>(){
56       @Override JavaDoc
57       public void onError(Object JavaDoc exception, CharSequence JavaDoc src, int ind, int steps, int offset){
58         printErrorTrace(min_steps, name, out, exception, src, ind, steps, offset);
59       }
60     };
61   }
62   /**
63    * Create a Trace object that prints trace message to output when parser succeeds.
64    * @param name the name in the trace message.
65    * @param out the writer for the output.
66    * @return the Trace object.
67    */

68   public static Trace<Object JavaDoc> printResult(final String JavaDoc name, final PrintWriter JavaDoc out){
69     return new EmptyTrace<Object JavaDoc>(){
70       @Override JavaDoc
71       public void onSuccess(Object JavaDoc result, CharSequence JavaDoc src, int ind, int steps, int offset){
72         printResultTrace(name, out, result, src, ind, steps, offset);
73       }
74     };
75   }
76   /**
77    * Create a Trace object that prints trace message to output.
78    * The minimal logical steps to trigger an error message is 1.
79    * @param name the name in the trace message.
80    * @param out the writer for the output.
81    * @return the Trace object.
82    */

83   public static Trace<Object JavaDoc> printTrace(final String JavaDoc name, final PrintWriter JavaDoc out){
84     return new Trace<Object JavaDoc>(){
85       public void onError(Object JavaDoc exception, CharSequence JavaDoc src, int ind, int steps, int offset){
86         printErrorTrace(1, name, out, exception, src, ind, steps, offset);
87       }
88       public void onSuccess(Object JavaDoc result, CharSequence JavaDoc src, int ind, int steps, int offset){
89         printResultTrace(name, out, result, src, ind, steps, offset);
90       }
91     };
92   }
93 }
94
Popular Tags