Author: byron
Date: Sun Feb  8 11:50:43 2009
New Revision: 742067

URL: http://svn.apache.org/viewvc?rev=742067&view=rev
Log:
VELOCITY-690 Allow block directives to not contain parenthesis

Modified:
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/Parser.java
    
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java
    velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt
    
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BlockMacroTestCase.java
    
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/StrictEscapeTestCase.java

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=742067&r1=742066&r2=742067&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
 Sun Feb  8 11:50:43 2009
@@ -723,7 +723,7 @@
     Directive d;
     int directiveType;
     boolean isVM = false;
-    boolean doItNow = false;
+    boolean isMacro = false;
     try {
       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
       case WORD:
@@ -757,7 +757,7 @@
 
         if (directiveName.equals("macro"))
         {
-             doItNow = true;
+             isMacro = true;
         }
 
         /*
@@ -844,7 +844,7 @@
           argType = DirectiveArg();
                 if (argType == ParserTreeConstants.JJTWORD)
                 {
-                    if (doItNow && argPos == 0)
+                    if (isMacro && argPos == 0)
                     {
                         /* if #macro and it's the 0th arg, ok */
                     }
@@ -867,7 +867,7 @@
                 }
                 else
                 {
-                    if (doItNow && argPos == 0)
+                    if (isMacro && argPos == 0)
                     {
                         /* if a VM and it's the 0th arg, not ok */
 
@@ -893,20 +893,22 @@
             {if (true) return jjtn000;}
         }
       } else {
-        if (doItNow)  // doItNow is true if the directive is "macro"
+        if (isMacro)  // isMacro is true if the directive is "macro"
         {
             // VELOCITY-667 We get here if we have a "#macro" construct
-            // without parenthesis which is a parse error
+                  // without parenthesis which is a parse error
             {if (true) throw new MacroParseException("A macro declaration 
requires at least a name argument"
               , currentTemplateName, t);}
         }
 
-        /**
-         * Not a directive
-         */
         token_source.stateStackPop();
         token_source.inDirective = false;
-        {if (true) return jjtn000;}
+
+        if (directiveType != Directive.BLOCK)
+        {
+            // not a directive
+            {if (true) return jjtn000;}
+        }
       }
       ASTBlock jjtn001 = new ASTBlock(this, JJTBLOCK);
       boolean jjtc001 = true;
@@ -978,7 +980,7 @@
          *     we don't have to worry about forward references and such...
          */
 
-        if (doItNow)
+        if (isMacro)
         {
             // Further checking of macro arguments
             Macro.checkArgs(rsvc, t, jjtn000, currentTemplateName);
@@ -2929,26 +2931,6 @@
     finally { jj_save(11, xla); }
   }
 
-  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;
@@ -3013,11 +2995,6 @@
     return false;
   }
 
-  final private boolean jj_3R_25() {
-    if (jj_3R_24()) return true;
-    return false;
-  }
-
   final private boolean jj_3R_68() {
     if (jj_scan_token(LEFT_CURLEY)) return true;
     Token xsp;
@@ -3034,6 +3011,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;
@@ -3095,13 +3077,13 @@
     return false;
   }
 
