view src/luan/LuanSource.java @ 141:c2ee8a717b73

move loading of standard modules to luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@142 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 13 Jun 2014 16:37:49 +0000
parents 1f8b6edc2b08
children
line wrap: on
line source

package luan;


public final class LuanSource {
	public final String name;
	public final String text;

	public LuanSource(String name,String text) {
		this.name = name;
		this.text = text;
	}

	public static final class CompilerElement extends LuanElement {
		public final LuanSource source;

		public CompilerElement(LuanSource source) {
			if( source==null )
				throw new NullPointerException("source is null");
			this.source = source;
		}

		@Override String location() {
			return "Compiling " + source.name;
		}
	}

	public static final class Element extends LuanElement {
		public final LuanSource source;
		public final int start;
		public final int end;

		public Element(LuanSource source,int start,int end) {
			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;
		}

		public String text() {
			return source.text.substring(start,end);
		}

		@Override String location() {
			return source.name + ':' + 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;
		}

	}
}