changeset 593:92c9fa5e39e6

remove theme "Get" and "Set", and add "define"
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Aug 2015 10:16:57 -0600
parents 1c64b1fd882b
children e91e476186c7
files core/src/luan/impl/ThemeParser.java
diffstat 1 files changed, 31 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/ThemeParser.java	Mon Aug 31 08:03:58 2015 -0600
+++ b/core/src/luan/impl/ThemeParser.java	Mon Aug 31 10:16:57 2015 -0600
@@ -178,43 +178,27 @@
 
 	private Stmt parseDef() throws ParseException {
 		int start = parser.begin();
+		if( !parser.match("{define:") )
+			return parser.failure(null);
 		Tag tag = parseBlockTag();
 		if( tag == null )
 			return parser.failure(null);
 		parser.match('\r');  parser.match('\n');  // ignore newline
-		Stmt rtn = null;
-		if( tag.name.equals("Set") ) {
-			String name = tag.attrs.remove("name");
-			if( name == null )
-				throw exception("block:Set missing required attribute 'name'");
-			if( !tag.attrs.isEmpty() )
-				throw exception("block:Set has unrecognized attributes: "+tag.attrs);
-			if( !validateName(name) )
-				throw exception("invalid Set name: "+name);
-			addSymbol( name );
-			frame = new Frame(frame);
-			addSymbol(ENV);
-			Stmt block = parseBody(tag);
-			FnDef fnDef = newFnDef(start,block);
-			frame = frame.parent;
-			rtn = new SetStmt( new SetLocalVar(symbolsSize()-1), fnDef );
-		} else {
-			if( !tag.attrs.isEmpty() )
-				throw exception("this block should have no attributes");
-			Expr table = new GetLocalVar(null,stackIndex(MOD));
-			Settable fnName = new SetTableEntry(se(start),table,new ConstExpr(null,tag.name));
-			frame = new Frame(frame);
-			addSymbol(ENV);
-			Stmt block = parseBody(tag);
-			FnDef fnDef = newFnDef(start,block);
-			frame = frame.parent;
-			rtn = new SetStmt(fnName,fnDef);
-		}
+		if( !tag.attrs.isEmpty() )
+			throw exception("this block should have no attributes");
+		Expr table = new GetLocalVar(null,stackIndex(MOD));
+		Settable fnName = new SetTableEntry(se(start),table,new ConstExpr(null,tag.name));
+		frame = new Frame(frame);
+		addSymbol(ENV);
+		Stmt block = parseBody("define",tag);
+		FnDef fnDef = newFnDef(start,block);
+		frame = frame.parent;
+		Stmt rtn = new SetStmt(fnName,fnDef);
 		return parser.success(rtn);
 	}
 
