velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1759426 - in /velocity/engine/trunk/velocity-engine-core/src/main: java/org/apache/velocity/runtime/parser/node/ parser/
Date Tue, 06 Sep 2016 12:47:43 GMT
Author: cbrisson
Date: Tue Sep  6 12:47:42 2016
New Revision: 1759426

URL: http://svn.apache.org/viewvc?rev=1759426&view=rev
Log:
some indentation debugging, and fixes VELOCITY-678 along the way

Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTBlock.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
    velocity/engine/trunk/velocity-engine-core/src/main/parser/Parser.jjt

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTBlock.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTBlock.java?rev=1759426&r1=1759425&r2=1759426&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTBlock.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTBlock.java
Tue Sep  6 12:47:42 2016
@@ -42,6 +42,11 @@ public class ASTBlock extends SimpleNode
     // used during parsing
     public boolean endsWithNewline = false;
     
+    /*
+     * '#' and '$' prefix characters eaten by javacc MORE mode, prefixing the '#' ending
the block
+     */
+    private String morePostfix = "";
+
     /**
      * @param id
      */
@@ -96,6 +101,11 @@ public class ASTBlock extends SimpleNode
         return prefix;
     }
 
+    public void setMorePostfix(String morePosffix)
+    {
+        this.morePostfix = morePosffix;
+    }
+
     /**
      * set indentation postfix
      * @param postfix
@@ -133,11 +143,13 @@ public class ASTBlock extends SimpleNode
         for (i = 0; i < k; i++)
             jjtGetChild(i).render(context, writer);
 
-        if (spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
+        if (morePostfix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) <
0)
         {
             writer.write(postfix);
         }
 
+        writer.write(morePostfix);
+
         return true;
     }
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java?rev=1759426&r1=1759425&r2=1759426&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
Tue Sep  6 12:47:42 2016
@@ -61,6 +61,11 @@ public class ASTDirective extends Simple
     private String prefix = "";
     private String postfix = "";
 
+    /*
+     * '#' and '$' prefix characters eaten by javacc MORE mode
+     */
+    private String morePrefix = "";
+
     /**
      * @param id
      */
@@ -93,6 +98,8 @@ public class ASTDirective extends Simple
     public synchronized Object init( InternalContextAdapter context, Object data)
     throws TemplateInitException
     {
+        Token t;
+
         /** method is synchronized to avoid concurrent directive initialization **/
 
         if (!isInitialized)
@@ -100,6 +107,20 @@ public class ASTDirective extends Simple
             super.init( context, data );
 
             /*
+             * handle '$' and '#' chars prefix
+             */
+            t = getFirstToken();
+            int pos = -1;
+            while (t != null && (pos = t.image.lastIndexOf('#')) == -1)
+            {
+                t = t.next;
+            }
+            if (t != null && pos > 0)
+            {
+                morePrefix = t.image.substring(0, pos);
+            }
+
+            /*
              *  only do things that are not context dependent
              */
 
@@ -127,7 +148,7 @@ public class ASTDirective extends Simple
                             e);
                 }
 
-                Token t = first;
+                t = getFirstToken();
                 if (t.kind == ParserConstants.WHITESPACE) t = t.next;
                 directive.setLocation(t.beginLine, t.beginColumn, getTemplate());
                 directive.init(rsvc, context, this);
@@ -200,7 +221,7 @@ public class ASTDirective extends Simple
             cleanupParserAndTokens();
         }
 
-        if (rsvc.getSpaceGobbling() == SpaceGobbling.STRUCTURED && isInitialized
&& isDirective && directive.getType() == Directive.BLOCK)
+        if (morePrefix.length() == 0 && rsvc.getSpaceGobbling() == SpaceGobbling.STRUCTURED
&& isInitialized && isDirective && directive.getType() == Directive.BLOCK)
         {
             NodeUtils.fixIndentation(this, prefix);
         }