-  final private boolean jj_3R_45() {
-    if (jj_3R_66()) return true;
+  final private boolean jj_3R_34() {
+    if (jj_3R_60()) return true;
     return false;
   }
 
-  final private boolean jj_3R_34() {
-    if (jj_3R_60()) return true;
+  final private boolean jj_3R_45() {
+    if (jj_3R_66()) return true;
     return false;
   }
 
@@ -3146,16 +3128,13 @@
     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_85() {
+    if (jj_3R_69()) return true;
     return false;
   }
 
-  final private boolean jj_3R_85() {
-    if (jj_3R_69()) return true;
+  final private boolean jj_3R_84() {
+    if (jj_3R_68()) return true;
     return false;
   }
 
@@ -3164,8 +3143,11 @@
     return false;
   }
 
-  final private boolean jj_3R_84() {
-    if (jj_3R_68()) return true;
+  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;
   }
 
@@ -3179,13 +3161,13 @@
     return false;
   }
 
-  final private boolean jj_3R_42() {
-    if (jj_3R_64()) return true;
+  final private boolean jj_3R_81() {
+    if (jj_3R_40()) return true;
     return false;
   }
 
-  final private boolean jj_3R_81() {
-    if (jj_3R_40()) return true;
+  final private boolean jj_3R_42() {
+    if (jj_3R_64()) return true;
     return false;
   }
 
@@ -3303,13 +3285,13 @@
     return false;
   }
 
-  final private boolean jj_3R_64() {
-    if (jj_scan_token(WORD)) return true;
+  final private boolean jj_3R_31() {
+    if (jj_3R_40()) return true;
     return false;
   }
 
-  final private boolean jj_3R_31() {
-    if (jj_3R_40()) return true;
+  final private boolean jj_3R_64() {
+    if (jj_scan_token(WORD)) return true;
     return false;
   }
 
@@ -3339,6 +3321,11 @@
     return false;
   }
 
+  final private boolean jj_3R_63() {
+    if (jj_3R_73()) return true;
+    return false;
+  }
+
   final private boolean jj_3_4() {
     Token xsp;
     xsp = jj_scanpos;
@@ -3349,11 +3336,6 @@
     return false;
   }
 
-  final private boolean jj_3R_63() {
-    if (jj_3R_73()) return true;
-    return false;
-  }
-
   final private boolean jj_3R_30() {
     if (jj_3R_24()) return true;
     return false;
@@ -3649,6 +3631,26 @@
     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/java/org/apache/velocity/runtime/parser/ParserTokenManager.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java?rev=742067&r1=742066&r2=742067&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java
 Sun Feb  8 11:50:43 2009
@@ -941,23 +941,29 @@
                      jjAddStates(45, 46);
                   break;
                case 62:
-               case 63:
                   if ((0x7fffffe87ffffffL & l) == 0L)
                      break;
                   if (kind > 64)
                      kind = 64;
                   jjCheckNAdd(63);
                   break;
+               case 63:
+                  if ((0x7fffffe87fffffeL & l) == 0L)
+                     break;
+                  if (kind > 64)
+                     kind = 64;
+                  jjCheckNAdd(63);
+                  break;
                case 64:
                   if (curChar == 123)
                      jjstateSet[jjnewStateCnt++] = 65;
                   break;
                case 65:
-                  if ((0x7fffffe87fffffeL & l) != 0L)
+                  if ((0x7fffffe87ffffffL & l) != 0L)
                      jjCheckNAddTwoStates(66, 67);
                   break;
                case 66:
-                  if ((0x7fffffe87ffffffL & l) != 0L)
+                  if ((0x7fffffe87fffffeL & l) != 0L)
                      jjCheckNAddTwoStates(66, 67);
                   break;
                case 67:
@@ -1434,23 +1440,29 @@
                      jjAddStates(89, 90);
                   break;
                case 15:
-               case 16:
                   if ((0x7fffffe87ffffffL & l) == 0L)
                      break;
                   if (kind > 64)
                      kind = 64;
                   jjCheckNAdd(16);
                   break;
+               case 16:
+                  if ((0x7fffffe87fffffeL & l) == 0L)
+                     break;
+                  if (kind > 64)
+                     kind = 64;
+                  jjCheckNAdd(16);
+                  break;
                case 17:
                   if (curChar == 123)
                      jjstateSet[jjnewStateCnt++] = 18;
                   break;
                case 18:
-                  if ((0x7fffffe87fffffeL & l) != 0L)
+                  if ((0x7fffffe87ffffffL & l) != 0L)
                      jjCheckNAddTwoStates(19, 20);
                   break;
                case 19:
-                  if ((0x7fffffe87ffffffL & l) != 0L)
+                  if ((0x7fffffe87fffffeL & l) != 0L)
                      jjCheckNAddTwoStates(19, 20);
                   break;
                case 20:
@@ -2431,23 +2443,29 @@
                      jjAddStates(129, 130);
                   break;
                case 9:
-               case 10:
                   if ((0x7fffffe87ffffffL & l) == 0L)
                      break;
                   if (kind > 13)
                      kind = 13;
                   jjCheckNAdd(10);
                   break;
+               case 10:
+                  if ((0x7fffffe87fffffeL & l) == 0L)
+                     break;
+                  if (kind > 13)
+                     kind = 13;
+                  jjCheckNAdd(10);
+                  break;
                case 11:
                   if (curChar == 123)
                      jjstateSet[jjnewStateCnt++] = 12;
                   break;
                case 12:
-                  if ((0x7fffffe87fffffeL & l) != 0L)
+                  if ((0x7fffffe87ffffffL & l) != 0L)
                      jjCheckNAddTwoStates(13, 14);
                   break;
                case 13:
-                  if ((0x7fffffe87ffffffL & l) != 0L)
+                  if ((0x7fffffe87fffffeL & l) != 0L)
                      jjCheckNAddTwoStates(13, 14);
                   break;
                case 14:

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=742067&r1=742066&r2=742067&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt (original)
+++ velocity/engine/branches/2.0_Exp/src/parser/Parser.jjt Sun Feb  8 11:50:43 
2009
@@ -1178,19 +1178,23 @@
 
 }
 
+/**
+ *  TODO, the "@" symbol for block macros to be correct really should prefix 
WORD
+ *  and BRACKETED_WORD, e.g., <WORD ["@"] ( <LETTER... etc...
+ *  However, having the conditional character at the beginning screws up
+ *  Macro parse.  As it is now you can have #...@1234 defined as a macro 
+ *  Which is not correct.
+ */
+
 <PRE_DIRECTIVE,DIRECTIVE>
 TOKEN:
 {
     <#LETTER: [ "a"-"z", "A" - "Z" ] >
-|   <#DIRECTIVE_CHAR: [ "a"-"z", "A"-"Z", "0"-"9", "_", "@" ] >
-|   <WORD:  ( <LETTER> | ["_"] | ["@"]) (<DIRECTIVE_CHAR>)* >
-|   <BRACKETED_WORD:  "{" ( <LETTER> | ["_"]) (<DIRECTIVE_CHAR>)* "}" >
-
+|   <#DIRECTIVE_CHAR: [ "a"-"z", "A"-"Z", "0"-"9", "_" ] >
+|   <WORD: ( <LETTER> | ["_"] | ["@"]) (<DIRECTIVE_CHAR>)* >
+|   <BRACKETED_WORD: "{" ( <LETTER> | ["_"] | ["@"]) (<DIRECTIVE_CHAR>)* "}" >
 }
 
-
-
-
 /* -----------------------------------------------------------------------
  *
  *  REFERENCE Lexical States
@@ -1512,7 +1516,7 @@
     Directive d;
     int directiveType;
     boolean isVM = false;
-    boolean doItNow = false;
+    boolean isMacro = false;
 }
 {
 
@@ -1520,7 +1524,6 @@
      * note that if we were escaped, that is now handled by
      * EscapedDirective()
      */
-
     ((t = <WORD>) | (t = <BRACKETED_WORD>))
     {
         String directiveName;
@@ -1543,7 +1546,7 @@
 
         if (directiveName.equals("macro"))
         {
-             doItNow = true;
+             isMacro = true;
         }
 
         /*
@@ -1602,7 +1605,7 @@
             {
                 if (argType == ParserTreeConstants.JJTWORD)
                 {
-                    if (doItNow && argPos == 0)
+                    if (isMacro && argPos == 0)
                     {
                         /* if #macro and it's the 0th arg, ok */
                     }
@@ -1625,7 +1628,7 @@
                 }
                 else
                 {
-                    if (doItNow && argPos == 0)
+                    if (isMacro && argPos == 0)
                     {
                         /* if a VM and it's the 0th arg, not ok */
 
@@ -1648,20 +1651,22 @@
     }
     |
     {
-       if (doItNow)  // doItNow is true if the directive is "macro"
-       {
+        if (isMacro)  // isMacro is true if the directive is "macro"
+        {
             // VELOCITY-667 We get here if we have a "#macro" construct
-           // without parenthesis which is a parse error
+                 // without parenthesis which is a parse error
             throw new MacroParseException("A macro declaration requires at 
least a name argument"
               , currentTemplateName, t);
-       }
+        }
        
-        /**
-         * Not a directive
-         */
         token_source.stateStackPop();
         token_source.inDirective = false;
-        return jjtThis;
+
+        if (directiveType != Directive.BLOCK)
+        {
+            // not a directive
+            return jjtThis;
+        }
     }
     )
     /*
@@ -1679,7 +1684,7 @@
          *     we don't have to worry about forward references and such...
          */
 
-        if (doItNow)
+        if (isMacro)
         {
             // Further checking of macro arguments
             Macro.checkArgs(rsvc, t, jjtThis, currentTemplateName);

Modified: 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BlockMacroTestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BlockMacroTestCase.java?rev=742067&r1=742066&r2=742067&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BlockMacroTestCase.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BlockMacroTestCase.java
 Sun Feb  8 11:50:43 2009
@@ -29,6 +29,7 @@
     public BlockMacroTestCase(String name)
     {
         super(name);
+        DEBUG=true;
     }
 
     public void testMultipleBodyContentIncludes() throws Exception
@@ -80,9 +81,24 @@
         assertEvalEquals(result, template);
     }
 
+    public void testStrict()
+    {
+        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, true);
+        assertEvalException("#...@foo#end");
+        assertEvalException("#...@foo()#end");
+    }
+    
+    public void testVelocity690()
+    {
+        assertEvalEquals(" output ", "#macro(foo) output #e...@foo #end");
+        assertEvalEquals("#[ output )", "#macro(foo2)#[$bodyContent)#e...@foo2 
output #end");
+        assertEvalEquals("#[output)", 
"#macro(foo2)#[$bodyContent)#en...@foo2}output#end");
+        assertEvalException("#macro(foo) output #e...@foo");
+    }
+    
     public void testVelocity675() throws Exception
     {
-        assertEvalEquals("#...@foo", "#...@foo"); 
+      assertEvalEquals("#...@foo#end", "#...@foo#end"); 
     }
     
     public void testVelocity685() throws Exception

Modified: 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/StrictEscapeTestCase.java?rev=742067&r1=742066&r2=742067&view=diff
==============================================================================
--- 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
 (original)
+++ 
velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
 Sun Feb  8 11:50:43 2009
@@ -90,8 +90,12 @@
       assertEvalEquals("\\bar", "#set($abc = \"\\\\#foo()\")$abc");
       
       assertEvalEquals("#...@foo()", "\...@foo()");
+      assertEvalEquals("#...@foo", "\...@foo");
+      assertEvalEquals("#...@bar", "\...@bar");
       assertEvalEquals("\\bar", "\\...@foo()#end");
       assertEvalEquals("#...@foo()#end", "\...@foo()\\#end");
+      assertEvalEquals("#...@foo#end", "\...@foo\\#end");
+      assertEvalEquals("#...@bar #end", "\...@bar \\#end");
       
       assertEvalEquals("#end #foreach #define() #elseif", "\\#end \\#foreach 
\\#define() \\#elseif");
       assertEvalEquals("#{end} #{foreach} #{define}() #{elseif}", "\\#{end} 
\\#{foreach} \\#{define}() \\#{elseif}");


Reply via email to