Author: byron
Date: Tue Feb 10 15:48:29 2009
New Revision: 743001

URL: http://svn.apache.org/viewvc?rev=743001&view=rev
Log:
Move directive parse argument error handling to error handling routine and out 
of the parser.  This allows more flexibility in defining custom directives and 
validating arguments

Modified:
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Directive.java
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Macro.java
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java
    velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt

Modified: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Directive.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Directive.java?rev=743001&r1=743000&r2=743001&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Directive.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Directive.java
 Tue Feb 10 15:48:29 2009
@@ -19,18 +19,19 @@
  * under the License.    
  */
 
-import java.io.Writer;
 import java.io.IOException;
-
-import org.apache.velocity.runtime.RuntimeServices;
+import java.io.Writer;
 
 import org.apache.velocity.context.InternalContextAdapter;
-import org.apache.velocity.runtime.parser.node.Node;
-
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.exception.TemplateInitException;
+import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.parser.ParseException;
+import org.apache.velocity.runtime.parser.ParserTreeConstants;
+import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.parser.node.Node;
 
 
 /**
@@ -131,6 +132,26 @@
     }
 
     /**
+     * The Parser calls this method during template parsing to check the 
arguments
+     * types.  Be aware that this method is called pre init, so not all data
+     * is available in this method.  The default implemenation is to throw a 
+     * parse exception if there is a word argument in any of the argument 
positions.
+     * @param argType type, for example ParserTreeConstants.JJTWORD.
+     * @param argPos argument position, starting with 0
+     * @param t, token of directive
+     * @param templateName, the name of the template this directive is 
referenced in.
+     */
+    public void checkArg(int argType, int argPos, Token t, String templateName)
+      throws ParseException
+    {      
+        if (argType == ParserTreeConstants.JJTWORD)
+        {
+          throw new MacroParseException("Invalid arg #"
+              + argPos + " in directive " + t.image, templateName, t);
+        }
+    }
+    
+    /**
      * How this directive is to be rendered
      * @param context
      * @param writer

Modified: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java?rev=743001&r1=743000&r2=743001&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Foreach.java
 Tue Feb 10 15:48:29 2009
@@ -32,6 +32,9 @@
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.runtime.log.Log;
+import org.apache.velocity.runtime.parser.ParseException;
+import org.apache.velocity.runtime.parser.ParserTreeConstants;
+import org.apache.velocity.runtime.parser.Token;
 import org.apache.velocity.runtime.parser.node.ASTReference;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.node.SimpleNode;
@@ -322,4 +325,19 @@
 
         return true;
     }
+    
+    /**
+     * We do not allow a word token in any other arg position except for the 
2nd since
+     * we are looking for the pattern #foreach($foo in $bar).
+     */
+    public void checkArg(int argType, int argPos, Token t, String templateName)
+      throws ParseException
+    {
+        if (argType == ParserTreeConstants.JJTWORD && argPos != 1)
+        {
+          throw new MacroParseException("Invalid arg #"
+              + argPos + " in directive " + t.image, templateName, t);
+        }
+    }    
+    
 }

Modified: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Macro.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Macro.java?rev=743001&r1=743000&r2=743001&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Macro.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Macro.java
 Tue Feb 10 15:48:29 2009
@@ -236,4 +236,20 @@
         ret.append(" )");
         return ret;
     }
+    
+    /**
+     * We expect the pattern #macro(foo $bar1 ...) so words are not allowed
+     * in the other argument positions.
+     */
+    public void checkArg(int argType, int argPos, Token t, String templateName)
+      throws ParseException
+  {
+      if (argType == ParserTreeConstants.JJTWORD && argPos > 0)
+      {
+          throw new MacroParseException("Invalid first arg"
+            + " in #macro() directive - must be a"
+            + " word token (no \' or \" surrounding)", templateName, t);
+      }
+  }    
+    
 }

Modified: 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java?rev=743001&r1=743000&r2=743001&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java
 Tue Feb 10 15:48:29 2009
