freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [31/50] incubator-freemarker git commit: Some internal AST oversight fixes, and a new internal parser setting, preventStrippings. These are needed for the FM2 to FM3 converter.
Date Sat, 05 Aug 2017 14:26:51 GMT
Some internal AST oversight fixes, and a new internal parser setting, preventStrippings. These
are needed for the FM2 to FM3 converter.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/9b879885
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/9b879885
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/9b879885

Branch: refs/heads/2.3
Commit: 9b879885406d687595fb7dafd772087dd83541cb
Parents: a36ea52
Author: ddekany <ddekany@apache.org>
Authored: Sat Jul 8 00:53:30 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Sat Jul 8 00:53:30 2017 +0200

----------------------------------------------------------------------
 .../java/freemarker/core/NumericalOutput.java   |  4 ++--
 src/main/java/freemarker/core/TextBlock.java    |  4 ++--
 .../java/freemarker/core/TrimInstruction.java   |  8 +++----
 src/main/java/freemarker/core/_CoreAPI.java     |  4 ++++
 .../java/freemarker/template/Configuration.java | 22 +++++++++++++++++++-
 src/main/java/freemarker/template/Template.java |  9 +++++---
 .../java/freemarker/template/_TemplateAPI.java  |  4 ++++
 src/main/javacc/FTL.jj                          | 15 ++++++++++---
 .../freemarker/core/ast-strlitinterpolation.ast |  4 ++--
 9 files changed, 57 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/core/NumericalOutput.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/NumericalOutput.java b/src/main/java/freemarker/core/NumericalOutput.java
