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 2019/06/06 16:13:47 UTC

svn commit: r1860722 [1/2] - in /velocity/engine/branches/parser_experiments/velocity-engine-core: ./ src/main/java/org/apache/velocity/runtime/ src/main/java/org/apache/velocity/runtime/parser/ src/main/java/org/apache/velocity/runtime/parser/node/ sr...

Author: cbrisson
Date: Thu Jun  6 16:13:47 2019
New Revision: 1860722

URL: http://svn.apache.org/viewvc?rev=1860722&view=rev
Log:
[engine][VELOCITY-917] Compile-time configurable parser : working StandardParser

Added:
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/parser/Parser.jjt
Removed:
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/ParserConstants.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/ParserTokenManager.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/parser/StandardParser.jjt
Modified:
    velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
    velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/NodeUtils.java

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml?rev=1860722&r1=1860721&r2=1860722&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/pom.xml Thu Jun  6 16:13:47 2019
@@ -31,13 +31,12 @@
     <name>Apache Velocity - Engine</name>
 
     <properties>
-        <!-- command line switch -Dparser.create=true generates the parser files -->
-        <parser.create>false</parser.create>
-
-        <!-- command line switch -Dparser.nodefiles=true generates AST Node classes (new structures added to parser) -->
-        <parser.nodefiles>false</parser.nodefiles>
-
         <parser.debug>false</parser.debug>
+        <parser.name>Standard</parser.name>
+        <parser.char.asterisk>*</parser.char.asterisk>
+        <parser.char.at>@</parser.char.at>
+        <parser.char.dollar>$</parser.char.dollar>
+        <parser.char.hash>#</parser.char.hash>
         
         <!-- You can modify those properties locally to test
              the DataSourceResourceLoader against other engines.
@@ -55,7 +54,15 @@
 
     <build>
         <plugins>
-            <!-- resources -->
+            <!-- maven lifecycle debugging plugin, see 'mvn buildplan:help' -->
+
+            <plugin>
+                <groupId>fr.jcgay.maven.plugins</groupId>
+                <artifactId>buildplan-maven-plugin</artifactId>
+                <version>1.3</version>
+            </plugin>
+
+            <!-- prepare parser grammar file -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-resources-plugin</artifactId>
@@ -66,6 +73,24 @@
                         <delimiter>${*}</delimiter>
                     </delimiters>
                 </configuration>
+                <executions>
+                    <execution>
+                        <id>gen-parser-grammar</id>
+                        <phase>generate-sources</phase>
+                        <goals>
+                            <goal>copy-resources</goal>
+                        </goals>
+                        <configuration>
+                            <resources>
+                                <resource>
+                                    <directory>src/main/parser</directory>
+                                    <filtering>true</filtering>
+                                </resource>
+                            </resources>
+                            <outputDirectory>${project.build.directory}/parser</outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
             </plugin>
 
             <!-- shading of commons-io -->
@@ -106,6 +131,20 @@
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>javacc-maven-plugin</artifactId>
                 <version>2.6</version>
+                <configuration>
+                    <isStatic>false</isStatic>
+                    <buildParser>true</buildParser>
+                    <buildNodeFiles>false</buildNodeFiles>
+                    <multi>true</multi>
+                    <debugParser>${parser.debug}</debugParser>
+                    <debugLookAhead>${parser.debug}</debugLookAhead>
+                    <debugTokenManager>${parser.debug}</debugTokenManager>
+                    <jdkVersion>${maven.compiler.target}</jdkVersion>
+                    <nodeUsesParser>true</nodeUsesParser>
+                    <nodePackage>org.apache.velocity.runtime.parser.node</nodePackage>
+                    <sourceDirectory>${project.build.directory}/parser</sourceDirectory>
+                    <tokenManagerUsesParser>true</tokenManagerUsesParser>
+                </configuration>
                 <executions>
                     <!-- build the standard parser -->
                     <execution>
