comparison core/src/luan/impl/ThemeParser.java @ 595:8370c4009cce

remove theme attributes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Sep 2015 14:20:52 -0600
parents e91e476186c7
children b926e53910dd
comparison
equal deleted inserted replaced
594:e91e476186c7 595:8370c4009cce
180 if( !parser.match("{define:") ) 180 if( !parser.match("{define:") )
181 return parser.failure(null); 181 return parser.failure(null);
182 String name = parseName(); 182 String name = parseName();
183 if( name==null ) 183 if( name==null )
184 throw exception("invalid block name"); 184 throw exception("invalid block name");
185 Map<String,String> attrs = parseAttrs();
186 String spaces = ""; 185 String spaces = "";
187 if( BlankLine() ) { 186 if( BlankLine() ) {
188 while( BlankLine() ); 187 while( BlankLine() );
189 int startSpaces = parser.currentIndex(); 188 int startSpaces = parser.currentIndex();
190 InlineSpaces(); 189 InlineSpaces();
191 spaces = parser.textFrom(startSpaces); 190 spaces = parser.textFrom(startSpaces);
192 } 191 }
193 if( !parser.match("}") ) 192 if( !parser.match("}") )
194 return null; 193 return null;
195 if( !attrs.isEmpty() )
196 throw exception("this block should have no attributes");
197 Expr table = new GetLocalVar(null,stackIndex(MOD)); 194 Expr table = new GetLocalVar(null,stackIndex(MOD));
198 Settable fnName = new SetTableEntry(se(start),table,new ConstExpr(null,name)); 195 Settable fnName = new SetTableEntry(se(start),table,new ConstExpr(null,name));
199 frame = new Frame(frame); 196 frame = new Frame(frame);
200 addSymbol(ENV); 197 addSymbol(ENV);
201 Stmt block = parseBody("define:"+name,spaces,EndOfLine()); 198 Stmt block = parseBody("define:"+name,spaces,EndOfLine());
317 if( !parser.match("{block:") ) 314 if( !parser.match("{block:") )
318 return parser.failure(null); 315 return parser.failure(null);
319 String name = parseName(); 316 String name = parseName();
320 if( name==null ) 317 if( name==null )
321 throw exception("invalid block name"); 318 throw exception("invalid block name");
322 Map<String,String> attrs = parseAttrs();
323 if( !parser.match("}") ) 319 if( !parser.match("}") )
324 return null; 320 return null;
325 frame = new Frame(frame); 321 frame = new Frame(frame);
326 addSymbol(ENV); 322 addSymbol(ENV);
327 Stmt block = parseBody("block:"+name,spaces,false); 323 Stmt block = parseBody("block:"+name,spaces,false);
330 // String rtn = "<% env." + tag.name + "(" + (tag.attrs.isEmpty() ? "nil" : table(tag.attrs)) + ",env,function(env) %>" + block + "<% end) %>"; 326 // String rtn = "<% env." + tag.name + "(" + (tag.attrs.isEmpty() ? "nil" : table(tag.attrs)) + ",env,function(env) %>" + block + "<% end) %>";
331 Expr env = env(); 327 Expr env = env();
332 Expr fn = new IndexExpr( se(start,"block:"+name), env, new ConstExpr(null,name) ); 328 Expr fn = new IndexExpr( se(start,"block:"+name), env, new ConstExpr(null,name) );
333 List<Expressions> args = new ArrayList<Expressions>(); 329 List<Expressions> args = new ArrayList<Expressions>();
334 args.add( env ); 330 args.add( env );
335 args.add( attrs.isEmpty() ? new ConstExpr(null,null) : table(attrs) );
336 args.add( fnDef ); 331 args.add( fnDef );
337 FnCall fnCall = new FnCall( se(start), fn, ExpList.build(args) ); 332 FnCall fnCall = new FnCall( se(start), fn, ExpList.build(args) );
338 Stmt rtn = new ExpressionsStmt(fnCall); 333 Stmt rtn = new ExpressionsStmt(fnCall);
339 return parser.success(rtn); 334 return parser.success(rtn);
340 } 335 }
344 if( !parser.match("{") ) 339 if( !parser.match("{") )
345 return parser.failure(null); 340 return parser.failure(null);
346 String name = parseName(); 341 String name = parseName();
347 if( name==null ) 342 if( name==null )
348 return parser.failure(null); 343 return parser.failure(null);
349 Map<String,String> attrs = parseAttrs();
350 Spaces(); 344 Spaces();
351 if( !parser.match("}") ) 345 if( !parser.match("}") )
352 return parser.failure(null); 346 return parser.failure(null);
353 // rtn = "<% env." + name + (attrs.isEmpty() ? "()" : table(attrs)) + " %>"; 347 // rtn = "<% env." + name + (attrs.isEmpty() ? "()" : table(attrs)) + " %>";
354 Expr env = env(); 348 Expr env = env();
368 tbl.rawPut(INDENT,newIndent); 362 tbl.rawPut(INDENT,newIndent);
369 return tbl; 363 return tbl;
370 } 364 }
371 }; 365 };
372 } 366 }
373 List<Expressions> args = new ArrayList<Expressions>(); 367 FnCall fnCall = new FnCall( se(start), fn, env );
374 args.add( env );
375 if( !attrs.isEmpty() )
376 args.add( table(attrs) );
377 FnCall fnCall = new FnCall( se(start), fn, ExpList.build(args) );
378 Stmt rtn = new ExpressionsStmt(fnCall); 368 Stmt rtn = new ExpressionsStmt(fnCall);
379 return parser.success(rtn); 369 return parser.success(rtn);
380 }
381
382 private TableExpr table(Map<String,String> attrs) {
383 List<TableExpr.Field> fields = new ArrayList<TableExpr.Field>();
384 for( Map.Entry<String,String> entry : attrs.entrySet() ) {
385 ConstExpr key = new ConstExpr(null,entry.getKey());
386 ConstExpr value = new ConstExpr(null,entry.getValue());
387 fields.add( new TableExpr.Field(key,value) );
388 }
389 return new TableExpr( null, fields.toArray(new TableExpr.Field[0]), ExpList.emptyExpList );
390 }
391
392 private Map<String,String> parseAttrs() {
393 Map<String,String> attrs = new HashMap<String,String>();
394 while( parseAttr(attrs) );
395 return attrs;
396 }
397
398 private boolean parseAttr(Map<String,String> attrs) {
399 parser.begin();
400 Spaces();
401 String name = parseName();
402 if( name==null )
403 return parser.failure();
404 Spaces();
405 if( !parser.match('=') )
406 return parser.failure();
407 Spaces();
408 if( !parser.match('"') )
409 return parser.failure();
410 int start = parser.currentIndex();
411 while( parser.noneOf("\"}") );
412 String val = parser.textFrom(start);
413 if( !parser.match('"') )
414 return parser.failure();
415 attrs.put(name,val);
416 return parser.success();
417 } 370 }
418 371
419 private void Spaces() { 372 private void Spaces() {
420 while( parser.anyOf(" \t\r\n") ); 373 while( parser.anyOf(" \t\r\n") );
421 } 374 }