You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2018/10/08 19:11:47 UTC

svn commit: r1843186 - in /velocity/engine/trunk/velocity-engine-core: ./ src/main/java/org/apache/velocity/runtime/ src/main/parser/ src/main/resources/org/apache/velocity/runtime/defaults/ src/test/java/org/apache/velocity/test/

Author: cbrisson
Date: Mon Oct  8 19:11:47 2018
New Revision: 1843186

URL: http://svn.apache.org/viewvc?rev=1843186&view=rev
Log:
[VELOCITY-542] Added a new 'parser.allows.dash.in.identifiers' boolean property (false per default) to (dis)allow '-' in reference identifiers

Added:
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java
      - copied, changed from r1843176, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ExpressionAsMethodArgumentTestCase.java
Modified:
    velocity/engine/trunk/velocity-engine-core/pom.xml
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
    velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt
    velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties

Modified: velocity/engine/trunk/velocity-engine-core/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/pom.xml?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/pom.xml (original)
+++ velocity/engine/trunk/velocity-engine-core/pom.xml Mon Oct  8 19:11:47 2018
@@ -176,6 +176,7 @@
           <nodeUsesParser>true</nodeUsesParser>
           <nodePackage>org.apache.velocity.runtime.parser.node</nodePackage>
           <sourceDirectory>${basedir}/src/main/parser</sourceDirectory>
+          <tokenManagerUsesParser>true</tokenManagerUsesParser>
         </configuration>
       </plugin>
     </plugins>

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java Mon Oct  8 19:11:47 2018
@@ -288,6 +288,11 @@ public interface RuntimeConstants
     String PARSER_POOL_SIZE = "parser.pool.size";
 
     /**
+     * Allow dash in identifiers (backward compatibility option)
+     */
+    String PARSER_DASH_ALLOWED = "parser.allows.dash.in.identifiers";
+
+    /**
      * Space gobbling mode
      */
     String SPACE_GOBBLING = "space.gobbling";

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java Mon Oct  8 19:11:47 2018
@@ -199,6 +199,11 @@ public class RuntimeInstance implements
     private SpaceGobbling spaceGobbling;
 
     /**
+     * Whether dash is allowed in identifiers
+     */
+    private boolean dashAllowedInIdentifiers;
+
+    /**
      * Creates a new RuntimeInstance object.
      */
     public RuntimeInstance()
@@ -352,6 +357,9 @@ public class RuntimeInstance implements
         {
             spaceGobbling = SpaceGobbling.LINES;
         }
+
+        /* init parser behavior */
+        dashAllowedInIdentifiers = getBoolean(PARSER_DASH_ALLOWED, false);
     }
 
     /**
@@ -1838,4 +1846,13 @@ public class RuntimeInstance implements
     {
         return spaceGobbling;
     }
+
+    /**
+     * get whether dashes are allowed in identifiers
+     * @return configured boolean flag
+     */
+    public boolean isDashAllowedInIdentifiers()
+    {
+        return dashAllowedInIdentifiers;
+    }
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java Mon Oct  8 19:11:47 2018
@@ -467,4 +467,10 @@ public interface RuntimeServices
      * @return space gobbling mode
      */
     SpaceGobbling getSpaceGobbling();
+
+   /**
+    * get whether dashes are allowed in identifiers
+    * @return configured boolean flag
+    */
+   boolean isDashAllowedInIdentifiers();
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt Mon Oct  8 19:11:47 2018
@@ -126,6 +126,11 @@ public class Parser
 
     private RuntimeServices rsvc = null;
 
+    public RuntimeServices getRuntimeServices()
+    {
+        return rsvc;
+    }
+
     private Logger log = null;
 
     /**
@@ -567,7 +572,7 @@ TOKEN_MGR_DECLS:
 /* The VelocityCharStream will send a zero-width whitespace
    just before EOF to let us accept a terminal $ or #
 */
-<PRE_DIRECTIVE,PRE_REFERENCE>
+<PRE_DIRECTIVE,PRE_REFERENCE,PRE_OLD_REFERENCE>
 TOKEN :
 {
     <LONE_SYMBOL: "\u200B" >
@@ -718,7 +723,7 @@ TOKEN:
  *  http://www.engr.mun.ca/~theo/JavaCC-FAQ/javacc-faq-ie.htm#tth_sEc3.12
  *
  */
-<DEFAULT, PRE_REFERENCE, REFERENCE, REFMODIFIER, REFMOD2>
+<DEFAULT, PRE_REFERENCE, PRE_OLD_REFERENCE, REFERENCE, REFMODIFIER, REFMOD2>
 TOKEN:
 {
   <SET_DIRECTIVE: ("#set" | "#{set}")  (" "|"\t")* "(">
@@ -768,7 +773,7 @@ MORE :
              * to end the previous ref
              */
 
-            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE)
+            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE || curLexState == PRE_OLD_REFERENCE)
             {
                 inReference = false;
                 stateStackPop();
@@ -776,15 +781,17 @@ MORE :
 
             inReference = true;
 
+            int preReferenceState = parser.getRuntimeServices().isDashAllowedInIdentifiers() ? PRE_OLD_REFERENCE : PRE_REFERENCE;
+
             if ( debugPrint )
-                System.out.println( " $  : going to PRE_REFERENCE" );
+                System.out.println( " $  : going to " + lexStateNames[preReferenceState]);
 
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
                 stateStackPush();
             }