@@ -115,46 +154,17 @@
                         </goals>
                         <configuration>
                             <includes>
-                                <include>StandardParser.jjt</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                    <!-- build a tailored parser -->
-                    <execution>
-                        <id>jjtree-javacc-custom</id>
-                        <goals>
-                            <goal>jjtree-javacc</goal>
-                        </goals>
-                        <configuration>
-                            <includes>
-                                <include>TailoredParser.jjt</include>
+                                <include>Parser.jjt</include>
                             </includes>
-                            <interimDirectory>${project.build.directory}/tailored-parser/jjtree</interimDirectory>
-                            <outputDirectory>${project.build.directory}/tailored-parser/javacc</outputDirectory>
                         </configuration>
                     </execution>
                 </executions>
-                <configuration>
-                    <isStatic>false</isStatic>
-                    <buildParser>true</buildParser>
-                    <buildNodeFiles>${parser.nodefiles}</buildNodeFiles>
-                    <multi>true</multi>
-                    <debugParser>${parser.debug}</debugParser>
-                    <debugLookAhead>${parser.debug}</debugLookAhead>
-                    <debugTokenManager>${parser.debug}</debugTokenManager>
-                    <jdkVersion>${maven.compiler.target}</jdkVersion>
-                    <nodeUsesParser>true</nodeUsesParser>
-                    <nodePackage>org.apache.velocity.runtime.parser.node</nodePackage>
-                    <sourceDirectory>${basedir}/src/main/parser</sourceDirectory>
-                    <tokenManagerUsesParser>true</tokenManagerUsesParser>
-                </configuration>
             </plugin>
 
             <!--
-                patch generated parser and token managers files, so that
-                + both token managers implement the ParserTokenManager interface
+                patch generated parser and token manager files, so that
+                + token manager implement the ParserTokenManager interface
                 + StandardParser uses the ParserTokenManager interface
-            -->
             <plugin>
                 <groupId>com.google.code.maven-replacer-plugin</groupId>
                 <artifactId>replacer</artifactId>
@@ -162,7 +172,7 @@
                 <executions>
                     <execution>
                         <id>patch-parser</id>
-                        <phase>process-sources</phase>
+                        <phase>generate-sources</phase>
                         <goals>
                             <goal>replace</goal> 
                         </goals>
@@ -178,14 +188,13 @@
                     </execution>
                     <execution>
                         <id>patch-token-manager</id>
-                        <phase>process-sources</phase>
+                        <phase>generate-sources</phase>
                         <goals>
                             <goal>replace</goal> 
                         </goals> 
                         <configuration>
                             <includes>
                                 <include>${project.build.directory}/generated-sources/javacc/org/apache/velocity/runtime/parser/StandardParserTokenManager.java</include>
-                                <include>${project.build.directory}/tailored-parser/javacc/org/apache/velocity/runtime/parser/TailoredParserTokenManager.java</include>
                             </includes>
                             <replacements>
                                 <replacement>
@@ -197,54 +206,7 @@
                     </execution>
                 </executions>
             </plugin>
-
-            <!-- pre-compile both parser token manager classes -->
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>compile-patch-sources</id>
-                        <phase>process-sources</phase>
-                        <goals>
-                            <goal>compile</goal>
-                        </goals>
-                        <configuration>
-                            <buildDirectory>${project.build.directory}/tailored-parser/classes</buildDirectory>
-                            <includes>
-                                <include>**/*TokenManager*.java/</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
-            <!-- remove duplicate javacc files -->
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-clean-plugin</artifactId>
-                <version>3.1.0</version>
-                <executions>
-                    <execution>
-                        <id>clean-extra-javacc</id>
-                        <phase>process-sources</phase>
-                        <goals>
-                            <goal>clean</goal>
-                        </goals>
-                        <configuration>
-                            <excludeDefaultDirectories>true</excludeDefaultDirectories>
-                            <filesets>
-                                <fileset>
-                                    <directory>${project.build.directory}/tailored-parser/javacc</directory>
-                                    <includes>
-                                        <include>org/apache/velocity/runtime/parser/Token*.java</include>
-                                    </includes>
-                                </fileset>
-                            </filesets>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
+            -->
 
             <!-- handle OSGi informations -->
             <plugin>
@@ -263,16 +225,6 @@
                 </configuration>
             </plugin>
 
-            <!-- exclude tailored parser from final archive -->
-            <plugin>
-                <artifactId>maven-jar-plugin</artifactId>
-                <configuration>
-                    <excludes>
-                        <exclude>**/*TailoredParser*.class</exclude>
-                    </excludes>
-                </configuration>
-            </plugin>
-
             <!-- tests -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=1860722&r1=1860721&r2=1860722&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java Thu Jun  6 16:13:47 2019
