view src/luan/parser/ParseException.java @ 104:754e6030c029

remove parboiled and rewrite parser; add jline; git-svn-id: https://luan-java.googlecode.com/svn/trunk@105 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 12 May 2014 05:57:53 +0000
parents
children 735708619119
line wrap: on
line source

package luan.parser;


public class ParseException extends Exception {
	public final String text;
	public final int iCurrent;
	public final int iHigh;

	ParseException(String msg,String text,int iCurrent,int iHigh) {
		super(msg);
		this.text = text;
		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 = 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);
	}

	public String getFancyMessage() {
		Location loc = new Location(iCurrent);
		String line = lines()[loc.line];
		String msg = 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();
	}
}