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 wrap: on
line diff
--- a/src/kryshen/tema/FunctionDataParser.java	Thu Mar 26 17:18:31 2009 +0300
+++ b/src/kryshen/tema/FunctionDataParser.java	Tue Apr 21 19:21:16 2009 +0400
@@ -40,19 +40,21 @@
 public class FunctionDataParser {
     static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
     
-    private TemplateParser tp;
-    private DataFormat fd;
-    private TemplateReader in;
-    
+    private final TemplateParser tp;
+    private final DataFormat fd;
+    private final TemplateReader in;
+    private final String name;
+
     private boolean available = true;
     
     private int lastReturnCode = 0;
     
-    FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in)
-    throws IOException, TemplateException {
+    FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in,
+            String name) throws IOException, TemplateException {
         this.tp = tp;
         this.fd = fd;
         this.in = in;
+        this.name = name;
         
         // Check for empty function data.
         checkAvailable();
@@ -188,7 +190,16 @@
     public TemplateReader getTemplateReader() {
         return in;
     }
-    
+
+    public String getName() {
+        return name;
+    }
+
+    public void warning(String message) {
+        System.err.println(in.getSource() + ":" + in.getLineNumber() +
+                ": in " + name + ": " + message);
+    }
+
     public File createFile(String path) {
         File file = new File(path);
         
--- a/src/kryshen/tema/GlobalContext.java	Thu Mar 26 17:18:31 2009 +0300
+++ b/src/kryshen/tema/GlobalContext.java	Tue Apr 21 19:21:16 2009 +0400
@@ -77,6 +77,8 @@
         set("false",      Control.FALSE);
         set("true",       Control.TRUE);
         set("not",        Control.NOT);
+        set("and",        Control.AND);
+        set("or",         Control.OR);
         set("optional",   Control.OPTIONAL);
         set("if",         Control.IF);
         set("if_else",    Control.IF_ELSE);
--- a/src/kryshen/tema/TemplateParser.java	Thu Mar 26 17:18:31 2009 +0300
+++ b/src/kryshen/tema/TemplateParser.java	Tue Apr 21 19:21:16 2009 +0400
@@ -406,7 +406,7 @@
             TemplateReader in, Writer out)
             throws IOException, TemplateException {
         
-        FunctionDataParser fdp = new FunctionDataParser(this, fd, in);
+        FunctionDataParser fdp = new FunctionDataParser(this, fd, in, name);
                 
         int r = invoke(name, fdp, out);                
         
--- a/src/kryshen/tema/functions/Control.java	Thu Mar 26 17:18:31 2009 +0300
+++ b/src/kryshen/tema/functions/Control.java	Tue Apr 21 19:21:16 2009 +0400
@@ -103,7 +103,9 @@
     public static final Function IF = new Function() {
         public int invoke(FunctionDataParser fdp, Writer out)
         throws IOException, TemplateException {
-            
+
+            fdp.warning("deprecated.");
+
             int value = fdp.parseNextArg(NullWriter.INSTANCE);
             
             if (value != 0) {
@@ -117,7 +119,9 @@
     public static final Function IF_ELSE = new Function() {
         public int invoke(FunctionDataParser fdp, Writer out)
         throws IOException, TemplateException {
-            
+
+            fdp.warning("deprecated.");
+
             int value = fdp.parseNextArg(NullWriter.INSTANCE);
             
             if (value != 0) {
@@ -128,7 +132,35 @@
             }
         }
     };
-    
+
+    public static final Function AND = new Function() {
+        public int invoke(FunctionDataParser fdp, Writer out)
+        throws IOException, TemplateException {
+
+            int r = fdp.getTemplateParser().getLastReturnCode();
+
+            if (r != 0) {
+                return fdp.parseData(out);
+            }
+
+            return 0;
+        }
+    };
+
+    public static final Function OR = new Function() {
+        public int invoke(FunctionDataParser fdp, Writer out)
+                throws IOException, TemplateException {
+
+            int r = fdp.getTemplateParser().getLastReturnCode();
+
+            if (r == 0) {
+                return fdp.parseData(out);
+            }
+
+            return r;
+        }
+    };
+
     /**
      * Outputs it's evaluated data while the evaluation result is non-zero.
      */