Trace.java 792 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import java.io.*;
  2. public class Trace implements ITrace {
  3. public String indent;
  4. public PrintStream out;
  5. public Trace(PrintStream out, String indent) {
  6. this.out = out;
  7. this.indent = indent;
  8. }
  9. public Trace(PrintStream out) {
  10. this(out, "");
  11. }
  12. public Trace() {
  13. this(System.err); // output defaults to System.err
  14. }
  15. public void print(String s, int lno) {
  16. if (out != null)
  17. out.printf("%4d: %s\n", lno, indent + s);
  18. }
  19. public void print(Token t) {
  20. print(t.match.toString()+" \""+t.toString()+"\"", t.lno);
  21. }
  22. public Trace nonterm(String s, int lno) {
  23. print(s, lno);
  24. return new Trace(out, indent + "| ");
  25. }
  26. public void reset() {
  27. indent = "";
  28. }
  29. }