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