-            switchTo(PRE_REFERENCE);
+            switchTo(preReferenceState);
         }
     }
 
@@ -797,7 +804,7 @@ MORE :
              * to end the previous ref
              */
 
-            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE)
+            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE || curLexState == PRE_OLD_REFERENCE)
             {
                 inReference = false;
                 stateStackPop();
@@ -805,15 +812,17 @@ MORE :
 
             inReference = true;
 
+            int preReferenceState = parser.getRuntimeServices().isDashAllowedInIdentifiers() ? PRE_OLD_REFERENCE : PRE_REFERENCE;
+
             if ( debugPrint )
-                System.out.println( " $!  : going to PRE_REFERENCE" );
+                System.out.println( " $  : going to " + lexStateNames[preReferenceState]);
 
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
                 stateStackPush();
             }
-            switchTo(PRE_REFERENCE);
+            switchTo(preReferenceState);
         }
     }
 
@@ -823,7 +832,7 @@ MORE :
        {
            inComment = true;
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
                 stateStackPush();
             }
@@ -838,7 +847,7 @@ MORE :
 	    input_stream.backup(1);
 	    inComment = true;
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
 	        stateStackPush();
             }
@@ -852,7 +861,7 @@ MORE :
     	{
 	    inComment=true;
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
 	        stateStackPush();
             }
@@ -871,7 +880,7 @@ MORE :
              * you are going into DIRECTIVE while in REFERENCE.  -gmj
              */
 
-            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE || curLexState == REFMODIFIER )
+            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE || curLexState == PRE_OLD_REFERENCE || curLexState == REFMODIFIER )
             {
                 inReference = false;
                 stateStackPop();
@@ -881,7 +890,7 @@ MORE :
                 System.out.println(" # :  going to PRE_DIRECTIVE" );
 
             /* do not push PRE states */
-            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE)
+            if (curLexState != PRE_REFERENCE && curLexState != PRE_DIRECTIVE && curLexState != PRE_OLD_REFERENCE)
             {
                 stateStackPush();
             }
@@ -892,14 +901,14 @@ MORE :
 
 // treat the single line comment case separately
 // to avoid ##<EOF> errors
-<DEFAULT,PRE_DIRECTIVE,DIRECTIVE,REFERENCE,PRE_REFERENCE,REFMOD2,REFMOD3,REFMODIFIER>
+<DEFAULT,PRE_DIRECTIVE,DIRECTIVE,REFERENCE,PRE_REFERENCE,PRE_OLD_REFERENCE,REFMOD2,REFMOD3,REFMODIFIER>
 TOKEN :
 {
    <SINGLE_LINE_COMMENT_START: "##">
    {
         if (!inComment)
         {
-            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE)
+            if (curLexState == REFERENCE || curLexState == PRE_REFERENCE || curLexState == PRE_OLD_REFERENCE)
             {
                 inReference = false;
                 stateStackPop();
@@ -1010,7 +1019,7 @@ TOKEN:
 /* needed for stuff like #foo() followed by ( '$' | '#' )* followed by ( <WHITESPACE> | <ENDLINE> )
    so that directive postfix doesn't eat the '$'s and '#'s
 */
-<PRE_DIRECTIVE, PRE_REFERENCE>
+<PRE_DIRECTIVE, PRE_REFERENCE, PRE_OLD_REFERENCE>
 TOKEN:
 {
   <SUFFIX: ([" ","\t"])* ("\n" | "\r" | "\r\n")>
@@ -1199,7 +1208,7 @@ TOKEN:
  *  ^ ^ ^   ^       ^
  *  | | |   |       |
  *  |_________________ >  PRE_REFERENCE : state initiated by the '$' character.
- *    | |   |       |
+ *    | |   |       |                     (or PRE_OLD_REFERENCE if '-' is allowed in identifiers)
  *    |________________>  REFERENCE : state initiated by the identifier. Continues
  *      |   |       |     until end of the reference, or the . character.
  *      |_____________ >  REFMODIFIER : state switched to when the <DOT> is encountered.
@@ -1227,6 +1236,18 @@ TOKEN :
     }
 }
 
+<PRE_OLD_REFERENCE>
+TOKEN :
+{
+    <#OLD_ALPHA_CHAR: ["a"-"z", "A"-"Z", "_"] >
+|   <#OLD_IDENTIFIER_CHAR: [ "a"-"z", "A"-"Z", "0"-"9", "_", "-" ] >
+|   <OLD_IDENTIFIER:  ( <OLD_ALPHA_CHAR> ) (<OLD_IDENTIFIER_CHAR>)* >
+    {
+        switchTo(REFERENCE);
+    }
+}
+
+
 <REFERENCE,REFMODIFIER,REFMOD2,REFMOD3>
 TOKEN:
 {
@@ -1253,7 +1274,7 @@ TOKEN:
     }
 }
 
-<PRE_REFERENCE,REFERENCE,REFMODIFIER,REFMOD3>
+<PRE_REFERENCE,PRE_OLD_REFERENCE,REFERENCE,REFMODIFIER,REFMOD3>
 TOKEN :
 {
     <LCURLY: "{">
@@ -1263,7 +1284,7 @@ TOKEN :
     }
 }
 