index bd1ed11..08e8301 100644
--- a/src/main/java/freemarker/core/NumericalOutput.java
+++ b/src/main/java/freemarker/core/NumericalOutput.java
@@ -150,8 +150,8 @@ final class NumericalOutput extends Interpolation {
     Object getParameterValue(int idx) {
         switch (idx) {
         case 0: return expression;
-        case 1: return Integer.valueOf(minFracDigits);
-        case 2: return Integer.valueOf(maxFracDigits);
+        case 1: return hasFormat ? Integer.valueOf(minFracDigits) : null;
+        case 2: return hasFormat ? Integer.valueOf(maxFracDigits) : null;
         default: throw new IndexOutOfBoundsException();
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/core/TextBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/TextBlock.java b/src/main/java/freemarker/core/TextBlock.java
index fb343fa..8be305b 100644
--- a/src/main/java/freemarker/core/TextBlock.java
+++ b/src/main/java/freemarker/core/TextBlock.java
@@ -143,8 +143,8 @@ public final class TextBlock extends TemplateElement {
     private boolean deliberateLeftTrim() {
         boolean result = false;
         for (TemplateElement elem = this.nextTerminalNode(); 
-             elem != null && elem.beginLine == this.endLine;
-             elem = elem.nextTerminalNode()) {
+            elem != null && elem.beginLine == this.endLine;
+            elem = elem.nextTerminalNode()) {
             if (elem instanceof TrimInstruction) {
                 TrimInstruction ti = (TrimInstruction) elem;
                 if (!ti.left && !ti.right) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/core/TrimInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/TrimInstruction.java b/src/main/java/freemarker/core/TrimInstruction.java
index 1279b99..a563a8a 100644
--- a/src/main/java/freemarker/core/TrimInstruction.java
+++ b/src/main/java/freemarker/core/TrimInstruction.java
@@ -25,10 +25,10 @@ package freemarker.core;
  */
 final class TrimInstruction extends TemplateElement {
     
-    private static final int TYPE_T = 0;  
-    private static final int TYPE_LT = 1;  
-    private static final int TYPE_RT = 2;  
-    private static final int TYPE_NT = 3;  
+    static final int TYPE_T = 0;  
+    static final int TYPE_LT = 1;  
+    static final int TYPE_RT = 2;  
+    static final int TYPE_NT = 3;  
 
     final boolean left, right;
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/core/_CoreAPI.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/_CoreAPI.java b/src/main/java/freemarker/core/_CoreAPI.java
index 66442da..1f10f81 100644
--- a/src/main/java/freemarker/core/_CoreAPI.java
+++ b/src/main/java/freemarker/core/_CoreAPI.java
@@ -213,5 +213,9 @@ public class _CoreAPI {
     public static TemplateElement getChildElement(TemplateElement te, int index) {
         return te.getChild(index);
     }
+
+    public static void setPreventStrippings(FMParser parser, boolean preventStrippings) {
+        parser.setPreventStrippings(preventStrippings);
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/template/Configuration.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/Configuration.java b/src/main/java/freemarker/template/Configuration.java
index 57b0121..5956a39 100644
--- a/src/main/java/freemarker/template/Configuration.java
+++ b/src/main/java/freemarker/template/Configuration.java
@@ -499,7 +499,8 @@ public class Configuration extends Configurable implements Cloneable,
ParserConf
     private Version incompatibleImprovements;
     private int tagSyntax = ANGLE_BRACKET_TAG_SYNTAX;
     private int namingConvention = AUTO_DETECT_NAMING_CONVENTION;
-    private int tabSize = 8;  // Default from JavaCC 3.x 
+    private int tabSize = 8;  // Default from JavaCC 3.x
+    private boolean preventStrippings;
 
     private TemplateCache cache;
     
@@ -2399,6 +2400,25 @@ public class Configuration extends Configurable implements Cloneable,
ParserConf
     }
     
     /**
+     * Getter pair of {@link #setPreventStrippings(boolean)}.
+     * 
+     * @since 2.3.27
+     */
+    boolean getPreventStrippings() {
+        return preventStrippings;
+    }
+
+    /**
+     * Used internally; added for the FreeMarker 2 to FreeMarker 3 converter, prevents the
stripping/removal of AST
+     * nodes so that the source code can be fully reproduced from the AST.
+     * 
+     * @since 2.3.27
+     */
+    void setPreventStrippings(boolean preventStrippings) {
+        this.preventStrippings = preventStrippings;
+    }
+
+    /**
      * Retrieves the template with the given name from the template cache, loading it into
the cache first if it's
      * missing/staled.
      * 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/template/Template.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/Template.java b/src/main/java/freemarker/template/Template.java
index 6c2aa4e..240e527 100644
--- a/src/main/java/freemarker/template/Template.java
+++ b/src/main/java/freemarker/template/Template.java
@@ -225,9 +225,9 @@ public class Template extends Configurable {
      * @since 2.3.24
      */
     public Template(
-            String name, String sourceName, Reader reader,
-            Configuration cfg, ParserConfiguration customParserConfiguration,
-            String encoding) throws IOException {
+           String name, String sourceName, Reader reader,
+           Configuration cfg, ParserConfiguration customParserConfiguration,
+           String encoding) throws IOException {
         this(name, sourceName, cfg, customParserConfiguration);
         
         this.setEncoding(encoding);
@@ -243,6 +243,9 @@ public class Template extends Configurable {
             
             try {
                 FMParser parser = new FMParser(this, reader, actualParserConfiguration);
+                if (cfg != null) {
+                    _CoreAPI.setPreventStrippings(parser, cfg.getPreventStrippings());
+                }
                 try {
                     this.rootElement = parser.Root();
                 } catch (IndexOutOfBoundsException exc) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/java/freemarker/template/_TemplateAPI.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/_TemplateAPI.java b/src/main/java/freemarker/template/_TemplateAPI.java
index 373e0e6..22cbc21 100644
--- a/src/main/java/freemarker/template/_TemplateAPI.java
+++ b/src/main/java/freemarker/template/_TemplateAPI.java
@@ -164,5 +164,9 @@ public class _TemplateAPI {
     public static TimeZone getDefaultTimeZone() {
         return Configuration.getDefaultTimeZone();
     }
+
+    public static void setPreventStrippings(Configuration conf, boolean preventStrippings)
{
+        conf.setPreventStrippings(preventStrippings);
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/main/javacc/FTL.jj
----------------------------------------------------------------------
diff --git a/src/main/javacc/FTL.jj b/src/main/javacc/FTL.jj
index 75c7784..94338af 100644
--- a/src/main/javacc/FTL.jj
+++ b/src/main/javacc/FTL.jj
@@ -70,7 +70,7 @@ public class FMParser {
 
     private Template template;
 
-    private boolean stripWhitespace, stripText;
+    private boolean stripWhitespace, stripText, preventStrippings;
     private int incompatibleImprovements;
     private OutputFormat outputFormat;
     private int autoEscapingPolicy;
@@ -291,6 +291,13 @@ public class FMParser {
         parentTokenSource.namingConvention = token_source.namingConvention;
         parentTokenSource.namingConventionEstabilisher = token_source.namingConventionEstabilisher;
     }
+    
+    /**
+     * Used when we need to recreate the source code from the AST (such as for the FM2 to
FM3 converter).
+     */
+    void setPreventStrippings(boolean preventStrippings) {
+        this.preventStrippings = preventStrippings;
+    }
 
     private OutputFormat getFormatFromStdFileExt() {
         String sourceName = template.getSourceName();
@@ -3975,7 +3982,7 @@ TextBlock PCData() :
         }
     )+
     {
-        if (stripText && mixedContentNesting == 1) return null;
+        if (stripText && mixedContentNesting == 1 && !preventStrippings)
return null;
 
         TextBlock result = new TextBlock(buf.toString(), false);
         result.setLocation(template, start, t);
@@ -4383,7 +4390,9 @@ TemplateElement Root() :
     {
         TemplateElement root = children.asSingleElement(); 
         root.setFieldsForRootElement();
-        root = root.postParseCleanup(stripWhitespace);
+        if (!preventStrippings) {
+            root = root.postParseCleanup(stripWhitespace);
+        }
         // The cleanup result is possibly an element from deeper:
         root.setFieldsForRootElement();
         return root;

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9b879885/src/test/resources/freemarker/core/ast-strlitinterpolation.ast
----------------------------------------------------------------------
diff --git a/src/test/resources/freemarker/core/ast-strlitinterpolation.ast b/src/test/resources/freemarker/core/ast-strlitinterpolation.ast
index 7d14e80..e8130bb 100644
--- a/src/test/resources/freemarker/core/ast-strlitinterpolation.ast
+++ b/src/test/resources/freemarker/core/ast-strlitinterpolation.ast
@@ -60,8 +60,8 @@
                 - content: x  // f.c.Identifier
             - value part: #{...}  // f.c.NumericalOutput
                 - content: y  // f.c.Identifier
-                - minimum decimals: "0"  // Integer
-                - maximum decimals: "0"  // Integer
+                - minimum decimals: null  // Null
+                - maximum decimals: null  // Null
     #text  // f.c.TextBlock
         - content: "\n6. "  // String
     ${...}  // f.c.DollarVariable


Mime
View raw message