@@ -842,38 +842,18 @@
             ;
           }
           argType = DirectiveArg();
-                if (argType == ParserTreeConstants.JJTWORD)
+                if (d != null)
                 {
-                    if (isMacro && argPos == 0)
-                    {
-                        /* if #macro and it's the 0th arg, ok */
-                    }
-                    else if (isVM)
-                    {
-                        {if (true) throw new MacroParseException("Invalid arg 
#"
-                        + argPos + " in VM " + t.image, currentTemplateName, 
t);}
-                    }
-                                          /* if #foreach and it's the 2nd arg, 
ok */
-                    else if (d != null && (!directiveName.equals("foreach") || 
argPos != 1))
-                    {
-                        {if (true) throw new MacroParseException("Invalid arg 
#"
-                        + argPos + " in directive " + t.image, 
currentTemplateName, t);}
-                    }
-                    else
-                    {
-                        /* either schmoo or a late-defined macro,
-                         * VelocimacroProxy will have to check for latter. */
-                    }
+                    // If a directive then call the directive check args method
+                    d.checkArg(argType, argPos, t, currentTemplateName);
+
                 }
-                else
+                else if (argType == ParserTreeConstants.JJTWORD)
                 {
-                    if (isMacro && argPos == 0)
+                    if (isVM)
                     {
-                        /* if a VM and it's the 0th arg, not ok */
-
-                        {if (true) throw new MacroParseException("Invalid 
first arg"
-                        + " in #macro() directive - must be a"
-                        + " word token (no \' or \" surrounding)", 
currentTemplateName, t);}
+                        {if (true) throw new MacroParseException("Invalid arg 
#"
+                        + argPos + " in VM " + t.image, currentTemplateName, 
t);}
                     }
                 }
 
@@ -2931,6 +2911,75 @@
     finally { jj_save(11, xla); }
   }
 
