changeset 682:0c334975d526

finish stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Apr 2016 21:41:40 -0600
parents f1c935be546d
children 67dd1449e354
files core/src/luan/LuanException.java core/src/luan/impl/LuanParser.java
diffstat 2 files changed, 70 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
diff -r f1c935be546d -r 0c334975d526 core/src/luan/LuanException.java
--- a/core/src/luan/LuanException.java	Thu Apr 14 20:10:38 2016 -0600
+++ b/core/src/luan/LuanException.java	Thu Apr 14 21:41:40 2016 -0600
@@ -2,27 +2,26 @@
 
 import java.io.StringWriter;
 import java.io.PrintWriter;
+import java.util.List;
+import java.util.ArrayList;
 
 
-public final class LuanException extends Exception implements DeepCloneable {
-	private LuanTable table = new LuanTable();
+public final class LuanException extends Exception {
 
 	public LuanException(String msg,Throwable cause) {
 		super(msg,cause);
-		setTable();
 	}
 
 	public LuanException(String msg) {
 		super(msg);
-		setTable();
 	}
 
 	public LuanException(Throwable cause) {
 		super(cause);
-		setTable();
 	}
 
-	private void setTable() {
+	public LuanTable table() {
+		LuanTable table = new LuanTable();
 		table.rawPut( "java", this );
 /*
 		for( StackTraceElement ste : luan.stackTrace ) {
@@ -42,24 +41,15 @@
 			table.rawPut( "throw", new LuanJavaFunction(
 				LuanException.class.getMethod( "throwThis" ), this
 			) );
+			table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction(
+				LuanException.class.getMethod( "getJavaStackTraceString" ), this
+			) );
 			metatable.rawPut( "__to_string", new LuanJavaFunction(
-				LuanException.class.getMethod( "__to_string", LuanTable.class ), null
+				LuanException.class.getMethod( "getFullMessage" ), this
 			) );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
-	}
-
-	@Override public LuanException shallowClone() {
-		return new LuanException(getMessage(),getCause());
-	}
-
-	@Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) {
-		LuanException clone = (LuanException)dc;
-		clone.table = (LuanTable)cloner.get(table);
-	}
-
-	public LuanTable table() {
 		return table;
 	}
 
@@ -68,10 +58,7 @@
 	}
 
 	public String getFullMessage() {
-		return __to_string(table);
-	}
-
-	public static String __to_string(LuanTable table) {
+		return getLuanStackTraceString();
 /*
 		StringBuilder buf = new StringBuilder();
 
@@ -97,10 +84,45 @@
 
 		return buf.toString();
 */
-		LuanException ex = (LuanException)table.rawGet("java");
+	}
+
+	public String getJavaStackTraceString() {
+		return getJavaStackTraceString(this);
+	}
+
+	private static String getJavaStackTraceString(Throwable th) {
 		StringWriter sw = new StringWriter();
-		ex.printStackTrace(new PrintWriter(sw));
+		th.printStackTrace(new PrintWriter(sw));
 		return sw.toString();
 	}
 
+	private List<StackTraceElement> justLuan() {
+		List<StackTraceElement> list = new ArrayList<StackTraceElement>();
+		StackTraceElement[] orig = getStackTrace();
+		for( int i=0; i<orig.length; i++ ) {
+			StackTraceElement ste = orig[i];
+			if( !ste.getClassName().startsWith("luan.impl.EXP") )
+				continue;
+			list.add(ste);
+			if( !ste.getMethodName().equals("doCall") )
+				i++;
+		}
+		return list;
+	}
+
+	public String getLuanStackTraceString() {
+		StringBuilder sb = new StringBuilder();
+		sb.append( getMessage() );
+		for( StackTraceElement ste : justLuan() ) {
+			sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() );
+			String method = ste.getMethodName();
+			if( !method.equals("doCall") )
+				sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
+		}
+		Throwable cause = getCause();
+		if( cause != null )
+			sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
+		return sb.toString();
+	}
+
 }
diff -r f1c935be546d -r 0c334975d526 core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Thu Apr 14 20:10:38 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Thu Apr 14 21:41:40 2016 -0600
@@ -1098,31 +1098,44 @@
 		parser.begin();
 		if( !parser.match('{') )
 			return parser.failure(null);
+		Expr tblExp = new Expr(Val.SINGLE,false);
+		tblExp.add( "LuanImpl.table(" );
+		Expr lastExp = tblExp;
+		List<Expr> builder = new ArrayList<Expr>();
+/*
 		Spaces();
-		List<Expr> builder = new ArrayList<Expr>();
 		Field(builder);
 		while( FieldSep() ) {
 			Spaces();
 			Field(builder);
 		}
+*/
+		do {
+			Spaces();  lastExp.addNewLines();
+			Expr exp = Field();
+			if( exp != null ) {
+				builder.add(exp);
+				lastExp = exp;
+				Spaces();  lastExp.addNewLines();
+			}
+		} while( FieldSep() );
 		Expr exp = TemplateExpressions(In.NOTHING);
 		if( exp != null )
 			builder.add(exp);
 		if( !parser.match('}') )
 			throw parser.exception("Expected table element or '}'");
+		tblExp.addAll( expString(builder).array() );
+		tblExp.add( ")" );
 		Spaces();
-		exp = new Expr(Val.SINGLE,false);
-		exp.add( "LuanImpl.table(" );
-		exp.addAll( expString(builder).array() );
-		exp.add( ")" );
-		return parser.success( exp );
+		tblExp.addNewLines();
+		return parser.success( tblExp );
 	}
 
 	private boolean FieldSep() throws ParseException {
 		return parser.anyOf(",;") || EndOfLine();
 	}
 
-	private boolean Field(List<Expr> builder) throws ParseException {
+	private Expr Field() throws ParseException {
 		parser.begin();
 		Expr exp = SubExpr(In.NOTHING);
 		if( exp==null )
@@ -1136,16 +1149,14 @@
 			newExp.add( "," );
 			newExp.addAll( val );
 			newExp.add( ")" );
-			builder.add( newExp );
-			return parser.success();
+			return parser.success(newExp);
 		}
 		parser.rollback();
 		Expr exprs = ExprZ(In.NOTHING);
 		if( exprs != null ) {
-			builder.add(exprs);
-			return parser.success();
+			return parser.success(exprs);
 		}
-		return parser.failure();
+		return parser.failure(null);
 	}
 
 	private Expr VarExp(In in) throws ParseException {