-<PRE_REFERENCE,REFERENCE,REFMODIFIER,REFMOD,REFMOD3>
+<PRE_REFERENCE,PRE_OLD_REFERENCE,REFERENCE,REFMODIFIER,REFMOD,REFMOD3>
 SPECIAL_TOKEN :
 {
     <REFERENCE_TERMINATOR: ~[] >
@@ -1468,7 +1489,7 @@ void StringLiteral() : {}
  */
 void Identifier() : {}
 {
-    <IDENTIFIER>
+    <IDENTIFIER> | <OLD_IDENTIFIER>
 }
 
 void Word() : {}
@@ -1862,13 +1883,13 @@ void Reference() : {}
      */
 
       (
-       <IDENTIFIER> (Index())*
+       ( <IDENTIFIER> | <OLD_IDENTIFIER> ) (Index())*
          (LOOKAHEAD(2) <DOT> (LOOKAHEAD(3) Method() | Identifier() ) (Index())* )*
       )
       |
       (
          <LCURLY>
-         <IDENTIFIER> (Index())*
+         ( <IDENTIFIER> | <OLD_IDENTIFIER> ) (Index())*
          (LOOKAHEAD(2) <DOT> (LOOKAHEAD(3) Method() | Identifier() ) (Index())* )*
          <RCURLY>
       )
@@ -1892,7 +1913,7 @@ void False() : {}
  * with the LOOKAHEAD in Reference, but I never found a way to resolve
  * it in a more fashionable way..
  */
-<DEFAULT,PRE_REFERENCE>
+<DEFAULT,PRE_REFERENCE,PRE_OLD_REFERENCE>
 TOKEN :
 {
   <EMPTY_INDEX : ("$[" | "$![" | "$\\![" | "$.")>

Modified: velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties?rev=1843186&r1=1843185&r2=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties Mon Oct  8 19:11:47 2018
@@ -238,3 +238,11 @@ introspector.restrict.classes = java.lan
 # ----------------------------------------------------------------------------
 
 space.gobbling = lines
+
+# ----------------------------------------------------------------------------
+# DASH IN IDENTIFIERS
+# ----------------------------------------------------------------------------
+# Set to true to allow '-' in reference identifiers (backward compatibility option)
+# ----------------------------------------------------------------------------
+
+parser.allows.dash.in.identifiers = false

Copied: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java (from r1843176, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ExpressionAsMethodArgumentTestCase.java)
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java?p2=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java&p1=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ExpressionAsMethodArgumentTestCase.java&r1=1843176&r2=1843186&rev=1843186&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ExpressionAsMethodArgumentTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/DashInIdentifiersTestCase.java Mon Oct  8 19:11:47 2018
@@ -20,36 +20,26 @@ package org.apache.velocity.test;
  */
 
 import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
 
 /**
  * This class tests passing expressions as method arguments
  */
 
-public class ExpressionAsMethodArgumentTestCase extends BaseTestCase
+public class DashInIdentifiersTestCase extends BaseTestCase
 {
-    public ExpressionAsMethodArgumentTestCase(final String name)
+    public DashInIdentifiersTestCase(final String name)
     {
         super(name);
     }
 
-    protected void setUpContext(VelocityContext context)
+    protected void setUpEngine(VelocityEngine engine)
     {
-        context.put("tool",new MyAbsTool());
-        context.put("foo",2);
-        context.put("bar",-3);
+        engine.addProperty("parser.allows.dash.in.identifiers", true);
     }
 
-    public void testExpressionAsMethod()
+    public void testDash()
     {
-        assertEvalEquals("6","$tool.abs( $foo * $bar )");
-        assertEvalEquals("12","$tool.abs( $foo * $tool.abs( $foo * $bar ) )");
-    }
-
-    public static class MyAbsTool
-    {
-        public int abs(int num)
-        {
-            return Math.abs(num);
-        }
+        assertEvalEquals("6","#set($var-1 = 7)#set($var-2 = $var-1 - 1)$var-2");
     }
 }