@@ -262,14 +283,16 @@ public class ASTDirective extends Simple
 
         if (isDirective)
         {
-            if (spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
+            if (morePrefix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES)
< 0)
             {
                 writer.write(prefix);
             }
 
+            writer.write(morePrefix);
+
             directive.render(context, writer, this);
 
-            if (spaceGobbling == SpaceGobbling.NONE)
+            if (morePrefix.length() > 0 || spaceGobbling == SpaceGobbling.NONE)
             {
                 writer.write(postfix);
             }
@@ -277,6 +300,7 @@ public class ASTDirective extends Simple
         else
         {
             writer.write(prefix);
+            writer.write(morePrefix);
             writer.write( "#");
             writer.write(directiveName);
             writer.write(postfix);

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java?rev=1759426&r1=1759425&r2=1759426&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
Tue Sep  6 12:47:42 2016
@@ -36,6 +36,7 @@ import org.apache.velocity.exception.Res
 import org.apache.velocity.exception.TemplateInitException;
 import org.apache.velocity.runtime.RuntimeConstants.SpaceGobbling;
 import org.apache.velocity.runtime.parser.Parser;
+import org.apache.velocity.runtime.parser.Token;
 
 import java.io.IOException;
 import java.io.Writer;
@@ -49,6 +50,11 @@ public class ASTIfStatement extends Simp
     private String prefix = "";
     private String postfix = "";
 
+    /*
+     * '#' and '$' prefix characters eaten by javacc MORE mode
+     */
+    private String morePrefix = "";
+
     /**
      * @param id
      */
@@ -82,6 +88,20 @@ public class ASTIfStatement extends Simp
     {
         Object obj = super.init(context, data);
 
+        /*
+         * handle '$' and '#' chars prefix
+         */
+        Token t = getFirstToken();
+        int pos = -1;
+        while (t != null && (pos = t.image.lastIndexOf('#')) == -1)
+        {
+            t = t.next;
+        }
+        if (t != null && pos > 0)
+        {
+            morePrefix = t.image.substring(0, pos);
+        }
+
         /* handle structured space gobbling */
         if (rsvc.getSpaceGobbling() == SpaceGobbling.STRUCTURED && postfix.length()
> 0)
         {
@@ -137,49 +157,50 @@ public class ASTIfStatement extends Simp
     {
         SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling();
 
-        if (spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
+        if (morePrefix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) <
0)
         {
             writer.write(prefix);
         }
 
+        writer.write(morePrefix);
+
         /*
          * Check if the #if(expression) construct evaluates to true:
-         * if so render and leave immediately because there
-         * is nothing left to do!
          */
         if (jjtGetChild(0).evaluate(context))
         {
             jjtGetChild(1).render(context, writer);
-            return true;
         }
-
-        int totalNodes = jjtGetNumChildren();
-
-        /*
-         * Now check the remaining nodes left in the
-         * if construct. The nodes are either elseif
-         *  nodes or else nodes. Each of these node
-         * types knows how to evaluate themselves. If
-         * a node evaluates to true then the node will
-         * render itself and this method will return
-         * as there is nothing left to do.
-         */
-        for (int i = 2; i < totalNodes; i++)
+        else
         {
-            if (jjtGetChild(i).evaluate(context))
+            int totalNodes = jjtGetNumChildren();
+
+            /*
+             * Now check the remaining nodes left in the
+             * if construct. The nodes are either elseif
+             *  nodes or else nodes. Each of these node
+             * types knows how to evaluate themselves. If
+             * a node evaluates to true then the node will
+             * render itself and this method will return
+             * as there is nothing left to do.
+             */
+            for (int i = 2; i < totalNodes; i++)
             {
-                jjtGetChild(i).render(context, writer);
-                break;
+                if (jjtGetChild(i).evaluate(context))
+                {
+                    jjtGetChild(i).render(context, writer);
+                    break;
+                }
             }
         }
 
-        if (spaceGobbling == SpaceGobbling.NONE)
+        if (morePrefix.length() > 0 || spaceGobbling == SpaceGobbling.NONE)
         {
             writer.write(postfix);
         }
 
         /*
-         * This is reached without rendering anything when an ASTIfStatement
+         * This is reached without rendering anything (other than potential suffix/prefix)
when an ASTIfStatement
          * consists of an if/elseif sequence where none of the nodes evaluate to true.
          */
 

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java?rev=1759426&r1=1759425&r2=1759426&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
Tue Sep  6 12:47:42 2016
@@ -26,6 +26,7 @@ import org.apache.velocity.exception.Tem
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeConstants.SpaceGobbling;
 import org.apache.velocity.runtime.parser.Parser;
+import org.apache.velocity.runtime.parser.Token;
 import org.apache.velocity.util.introspection.Info;
 
 import java.io.IOException;
@@ -47,6 +48,12 @@ public class ASTSetDirective extends Sim
     private String prefix = "";
     private String postfix = "";
 
+    /*
+     * '#' and '$' prefix characters eaten by javacc MORE mode
+     */
+    private String morePrefix = "";
+
+
     /**
      *  This is really immutable after the init, so keep one for this node
      */
@@ -102,6 +109,21 @@ public class ASTSetDirective extends Sim
     
             super.init( context, data );
     
+            /*
+             * handle '$' and '#' chars prefix
+             */
+            Token t = getFirstToken();
+            int pos = -1;
+            while (t != null && (pos = t.image.lastIndexOf('#')) == -1)
+            {
+                t = t.next;
+            }
+            if (t != null && pos > 0)
+            {
+                morePrefix = t.image.substring(0, pos);
+            }
+
+
             uberInfo = new Info(getTemplateName(),
                     getLine(), getColumn());
     
@@ -199,11 +221,13 @@ public class ASTSetDirective extends Sim
            We handle this by appropriately emptying the prefix in BC mode.
          */
 
-        if (spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
+        if (morePrefix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) <
0)
         {
             writer.write(prefix);
         }
 
+        writer.write(morePrefix);
+
         /*
          *  get the RHS node, and its value
          */
@@ -220,12 +244,11 @@ public class ASTSetDirective extends Sim
             EventHandlerUtil.invalidSetMethod(rsvc, context, leftReference, rightReference,
uberInfo);
         }
 
-        if (spaceGobbling == SpaceGobbling.NONE)
+        if (morePrefix.length() > 0 || spaceGobbling == SpaceGobbling.NONE)
         {
             writer.write(postfix);
         }
 
-
         return left.setValue(context, value);
     }
     

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=1759426&r1=1759425&r2=1759426&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 Tue Sep  6 12:47:42
2016
@@ -1440,7 +1440,7 @@ void DirectiveAssign() : {}
  */
 boolean Directive() :
 {
-    Token id = null, t = null, u = null;
+    Token id = null, t = null, u = null, end = null;
     int argType;
     int argPos = 0;
     Directive d;
@@ -1467,13 +1467,14 @@ boolean Directive() :
     ((id = <WORD>) | (id = <BRACKETED_WORD>))
     {
         String directiveName;
+        int p = id.image.lastIndexOf('#');
         if (id.kind == ParserConstants.BRACKETED_WORD)
         {
-            directiveName = id.image.substring(2, id.image.length() - 1);
+            directiveName = id.image.substring(p + 2, id.image.length() - 1);
         }
         else
         {
-            directiveName = id.image.substring(1);
+            directiveName = id.image.substring(p + 1);
         }
 
         d = getDirective(directiveName);
@@ -1621,7 +1622,7 @@ boolean Directive() :
           t = null;
       }
     ]
-    (<END>
+    ((end = <END>)
      [ LOOKAHEAD(2) ( [ ( t = <WHITESPACE> ) ] ( u = <NEWLINE> ) )
      {
          jjtThis.setPostfix(t == null ? u.image : t.image + u.image);
@@ -1629,6 +1630,10 @@ boolean Directive() :
          newlineAtEnd = true;
      }
      ]
+     {
+         int pos = end.image.lastIndexOf('#');
+         if (pos > 0) block.setMorePostfix(end.image.substring(0, pos));
+     }
     )
     {
         /*
@@ -1827,7 +1832,7 @@ boolean Text() : {}
 
 boolean IfStatement() :
 {
-    Token t = null, u = null;
+    Token t = null, u = null, end = null;
     ASTBlock lastBlock = null;
     boolean afterNewline = false, newlineAtEnd = false;
 }
@@ -1867,7 +1872,7 @@ boolean IfStatement() :
             t = null;
         }
     ]
-    <END>
+    (end = <END>)
     [
         LOOKAHEAD(2) ( [ ( t = <WHITESPACE> ) ] ( u = <NEWLINE> ) )
         {
@@ -1876,13 +1881,18 @@ boolean IfStatement() :
         }
     ]
     {
+        int pos = end.image.lastIndexOf('#');
+        if (pos > 0)
+        {
+            lastBlock.setMorePostfix(end.image.substring(0, pos));
+        }
         return newlineAtEnd;
     }
 }
 
 ASTBlock ElseStatement(ASTBlock previousBlock) :
 {
-    Token t = null, u = null;
+    Token t = null, u = null, _else = null;
     ASTBlock block = null;
     boolean afterNewline = false;
 }
@@ -1893,7 +1903,7 @@ ASTBlock ElseStatement(ASTBlock previous
          t = null;
      }
    ]
-   <ELSE>
+   (_else = <ELSE>)
    (
      [
        LOOKAHEAD(2) ( [ ( t = <WHITESPACE> ) ] ( u = <NEWLINE> ) )
@@ -1911,13 +1921,18 @@ ASTBlock ElseStatement(ASTBlock previous
     )
     #Block
     {
+        int pos = _else.image.lastIndexOf('#');
+        if (pos > 0)
+        {
+            previousBlock.setMorePostfix(_else.image.substring(0, pos));
+        }
         return block;
     }
 }
 
 ASTBlock ElseIfStatement(ASTBlock previousBlock) :
 {
-    Token t = null, u = null;
+    Token t = null, u = null, elseif = null;
     ASTBlock block = null;
     boolean afterNewline = false;
 }
@@ -1928,7 +1943,7 @@ ASTBlock ElseIfStatement(ASTBlock previo
         t = null;
     }
   ]
-  <ELSEIF> ( <WHITESPACE> | <NEWLINE> )*
+  (elseif = <ELSEIF>) ( <WHITESPACE> | <NEWLINE> )*
   <LPAREN> Expression() <RPAREN>
   (
     [
@@ -1947,6 +1962,11 @@ ASTBlock ElseIfStatement(ASTBlock previo
   )
   #Block
   {
+      int pos = elseif.image.lastIndexOf('#');
+      if (pos > 0)
+      {
+          previousBlock.setMorePostfix(elseif.image.substring(0, pos));
+      }
       return block;
   }
 }



Mime
View raw message