Mercurial Hosting > luan
diff src/luan/lib/parser/ParseException.java @ 1111:88b5b81cad4a
move Parser to luan.lib.parser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Aug 2017 15:19:47 -0600 |
parents | src/luan/modules/parsers/ParseException.java@38a42f437fd2 |
children | 540e1940170f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/parser/ParseException.java Wed Aug 02 15:19:47 2017 -0600 @@ -0,0 +1,62 @@ +package luan.lib.parser; + + +public final class ParseException extends Exception { + public final String text; + public final int errorIndex; + public final int highIndex; + + public ParseException(Parser parser,String msg) { + super(msg); + this.text = parser.text; + this.errorIndex = parser.currentIndex(); + this.highIndex = parser.highIndex(); + } + + public ParseException(Parser parser,Exception cause) { + this(parser,cause.getMessage(),cause); + } + + public ParseException(Parser parser,String msg,Exception cause) { + super(msg,cause); + this.text = parser.text; + this.errorIndex = parser.currentIndex(); + this.highIndex = parser.highIndex(); + } + + private class Location { + final int line; + final int pos; + + Location(int index) { + int line = 0; + int i = -1; + while(true) { + int j = text.indexOf('\n',i+1); + if( j == -1 || j >= index ) + break; + i = j; + line++; + } + this.line = line; + this.pos = index - i - 1; + } + } + + private String[] lines() { + return text.split("\n",-1); + } + + @Override public String getMessage() { + Location loc = new Location(errorIndex); + String line = lines()[loc.line]; + String msg = super.getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n"; + StringBuilder sb = new StringBuilder(msg); + sb.append( line + "\n" ); + for( int i=0; i<loc.pos; i++ ) { + sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); + } + sb.append("^\n"); + return sb.toString(); + } +}