changeset 42:786699c78837

implement try-catch git-svn-id: https://luan-java.googlecode.com/svn/trunk@43 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 23 Dec 2012 06:36:56 +0000
parents c53dc854e6cc
children 80b67b1a653c
files src/luan/LuaException.java src/luan/LuaState.java src/luan/interp/LuaParser.java src/luan/lib/BasicLib.java src/luan/tools/CmdLine.java
diffstat 5 files changed, 49 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r c53dc854e6cc -r 786699c78837 src/luan/LuaException.java
--- a/src/luan/LuaException.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/LuaException.java	Sun Dec 23 06:36:56 2012 +0000
@@ -2,20 +2,38 @@
 
 
 public class LuaException extends Exception {
+	private final String stackTrace;
 
-	public LuaException(LuaState lua,LuaElement el,String msg) {
-		super(hideNull(msg)+stackTrace(lua,el));
+	public LuaException(LuaState lua,LuaElement el,Object msg) {
+		super(message(msg),cause(msg));
+		stackTrace = stackTrace(lua,el,msg);
+	}
+
+	@Override public String getMessage() {
+		return super.getMessage() + stackTrace;
+	}
+
+	private String message() {
+		return super.getMessage();
 	}
 
-	public LuaException(LuaState lua,LuaElement el,Exception cause) {
-		super(hideNull(cause.getMessage())+stackTrace(lua,el),cause);
+	private static Throwable cause(Object msg) {
+		return msg instanceof Throwable ? (Throwable)msg : null;
 	}
 
-	private static String hideNull(String s) {
-		return s==null ? "" : s;
+	private static String message(Object msg) {
+		if( msg instanceof LuaException ) {
+			LuaException le = (LuaException)msg;
+			return le.message();
+		} else if( msg instanceof Throwable ) {
+			Throwable t = (Throwable)msg;
+			return t.getMessage();
+		} else {
+			return msg.toString();
+		}
 	}
 
-	private static String stackTrace(LuaState lua,LuaElement el) {
+	private static String stackTrace(LuaState lua,LuaElement el,Object msg) {
 		StringBuilder buf = new StringBuilder();
 		int i = lua.stackTrace.size() - 1;
 		do {
@@ -23,6 +41,10 @@
 			buf.append( "\n\t" ).append( el.toString(stackTraceElement.fnName) );
 			el = stackTraceElement.call;
 		} while( --i >= 0 );
+		if( msg instanceof LuaException ) {
+			LuaException le = (LuaException)msg;
+			buf.append( "\ncaused by:" ).append( le.stackTrace );
+		}
 		return buf.toString();
 	}
 }
diff -r c53dc854e6cc -r 786699c78837 src/luan/LuaState.java
--- a/src/luan/LuaState.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/LuaState.java	Sun Dec 23 06:36:56 2012 +0000
@@ -65,6 +65,10 @@
 			return checkString( el, Lua.first( call(fn,el,"__tostring",obj) ) );
 		if( obj == null )
 			return "nil";
+		if( obj instanceof LuaException ) {
+			LuaException le = (LuaException)obj;
+			return le.getMessage();
+		}
 		return obj.toString();
 	}
 
diff -r c53dc854e6cc -r 786699c78837 src/luan/interp/LuaParser.java
--- a/src/luan/interp/LuaParser.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/interp/LuaParser.java	Sun Dec 23 06:36:56 2012 +0000
@@ -223,6 +223,7 @@
 					BreakStmt(),
 					GenericForStmt(),
 					NumericForStmt(),
+					TryStmt(),
 					DoStmt(),
 					WhileStmt(),
 					RepeatStmt(),
@@ -316,6 +317,16 @@
 		);
 	}
 
+	Rule TryStmt() {
+		return Sequence(
+			Keyword("try"), Block(),
+			Keyword("catch"), Name(), addSymbol( (String)pop() ),
+			Keyword("do"), Block(), Keyword("end"),
+			push( new TryStmt( (Stmt)pop(1), symbolsSize()-1, (Stmt)pop() ) ),
+			popSymbols(1)
+		);
+	}
+
 	Rule DoStmt() {
 		return Sequence(
 			Keyword("do"), Block(), Keyword("end")
@@ -827,6 +838,7 @@
 	static final Set<String> keywords = new HashSet<String>(Arrays.asList(
 		"and",
 		"break",
+		"catch",
 		"do",
 		"else",
 		"elseif",
@@ -847,6 +859,7 @@
 		"then",
 		"to",
 		"true",
+		"try",
 		"until",
 		"while"
 	));
diff -r c53dc854e6cc -r 786699c78837 src/luan/lib/BasicLib.java
--- a/src/luan/lib/BasicLib.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/lib/BasicLib.java	Sun Dec 23 06:36:56 2012 +0000
@@ -26,7 +26,7 @@
 		LuaTable global = lua.global();
 		global.put( "_G", global );
 		add( global, "do_file", LuaState.class, String.class );
-		add( global, "error", LuaState.class, String.class );
+		add( global, "error", LuaState.class, Object.class );
 		add( global, "get_metatable", LuaState.class, Object.class );
 		add( global, "ipairs", LuaTable.class );
 		add( global, "load", LuaState.class, String.class, String.class );
@@ -225,7 +225,7 @@
 		return lua.toString(LuaElement.JAVA,v);
 	}
 
-	public static void error(LuaState lua,String msg) throws LuaException {
+	public static void error(LuaState lua,Object msg) throws LuaException {
 		throw new LuaException(lua,LuaElement.JAVA,msg);
 	}
 
diff -r c53dc854e6cc -r 786699c78837 src/luan/tools/CmdLine.java
--- a/src/luan/tools/CmdLine.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/tools/CmdLine.java	Sun Dec 23 06:36:56 2012 +0000
@@ -75,7 +75,7 @@
 				LuaFunction fn = BasicLib.load_file(lua,file);
 				lua.call(fn,null,null,varArgs);
 			} catch(LuaException e) {
-				System.err.println(e);
+				System.err.println("error: "+e.getMessage());
 //				e.printStackTrace();
 				System.exit(-1);
 			}