00001 package edu.mit.csail.sdg.squander.specfile.parser;
00002
00003 import org.antlr.runtime.RecognitionException;
00004 import org.antlr.runtime.tree.Tree;
00005
00011 @SuppressWarnings("serial")
00012 public class SpecFileParserException extends RuntimeException {
00014 private int line = -1, column = -1;
00015
00017 private Tree token;
00018
00020 private String source;
00021
00022 public SpecFileParserException(String msg) {
00023 super(msg);
00024 }
00025
00026 public SpecFileParserException(String msg, Throwable cause) {
00027 super(msg, cause);
00028 }
00029
00030 public SpecFileParserException(String msg, Tree token) {
00031 super(msg);
00032 setToken(token);
00033 }
00034
00035 public SpecFileParserException(String msg, String source) {
00036 super(msg);
00037 setSource(source);
00038 }
00039
00040 public SpecFileParserException(RecognitionException cause) {
00041 super("parsing error on token '" + cause.token.getText() +"' at column " + cause.charPositionInLine + ", line " + cause.line, cause);
00042 this.column = cause.charPositionInLine;
00043 this.line = cause.line;
00044 this.source = cause.input.toString();
00045 }
00046
00047 public SpecFileParserException(Throwable cause) {
00048 super(cause);
00049 }
00050
00051 public SpecFileParserException(Throwable cause, Tree token) {
00052 super(cause);
00053 setToken(token);
00054 }
00055
00056 public void setToken(Tree token) {
00057 if (token != null) {
00058 this.token = token;
00059 this.column = token.getCharPositionInLine();
00060 this.line = token.getLine();
00061 }
00062 }
00063
00064 public void setSource(String source) {
00065 this.source = source;
00066 }
00067
00068 public String source() {
00069 return source;
00070 }
00071
00072 public int column() {
00073 return column;
00074 }
00075
00076 public int line() {
00077 return line;
00078 }
00079
00081 public String token() {
00082 if (token == null) return null;
00083
00084 StringBuilder sb = new StringBuilder();
00085 return layout(token, sb, "").toString();
00086 }
00087
00089 private StringBuilder layout(Tree t, StringBuilder sb, String buf) {
00090 sb.append(buf).append(t).append('\n');
00091 for (int i = 0; i < t.getChildCount(); i++)
00092 layout(t.getChild(i), sb, buf + '\t');
00093 return sb;
00094 }
00095 }