@@ -372,6 +372,18 @@ public interface RuntimeConstants extend
      */
 
     /**
+     * Property specifying the parser class to use
+     * @since 2.2
+     */
+    String PARSER_CLASS = "parser.class";
+
+    /**
+     * Default parser class
+     * @since 2.2
+     */
+    String DEFAULT_PARSER_CLASS = "org.apache.velocity.runtime.parser.StandardParser";
+
+    /**
      * The <code>parser.pool.class</code> property specifies the name of the {@link org.apache.velocity.util.SimplePool}
      * implementation to use.
      */
@@ -388,30 +400,6 @@ public interface RuntimeConstants extend
      */
     String PARSER_HYPHEN_ALLOWED = "parser.allow_hyphen_in_identifiers";
 
-    /**
-     * Set the character (onlysingle byte UTF-8 supported at present) to use instead of '$' for references.
-     * @since 2.2
-     */
-    String PARSER_CHAR_DOLLAR = "parser.character.dollar";
-
-    /**
-     * Set the character (onlysingle byte UTF-8 supported at present) to use instead of '#' for directives, macros and comments.
-     * @since 2.2
-     */
-    String PARSER_CHAR_HASH = "parser.character.hash";
-
-    /**
-     * Set the character (onlysingle byte UTF-8 supported at present) to use instead of '@' for '#@' block macros.
-     * @since 2.2
-     */
-    String PARSER_CHAR_AT = "parser.character.at";
-
-    /**
-     * Set the character (onlysingle byte UTF-8 supported at present) to use instead of '*' for '#* *#' block comments.
-     * @since 2.2
-     */
-    String PARSER_CHAR_ASTERISK = "parser.character.asterisk";
-
     /*
      * ----------------------------------------------------------------------
      * G E N E R A L  R U N T I M E  C O N F I G U R A T I O N

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=1860722&r1=1860721&r2=1860722&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java Thu Jun  6 16:13:47 2019
@@ -62,6 +62,8 @@ import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.Writer;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -228,6 +230,12 @@ public class RuntimeInstance implements
     private LogContext logContext;
 
     /**
+     * Configured parser class
+     * @since 2.2
+     */
+    private Constructor parserConstructor;
+
+    /**
      * Configured '$' character
      * @since 2.2
      */
@@ -413,10 +421,6 @@ public class RuntimeInstance implements
 
         /* init parser behavior */
         hyphenAllowedInIdentifiers = getBoolean(PARSER_HYPHEN_ALLOWED, false);
-        dollar = getConfiguredCharacter(PARSER_CHAR_DOLLAR, '$');
-        hash = getConfiguredCharacter(PARSER_CHAR_HASH, '#');
-        at = getConfiguredCharacter(PARSER_CHAR_AT, '@');
-        asterisk = getConfiguredCharacter(PARSER_CHAR_ASTERISK, '*');
     }
 
     private char getConfiguredCharacter(String configKey, char defaultChar)