+  final private boolean jj_3R_92() {
+    if (jj_3R_65()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_99() {
+    if (jj_3R_40()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_76() {
+    if (jj_3R_40()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_101() {
+    if (jj_scan_token(COMMA)) return true;
+    if (jj_3R_29()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_91() {
+    Token xsp;
+    xsp = jj_scanpos;
+    if (jj_scan_token(33)) jj_scanpos = xsp;
+    xsp = jj_scanpos;
+    if (jj_3R_92()) {
+    jj_scanpos = xsp;
+    if (jj_3R_93()) {
+    jj_scanpos = xsp;
+    if (jj_3R_94()) {
+    jj_scanpos = xsp;
+    if (jj_3R_95()) {
+    jj_scanpos = xsp;
+    if (jj_3R_96()) return true;
+    }
+    }
+    }
+    }
+    xsp = jj_scanpos;
+    if (jj_scan_token(33)) jj_scanpos = xsp;
+    return false;
+  }
+
+  final private boolean jj_3R_26() {
+    if (jj_3R_40()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_98() {
+    if (jj_3R_24()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_75() {
+    if (jj_3R_24()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_78() {
+    if (jj_3R_29()) return true;
+    Token xsp;
+    while (true) {
+      xsp = jj_scanpos;
+      if (jj_3R_101()) { jj_scanpos = xsp; break; }
+    }
+    return false;
+  }
+
   final private boolean jj_3R_66() {
     if (jj_scan_token(LBRACKET)) return true;
     Token xsp;
@@ -2957,8 +3006,8 @@
     return false;
   }
 
-  final private boolean jj_3R_26() {
-    if (jj_3R_40()) return true;
+  final private boolean jj_3R_25() {
+    if (jj_3R_24()) return true;
     return false;
   }
 
@@ -2971,6 +3020,11 @@
     return false;
   }
 
+  final private boolean jj_3_1() {
+    if (jj_3R_24()) return true;
+    return false;
+  }
+
   final private boolean jj_3R_77() {
     Token xsp;
     xsp = jj_scanpos;
@@ -3011,31 +3065,11 @@
     return false;
   }
 
-  final private boolean jj_3R_25() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
-  final private boolean jj_3_1() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_36() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
   final private boolean jj_3R_50() {
     if (jj_3R_71()) return true;
     return false;
   }
 
-  final private boolean jj_3R_90() {
-    if (jj_3R_73()) return true;
-    return false;
-  }
-
   final private boolean jj_3R_49() {
     if (jj_3R_70()) return true;
     return false;
@@ -3062,8 +3096,8 @@
     return false;
   }
 
-  final private boolean jj_3R_89() {
-    if (jj_3R_73()) return true;
+  final private boolean jj_3R_36() {
+    if (jj_3R_24()) return true;
     return false;
   }
 
@@ -3077,13 +3111,85 @@
     return false;
   }
 
+  final private boolean jj_3R_45() {
+    if (jj_3R_66()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_90() {
+    if (jj_3R_73()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_44() {
+    if (jj_3R_40()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_89() {
+    if (jj_3R_73()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_43() {
+    if (jj_3R_65()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_27() {
+    if (jj_scan_token(COMMA)) return true;
+    Token xsp;
+    xsp = jj_scanpos;
+    if (jj_scan_token(33)) jj_scanpos = xsp;
+    return false;
+  }
+
+  final private boolean jj_3R_42() {
+    if (jj_3R_64()) return true;
+    return false;
+  }
+
   final private boolean jj_3R_34() {
     if (jj_3R_60()) return true;
     return false;
   }
 
-  final private boolean jj_3R_45() {
-    if (jj_3R_66()) return true;
+  final private boolean jj_3R_41() {
+    if (jj_3R_24()) return true;
+    return false;
+  }
+
+  final private boolean jj_3R_28() {
+    Token xsp;
+    xsp = jj_scanpos;
+    if (jj_3R_41()) {
+    jj_scanpos = xsp;
+    if (jj_3R_42()) {
+    jj_scanpos = xsp;
+    if (jj_3R_43()) {
+    jj_scanpos = xsp;
+    if (jj_3R_44()) {
+    jj_scanpos = xsp;
+    if (jj_3R_45()) {
+    jj_scanpos = xsp;
+    if (jj_3R_46()) {
+    jj_scanpos = xsp;
+    if (jj_3R_47()) {
+    jj_scanpos = xsp;
+    if (jj_3R_48()) {
+    jj_scanpos = xsp;
+    if (jj_3R_49()) {
+    jj_scanpos = xsp;
+    if (jj_3R_50()) return true;
+    }
+    }
+    }
+    }
+    }
+    }
+    }
+    }
+    }
     return false;
   }
 
@@ -3108,13 +3214,13 @@
     return false;
   }
 
-  final private boolean jj_3R_44() {
-    if (jj_3R_40()) return true;
+  final private boolean jj_3R_88() {
+    if (jj_scan_token(LPAREN)) return true;
     return false;
   }
 
-  final private boolean jj_3R_88() {
-    if (jj_scan_token(LPAREN)) return true;
+  final private boolean jj_3R_64() {
+    if (jj_scan_token(WORD)) return true;
     return false;
   }
 
@@ -3138,21 +3244,13 @@
     return false;
   }
 
-  final private boolean jj_3R_43() {
-    if (jj_3R_65()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_27() {
-    if (jj_scan_token(COMMA)) return true;
-    Token xsp;
-    xsp = jj_scanpos;
-    if (jj_scan_token(33)) jj_scanpos = xsp;
+  final private boolean jj_3R_83() {
+    if (jj_3R_67()) return true;
     return false;
   }
 
-  final private boolean jj_3R_83() {
-    if (jj_3R_67()) return true;
+  final private boolean jj_3R_60() {
+    if (jj_scan_token(IDENTIFIER)) return true;
     return false;
   }
 
@@ -3166,11 +3264,6 @@
     return false;
   }
 
-  final private boolean jj_3R_42() {
-    if (jj_3R_64()) return true;
-    return false;
-  }
-
   final private boolean jj_3R_80() {
     if (jj_3R_24()) return true;
     return false;
@@ -3181,42 +3274,13 @@
     return false;
   }
 
-  final private boolean jj_3R_41() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_28() {
+  final private boolean jj_3_4() {
     Token xsp;
     xsp = jj_scanpos;
-    if (jj_3R_41()) {
-    jj_scanpos = xsp;
-    if (jj_3R_42()) {
-    jj_scanpos = xsp;
-    if (jj_3R_43()) {
-    jj_scanpos = xsp;
-    if (jj_3R_44()) {
-    jj_scanpos = xsp;
-    if (jj_3R_45()) {
-    jj_scanpos = xsp;
-    if (jj_3R_46()) {
-    jj_scanpos = xsp;
-    if (jj_3R_47()) {
-    jj_scanpos = xsp;
-    if (jj_3R_48()) {
-    jj_scanpos = xsp;
-    if (jj_3R_49()) {
-    jj_scanpos = xsp;
-    if (jj_3R_50()) return true;
-    }
-    }
-    }
-    }
-    }
-    }
-    }
-    }
-    }
+    if (jj_scan_token(33)) jj_scanpos = xsp;
+    xsp = jj_scanpos;
+    if (jj_3R_27()) jj_scanpos = xsp;
+    if (jj_3R_28()) return true;
     return false;
   }
 
@@ -3290,8 +3354,8 @@
     return false;
   }
 
-  final private boolean jj_3R_64() {
-    if (jj_scan_token(WORD)) return true;
+  final private boolean jj_3R_65() {
+    if (jj_scan_token(STRING_LITERAL)) return true;
     return false;
   }
 
@@ -3305,8 +3369,8 @@
     return false;
   }
 
-  final private boolean jj_3R_60() {
-    if (jj_scan_token(IDENTIFIER)) return true;
+  final private boolean jj_3R_40() {
+    if (jj_scan_token(INTEGER_LITERAL)) return true;
     return false;
   }
 
@@ -3321,18 +3385,13 @@
     return false;
   }
 
-  final private boolean jj_3R_63() {
-    if (jj_3R_73()) return true;
+  final private boolean jj_3R_67() {
+    if (jj_scan_token(FLOATING_POINT_LITERAL)) return true;
     return false;
   }
 
-  final private boolean jj_3_4() {
-    Token xsp;
-    xsp = jj_scanpos;
-    if (jj_scan_token(33)) jj_scanpos = xsp;
-    xsp = jj_scanpos;
-    if (jj_3R_27()) jj_scanpos = xsp;
-    if (jj_3R_28()) return true;
+  final private boolean jj_3R_63() {
+    if (jj_3R_73()) return true;
     return false;
   }
 
@@ -3361,11 +3420,6 @@
     return false;
   }
 
-  final private boolean jj_3R_65() {
-    if (jj_scan_token(STRING_LITERAL)) return true;
-    return false;
-  }
-
   final private boolean jj_3_7() {
     if (jj_scan_token(DOT)) return true;
     Token xsp;
@@ -3407,11 +3461,6 @@
     return false;
   }
 
-  final private boolean jj_3R_40() {
-    if (jj_scan_token(INTEGER_LITERAL)) return true;
-    return false;
-  }
-
   final private boolean jj_3R_38() {
     if (jj_scan_token(IDENTIFIER)) return true;
     Token xsp;
@@ -3426,11 +3475,6 @@
     return false;
   }
 
-  final private boolean jj_3R_67() {
-    if (jj_scan_token(FLOATING_POINT_LITERAL)) return true;
-    return false;
-  }
-
   final private boolean jj_3R_24() {
     Token xsp;
     xsp = jj_scanpos;
@@ -3519,6 +3563,11 @@
     return false;
   }
 
+  final private boolean jj_3_2() {
+    if (jj_scan_token(DOUBLE_ESCAPE)) return true;
+    return false;
+  }
+
   final private boolean jj_3R_29() {
     Token xsp;
     xsp = jj_scanpos;
@@ -3582,75 +3631,6 @@
     return false;
   }
 
-  final private boolean jj_3R_92() {
-    if (jj_3R_65()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_99() {
-    if (jj_3R_40()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_76() {
-    if (jj_3R_40()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_101() {
-    if (jj_scan_token(COMMA)) return true;
-    if (jj_3R_29()) return true;
-    return false;
-  }
-
-  final private boolean jj_3_2() {
-    if (jj_scan_token(DOUBLE_ESCAPE)) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_91() {
-    Token xsp;
-    xsp = jj_scanpos;
-    if (jj_scan_token(33)) jj_scanpos = xsp;
-    xsp = jj_scanpos;
-    if (jj_3R_92()) {
-    jj_scanpos = xsp;
-    if (jj_3R_93()) {
-    jj_scanpos = xsp;
-    if (jj_3R_94()) {
-    jj_scanpos = xsp;
-    if (jj_3R_95()) {
-    jj_scanpos = xsp;
-    if (jj_3R_96()) return true;
-    }
-    }
-    }
-    }
-    xsp = jj_scanpos;
-    if (jj_scan_token(33)) jj_scanpos = xsp;
-    return false;
-  }
-
-  final private boolean jj_3R_98() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_75() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_78() {
-    if (jj_3R_29()) return true;
-    Token xsp;
-    while (true) {
-      xsp = jj_scanpos;
-      if (jj_3R_101()) { jj_scanpos = xsp; break; }
-    }
-    return false;
-  }
-
   public ParserTokenManager token_source;
   public Token token, jj_nt;
   private int jj_ntk;

Modified: velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt?rev=743001&r1=743000&r2=743001&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt (original)
+++ velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt Tue Feb 10 15:48:29 
2009
@@ -1568,12 +1568,8 @@
                 /*
                  *  if null, then not a real directive, but maybe a Velocimacro
                  */
-
                 isVM = rsvc.isVelocimacro(directiveName, currentTemplateName);
 
-                /*
-                 * Currently, all VMs are LINE directives
-                 */
                 directiveType = Directive.LINE;
             }
         }
@@ -1603,38 +1599,18 @@
 
             argType = DirectiveArg()
             {
-                if (argType == ParserTreeConstants.JJTWORD)
+                if (d != null)
                 {
-                    if (isMacro && argPos == 0)
-                    {
-                        /* if #macro and it's the 0th arg, ok */
-                    }
-                    else if (isVM)
-                    {
-                        throw new MacroParseException("Invalid arg #"
-                        + argPos + " in VM " + t.image, currentTemplateName, 
t);
-                    }
-                                          /* if #foreach and it's the 2nd arg, 
ok */
-                    else if (d != null && (!directiveName.equals("foreach") || 
argPos != 1))
-                    {
-                        throw new MacroParseException("Invalid arg #"
-                        + argPos + " in directive " + t.image, 
currentTemplateName, t);
-                    }
-                    else
-                    {
-                        /* either schmoo or a late-defined macro,
-                         * VelocimacroProxy will have to check for latter. */
-                    }
+                    // If a directive then call the directive check args method
+                    d.checkArg(argType, argPos, t, currentTemplateName);
+                    
                 }
-                else
+                else if (argType == ParserTreeConstants.JJTWORD)
                 {
-                    if (isMacro && argPos == 0)
+                    if (isVM)
                     {
-                        /* if a VM and it's the 0th arg, not ok */
-
-                        throw new MacroParseException("Invalid first arg"
-                        + " in #macro() directive - must be a"
-                        + " word token (no \' or \" surrounding)", 
currentTemplateName, t);
+                        throw new MacroParseException("Invalid arg #"
+                        + argPos + " in VM " + t.image, currentTemplateName, 
t);
                     }
                 }
 


Reply via email to