Mercurial > hg > tema
changeset 19:7b11f5174e29
New functions: "and", "or". Deprecated: "if", "if_else".
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Tue, 21 Apr 2009 19:21:16 +0400 |
parents | d53cd4995bd4 |
children | fe2a094f4509 |
files | src/kryshen/tema/FunctionDataParser.java src/kryshen/tema/GlobalContext.java src/kryshen/tema/TemplateParser.java src/kryshen/tema/functions/Control.java |
diffstat | 4 files changed, 56 insertions(+), 11 deletions(-) [+] |
line diff
1.1 --- a/src/kryshen/tema/FunctionDataParser.java Thu Mar 26 17:18:31 2009 +0300 1.2 +++ b/src/kryshen/tema/FunctionDataParser.java Tue Apr 21 19:21:16 2009 +0400 1.3 @@ -40,19 +40,21 @@ 1.4 public class FunctionDataParser { 1.5 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS; 1.6 1.7 - private TemplateParser tp; 1.8 - private DataFormat fd; 1.9 - private TemplateReader in; 1.10 - 1.11 + private final TemplateParser tp; 1.12 + private final DataFormat fd; 1.13 + private final TemplateReader in; 1.14 + private final String name; 1.15 + 1.16 private boolean available = true; 1.17 1.18 private int lastReturnCode = 0; 1.19 1.20 - FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in) 1.21 - throws IOException, TemplateException { 1.22 + FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in, 1.23 + String name) throws IOException, TemplateException { 1.24 this.tp = tp; 1.25 this.fd = fd; 1.26 this.in = in; 1.27 + this.name = name; 1.28 1.29 // Check for empty function data. 1.30 checkAvailable(); 1.31 @@ -188,7 +190,16 @@ 1.32 public TemplateReader getTemplateReader() { 1.33 return in; 1.34 } 1.35 - 1.36 + 1.37 + public String getName() { 1.38 + return name; 1.39 + } 1.40 + 1.41 + public void warning(String message) { 1.42 + System.err.println(in.getSource() + ":" + in.getLineNumber() + 1.43 + ": in " + name + ": " + message); 1.44 + } 1.45 + 1.46 public File createFile(String path) { 1.47 File file = new File(path); 1.48
2.1 --- a/src/kryshen/tema/GlobalContext.java Thu Mar 26 17:18:31 2009 +0300 2.2 +++ b/src/kryshen/tema/GlobalContext.java Tue Apr 21 19:21:16 2009 +0400 2.3 @@ -77,6 +77,8 @@ 2.4 set("false", Control.FALSE); 2.5 set("true", Control.TRUE); 2.6 set("not", Control.NOT); 2.7 + set("and", Control.AND); 2.8 + set("or", Control.OR); 2.9 set("optional", Control.OPTIONAL); 2.10 set("if", Control.IF); 2.11 set("if_else", Control.IF_ELSE);
3.1 --- a/src/kryshen/tema/TemplateParser.java Thu Mar 26 17:18:31 2009 +0300 3.2 +++ b/src/kryshen/tema/TemplateParser.java Tue Apr 21 19:21:16 2009 +0400 3.3 @@ -406,7 +406,7 @@ 3.4 TemplateReader in, Writer out) 3.5 throws IOException, TemplateException { 3.6 3.7 - FunctionDataParser fdp = new FunctionDataParser(this, fd, in); 3.8 + FunctionDataParser fdp = new FunctionDataParser(this, fd, in, name); 3.9 3.10 int r = invoke(name, fdp, out); 3.11
4.1 --- a/src/kryshen/tema/functions/Control.java Thu Mar 26 17:18:31 2009 +0300 4.2 +++ b/src/kryshen/tema/functions/Control.java Tue Apr 21 19:21:16 2009 +0400 4.3 @@ -103,7 +103,9 @@ 4.4 public static final Function IF = new Function() { 4.5 public int invoke(FunctionDataParser fdp, Writer out) 4.6 throws IOException, TemplateException { 4.7 - 4.8 + 4.9 + fdp.warning("deprecated."); 4.10 + 4.11 int value = fdp.parseNextArg(NullWriter.INSTANCE); 4.12 4.13 if (value != 0) { 4.14 @@ -117,7 +119,9 @@ 4.15 public static final Function IF_ELSE = new Function() { 4.16 public int invoke(FunctionDataParser fdp, Writer out) 4.17 throws IOException, TemplateException { 4.18 - 4.19 + 4.20 + fdp.warning("deprecated."); 4.21 + 4.22 int value = fdp.parseNextArg(NullWriter.INSTANCE); 4.23 4.24 if (value != 0) { 4.25 @@ -128,7 +132,35 @@ 4.26 } 4.27 } 4.28 }; 4.29 - 4.30 + 4.31 + public static final Function AND = new Function() { 4.32 + public int invoke(FunctionDataParser fdp, Writer out) 4.33 + throws IOException, TemplateException { 4.34 + 4.35 + int r = fdp.getTemplateParser().getLastReturnCode(); 4.36 + 4.37 + if (r != 0) { 4.38 + return fdp.parseData(out); 4.39 + } 4.40 + 4.41 + return 0; 4.42 + } 4.43 + }; 4.44 + 4.45 + public static final Function OR = new Function() { 4.46 + public int invoke(FunctionDataParser fdp, Writer out) 4.47 + throws IOException, TemplateException { 4.48 + 4.49 + int r = fdp.getTemplateParser().getLastReturnCode(); 4.50 + 4.51 + if (r == 0) { 4.52 + return fdp.parseData(out); 4.53 + } 4.54 + 4.55 + return r; 4.56 + } 4.57 + }; 4.58 + 4.59 /** 4.60 * Outputs it's evaluated data while the evaluation result is non-zero. 4.61 */