diff src/nabble/naml/compiler/StackTraceElement.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/naml/compiler/StackTraceElement.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,96 @@
+package nabble.naml.compiler;
+
+import java.lang.reflect.Method;
+import fschmidt.util.java.Interner;
+import nabble.naml.dom.Element;
+
+
+public final class StackTraceElement {
+
+	private static final Interner<StackTraceElement> interner = new Interner<StackTraceElement>();
+
+	public final Source source;
+	private final int lineNumber;
+	private final Element element;
+	private final String commandName;
+	private Method method;
+
+	StackTraceElement(Source source,int lineNumber) {
+		this.source = source;
+		this.lineNumber = lineNumber;
+		this.element = null;
+		this.commandName = null;
+	}
+
+	public StackTraceElement(Source source,Element element) {
+		this(source,element,(String)null);
+	}
+
+	StackTraceElement(Macro macro) {
+		this(macro.source,macro.element);
+	}
+
+	StackTraceElement(Source source,Element element,String commandName) {
+		this.source = source;
+		this.lineNumber = element.lineNumber();
+		this.element = element;
+		this.commandName = commandName==null ? null : commandName.intern();
+	}
+
+	StackTraceElement(Source source,Element element,JavaCommand javaCommand) {
+		this(source,element,javaCommand.name);
+		setMethod(javaCommand.method);
+	}
+
+	void setMethod(Method method) {
+		this.method = method;
+	}
+
+	@Override public boolean equals(Object obj) {
+		if( !(obj instanceof StackTraceElement) )
+			return false;
+		StackTraceElement ste = (StackTraceElement)obj;
+		return ste.source == source
+			&& ste.lineNumber == lineNumber
+			&& ste.element == element
+			&& ste.commandName == commandName
+		;
+	}
+
+	@Override public int hashCode() {
+		int hash = source.id.hashCode();
+		hash = 31*hash + lineNumber;
+		if( element != null )
+			hash = 31*hash + element.hashCode();
+		if( commandName != null )
+			hash = 31*hash + commandName.hashCode();
+		return hash;
+	}
+
+	StackTraceElement intern() {
+		return interner.intern(this);
+	}
+
+	public String toString() {
+		if( element == null )
+			return source + ":" + (lineNumber+1);
+		StringBuilder sb = new StringBuilder();
+		if( commandName != null )
+			sb.append( commandName );
+		sb
+			.append( "(" )
+			.append( source )
+			.append( ":" )
+			.append( lineNumber+1 )
+			.append( ") - " )
+			.append( element.openingTag() )
+		;
+		if( method != null )
+			sb.append( " - " ).append( method );
+		return sb.toString();
+	}
+
+	String commandName() {
+		return commandName;
+	}
+}