-	private Stmt parseBody(Tag tag) throws ParseException {
-		String endTag = "{/block:" + tag.name + "}";
+	private Stmt parseBody(String type,Tag tag) throws ParseException {
+		String endTag = "{/"+type+":" + tag.name + "}";
 		List<Stmt> stmts = new ArrayList<Stmt>();
 		int stackStart = symbolsSize();
 		StringBuilder sb = new StringBuilder();
@@ -257,6 +241,8 @@
 
 	private Stmt parseBlock() throws ParseException {
 		int start = parser.begin();
+		if( !parser.match("{block:") )
+			return parser.failure(null);
 		Tag tag = parseBlockTag();
 		if( tag == null )
 			return parser.failure(null);
@@ -264,15 +250,15 @@
 			throw exception("block:Set not allowed here");
 		frame = new Frame(frame);
 		addSymbol(ENV);
-		Stmt block = parseBody(tag);
+		Stmt block = parseBody("block",tag);
 		FnDef fnDef = newFnDef(start,block);
 		frame = frame.parent;
 //		String rtn = "<% env." + tag.name + "(" + (tag.attrs.isEmpty() ? "nil" : table(tag.attrs)) + ",env,function(env) %>" + block + "<% end) %>";
 		Expr env = env();
 		Expr fn = new IndexExpr( se(start,"block:"+tag.name), env, new ConstExpr(null,tag.name) );
 		List<Expressions> args = new ArrayList<Expressions>();
+		args.add( env );
 		args.add( tag.attrs.isEmpty() ? new ConstExpr(null,null) : table(tag.attrs) );
-		args.add( env );
 		args.add( fnDef );
 		FnCall fnCall = new FnCall( se(start), fn, ExpList.build(args) );
 		Stmt rtn = new ExpressionsStmt(fnCall);
@@ -280,17 +266,14 @@
 	}
 
 	private Tag parseBlockTag() throws ParseException {
-		parser.begin();
-		if( !parser.match("{block:") )
-			return parser.failure(null);
 		String name = parseName();
 		if( name==null )
 			throw exception("invalid block name");
 		Map<String,String> attrs = parseAttrs();
 		if( !parser.match("}") )
-			return parser.failure(null);
+			return null;
 		Tag tag = new Tag(name,attrs);
-		return parser.success(tag);
+		return tag;
 	}
 
 	private Stmt parseSimpleTag() throws ParseException {
@@ -303,27 +286,14 @@
 		Map<String,String> attrs = parseAttrs();
 		if( !parser.match("}") )
 			return parser.failure(null);
-		FnCall fnCall;
-		if( name.equals("Get") ) {
-			name = attrs.remove("name");
-			if( name == null )
-				throw exception("Get missing required attribute 'name'");
-			if( !attrs.isEmpty() )
-				throw exception("Get has unrecognized attributes: "+attrs);
-			if( !validateName(name) )
-				throw exception("invalid Get name: "+name);
-//			rtn = "<% " + name + "(env) %>";
-			int index = upValueIndex(name);
-			if( index == -1 )
-				throw exception("name '"+name+"' not defined");
-			Expr fn = new GetUpVar(se(start,name),index);
-			fnCall = new FnCall( se(start), fn, env() );
-		} else {
-//			rtn = "<% env." + name + (attrs.isEmpty() ? "()" : table(attrs)) + " %>";
-			Expr fn = new IndexExpr( se(start,name), env(), new ConstExpr(null,name) );
-			Expressions args = attrs.isEmpty() ? ExpList.emptyExpList : table(attrs);
-			fnCall = new FnCall( se(start), fn, args );
-		}
+//		rtn = "<% env." + name + (attrs.isEmpty() ? "()" : table(attrs)) + " %>";
+		Expr env = env();
+		Expr fn = new IndexExpr( se(start,name), env, new ConstExpr(null,name) );
+		List<Expressions> args = new ArrayList<Expressions>();
+		args.add( env );
+		if( !attrs.isEmpty() )
+			args.add( table(attrs) );
+		FnCall fnCall = new FnCall( se(start), fn, ExpList.build(args) );
 		Stmt rtn = new ExpressionsStmt(fnCall);
 		return parser.success(rtn);
 	}
@@ -371,23 +341,15 @@
 	}
 
 	private String parseName() {
-		return parseName(parser);
-	}
-
-	private static boolean validateName(String name) {
-		return name.equals(parseName(new Parser(new LuanSource("NAME",name))));
-	}
-
-	private static String parseName(Parser parser) {
 		int start = parser.begin();
-		if( !NameChar(parser) )
+		if( !NameChar() )
 			return parser.failure(null);
-		while( NameChar(parser) );
+		while( NameChar() );
 		String match = parser.textFrom(start);
 		return parser.success(match);
 	}
 
-	private static boolean NameChar(Parser parser) {
+	private boolean NameChar() {
 		return parser.inCharRange('a', 'z') || parser.inCharRange('A', 'Z')
 			|| parser.inCharRange('0', '9') || parser.anyOf("-_");
 	}