view core/src/luan/LuanElement.java @ 460:b48cfa14ba60

improve stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 May 2015 14:32:29 -0600
parents 3dcb0f9bee82
children 5d4a78c93383
line wrap: on
line source

package luan;


public final class LuanElement {
	public final LuanSource source;
	public final int start;
	public final int end;
	private final String text;

	public LuanElement(LuanSource source,int start,int end) {
		this(source,start,end,null);
	}

	public LuanElement(LuanSource source,int start,int end,String text) {
		if( source==null )
			throw new NullPointerException("source is null");
		this.source = source;
		this.start = start;
		while( end > 0 && Character.isWhitespace(source.text.charAt(end-1)) ) {
			end--;
		}
		this.end = end;
		this.text = text;
	}

	public String text() {
		return text!=null ? text : source.text.substring(start,end);
	}

	private String location() {
		return source.name + " line " + lineNumber();
	}

	private int lineNumber() {
		int line = 0;
		int i = -1;
		do {
			line++;
			i = source.text.indexOf('\n',i+1);
		} while( i != -1 && i < start );
		return line;
	}

	String toString(String fnName) {
		String s = location();
		if( fnName != null )
			s += " in call to '" + fnName + "'";
		return s;
	}

}