Mercurial Hosting > luan
diff core/src/luan/impl/ParseException.java @ 171:3dcb0f9bee82
add core component
git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 05:41:22 +0000 |
parents | src/luan/impl/ParseException.java@4eaee12f6c65 |
children | 859c0dedc8b6 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/luan/impl/ParseException.java Sun Jun 22 05:41:22 2014 +0000 @@ -0,0 +1,55 @@ +package luan.impl; + +import luan.LuanSource; + + +public final class ParseException extends Exception { + public final LuanSource src; + public final int iCurrent; + public final int iHigh; + + ParseException(String msg,LuanSource src,int iCurrent,int iHigh) { + super(msg); + this.src = src; + this.iCurrent = iCurrent; + this.iHigh = iHigh; +//System.out.println("iCurrent = "+iCurrent); +//System.out.println("iHigh = "+iHigh); + } + + private class Location { + final int line; + final int pos; + + Location(int index) { + int line = 0; + int i = -1; + while(true) { + int j = src.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 src.text.split("\n",-1); + } + + public String getFancyMessage() { + Location loc = new Location(iCurrent); + String line = lines()[loc.line]; + String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + src.name + "\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(); + } +}