@@ -1191,6 +1195,29 @@ public class RuntimeInstance implements
     private void initializeParserPool()
     {
         /*
+         * First initialize parser class. If it's not valid or not found, it will generate an error
+         * later on in this method when parser creation is tester.
+         */
+        String parserClassName = getString(PARSER_CLASS, DEFAULT_PARSER_CLASS);
+        Class parserClass;
+        try
+        {
+            parserClass = ClassUtils.getClass(parserClassName);
+        }
+        catch (ClassNotFoundException cnfe)
+        {
+            throw new VelocityException("parser class not found: " + parserClassName, cnfe);
+        }
+        try
+        {
+            parserConstructor = parserClass.getConstructor(RuntimeServices.class);
+        }
+        catch (NoSuchMethodException nsme)
+        {
+            throw new VelocityException("parser class must provide a constructor taking a RuntimeServices argument", nsme);
+        }
+
+        /*
          * Which parser pool?
          */
         String pp = getString(RuntimeConstants.PARSER_POOL_CLASS);
@@ -1239,6 +1266,16 @@ public class RuntimeInstance implements
             parserPool = (ParserPool) o;
 
             parserPool.initialize(this);
+
+            /*
+             * test parser creation and use generated parser to fill up customized characters
+             */
+            Parser parser = parserPool.get();
+            dollar = parser.dollar();
+            hash = parser.hash();
+            at = parser.at();
+            asterisk = parser.asterisk();
+            parserPool.put(parser);
         }
         else
         {
@@ -1264,8 +1301,14 @@ public class RuntimeInstance implements
     public Parser createNewParser()
     {
         requireInitialization();
-
-        return new StandardParser(this);
+        try
+        {
+            return (Parser)parserConstructor.newInstance((RuntimeServices)this);
+        }
+        catch (IllegalAccessException | InstantiationException | InvocationTargetException e)
+        {
+            throw new VelocityException("could not build new parser class", e);
+        }
     }
 
     /**

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java?rev=1860722&r1=1860721&r2=1860722&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java Thu Jun  6 16:13:47 2019
@@ -31,7 +31,7 @@ import org.apache.velocity.runtime.direc
 import org.apache.velocity.runtime.directive.RuntimeMacro;
 import org.apache.velocity.runtime.parser.ParseException;
 import org.apache.velocity.runtime.parser.Parser;
-import org.apache.velocity.runtime.parser.ParserConstants;
+import org.apache.velocity.runtime.parser.StandardParserConstants;
 import org.apache.velocity.runtime.parser.Token;
 import org.apache.velocity.util.introspection.Info;
 
@@ -143,7 +143,7 @@ public class ASTDirective extends Simple
                 }
 
                 t = getFirstToken();
-                if (t.kind == ParserConstants.WHITESPACE) t = t.next;
+                if (t.kind == StandardParserConstants.WHITESPACE) t = t.next;
                 directive.setLocation(t.beginLine, t.beginColumn, getTemplate());
                 directive.init(rsvc, context, this);
             }

Modified: velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/NodeUtils.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/NodeUtils.java?rev=1860722&r1=1860721&r2=1860722&view=diff
==============================================================================
--- velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/NodeUtils.java (original)
+++ velocity/engine/branches/parser_experiments/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/NodeUtils.java Thu Jun  6 16:13:47 2019
@@ -20,7 +20,7 @@ package org.apache.velocity.runtime.pars
  */
 
 import org.apache.velocity.runtime.parser.Parser;
-import org.apache.velocity.runtime.parser.ParserConstants;
+import org.apache.velocity.runtime.parser.StandardParserConstants;
 import org.apache.velocity.runtime.parser.Token;
 
 /**
@@ -130,7 +130,7 @@ public class NodeUtils
     public static String tokenLiteral( Parser parser, Token t )
     {
         // Look at kind of token and return "" when it's a block comment
-        if (t.kind == ParserConstants.MULTI_LINE_COMMENT)
+        if (t.kind == StandardParserConstants.MULTI_LINE_COMMENT)
         {
             return "";
         }