flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joshtynj...@apache.org
Subject [2/3] git commit: [flex-falcon] [refs/heads/develop] - compiler.jx: started on source maps for switch statements
Date Wed, 27 Apr 2016 23:34:37 GMT
compiler.jx: started on source maps for switch statements


Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/18d2fd69
Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/18d2fd69
Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/18d2fd69

Branch: refs/heads/develop
Commit: 18d2fd69b1653bd54fa7ad5b9b8b164f3a3e03df
Parents: 518b959
Author: Josh Tynjala <joshtynjala@gmail.com>
Authored: Wed Apr 27 16:34:18 2016 -0700
Committer: Josh Tynjala <joshtynjala@gmail.com>
Committed: Wed Apr 27 16:34:18 2016 -0700

----------------------------------------------------------------------
 .../compiler/internal/codegen/js/JSEmitter.java | 10 ++
 .../internal/codegen/js/jx/SwitchEmitter.java   | 98 ++++++++++++++++++++
 .../js/sourcemaps/TestSourceMapStatements.java  | 23 +++++
 3 files changed, 131 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/18d2fd69/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/JSEmitter.java
----------------------------------------------------------------------
diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/JSEmitter.java
b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/JSEmitter.java
index d0a4aa8..d7db258 100644
--- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/JSEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/JSEmitter.java
@@ -48,6 +48,7 @@ import org.apache.flex.compiler.internal.codegen.js.jx.ParametersEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.ReturnEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.SourceMapDirectiveEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.StatementEmitter;
+import org.apache.flex.compiler.internal.codegen.js.jx.SwitchEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.TernaryOperatorEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.UnaryOperatorEmitter;
 import org.apache.flex.compiler.internal.codegen.js.jx.WhileLoopEmitter;
@@ -69,6 +70,7 @@ import org.apache.flex.compiler.tree.as.IObjectLiteralValuePairNode;
 import org.apache.flex.compiler.tree.as.IPackageNode;
 import org.apache.flex.compiler.tree.as.IParameterNode;
 import org.apache.flex.compiler.tree.as.IReturnNode;
+import org.apache.flex.compiler.tree.as.ISwitchNode;
 import org.apache.flex.compiler.tree.as.ITernaryOperatorNode;
 import org.apache.flex.compiler.tree.as.ITypeNode;
 import org.apache.flex.compiler.tree.as.ITypedExpressionNode;
@@ -98,6 +100,7 @@ public class JSEmitter extends ASEmitter implements IJSEmitter
     public TernaryOperatorEmitter ternaryOperatorEmitter;
     public MemberKeywordEmitter memberKeywordEmitter;
     public IfEmitter ifEmitter;
+    public SwitchEmitter switchEmitter;
     public WhileLoopEmitter whileLoopEmitter;
     public DoWhileLoopEmitter doWhileLoopEmitter;
     public ForLoopEmitter forLoopEmitter;
@@ -144,6 +147,7 @@ public class JSEmitter extends ASEmitter implements IJSEmitter
         ternaryOperatorEmitter = new TernaryOperatorEmitter(this);
         memberKeywordEmitter = new MemberKeywordEmitter(this);
         ifEmitter = new IfEmitter(this);
+        switchEmitter = new SwitchEmitter(this);
         whileLoopEmitter = new WhileLoopEmitter(this);
         doWhileLoopEmitter = new DoWhileLoopEmitter(this);
         forLoopEmitter = new ForLoopEmitter(this);
@@ -288,6 +292,12 @@ public class JSEmitter extends ASEmitter implements IJSEmitter
     }
 
     @Override
+    public void emitSwitch(ISwitchNode node)
+    {
+        switchEmitter.emit(node);
+    }
+
+    @Override
     public void emitWhileLoop(IWhileLoopNode node)
     {
         whileLoopEmitter.emit(node);

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/18d2fd69/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/SwitchEmitter.java
----------------------------------------------------------------------
diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/SwitchEmitter.java
b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/SwitchEmitter.java
new file mode 100644
index 0000000..ce14b14
--- /dev/null
+++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/SwitchEmitter.java
@@ -0,0 +1,98 @@
+/*
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.flex.compiler.internal.codegen.js.jx;
+
+import org.apache.flex.compiler.codegen.ISubEmitter;
+import org.apache.flex.compiler.codegen.js.IJSEmitter;
+import org.apache.flex.compiler.internal.codegen.as.ASEmitterTokens;
+import org.apache.flex.compiler.internal.codegen.js.JSSubEmitter;
+import org.apache.flex.compiler.internal.codegen.js.utils.EmitterUtils;
+import org.apache.flex.compiler.tree.as.IASNode;
+import org.apache.flex.compiler.tree.as.IConditionalNode;
+import org.apache.flex.compiler.tree.as.IContainerNode;
+import org.apache.flex.compiler.tree.as.IStatementNode;
+import org.apache.flex.compiler.tree.as.ISwitchNode;
+import org.apache.flex.compiler.tree.as.ITerminalNode;
+import org.apache.flex.compiler.utils.ASNodeUtils;
+
+public class SwitchEmitter extends JSSubEmitter implements
+        ISubEmitter<ISwitchNode>
+{
+    public SwitchEmitter(IJSEmitter emitter)
+    {
+        super(emitter);
+    }
+
+    @Override
+    public void emit(ISwitchNode node)
+    {
+        startMapping(node);
+        writeToken(ASEmitterTokens.SWITCH);
+        write(ASEmitterTokens.PAREN_OPEN);
+        endMapping(node);
+        IASNode expressionNode = node.getChild(0);
+        getWalker().walk(expressionNode);
+        startMapping(node, expressionNode);
+        writeToken(ASEmitterTokens.PAREN_CLOSE);
+        endMapping(node);
+        IASNode statementContentsNode = node.getStatementContentsNode();
+        startMapping(statementContentsNode);
+        write(ASEmitterTokens.BLOCK_OPEN);
+        endMapping(statementContentsNode);
+        indentPush();
+        writeNewline();
+
+        IConditionalNode[] cnodes = ASNodeUtils.getCaseNodes(node);
+        ITerminalNode dnode = ASNodeUtils.getDefaultNode(node);
+        
+        for (int i = 0; i < cnodes.length; i++)
+        {
+            IConditionalNode casen = cnodes[i];
+            IContainerNode cnode = (IContainerNode) casen.getChild(1);
+            writeToken(ASEmitterTokens.CASE);
+            getWalker().walk(casen.getConditionalExpressionNode());
+            write(ASEmitterTokens.COLON);
+            if (!EmitterUtils.isImplicit(cnode))
+                write(ASEmitterTokens.SPACE);
+            getWalker().walk(casen.getStatementContentsNode());
+            if (i == cnodes.length - 1 && dnode == null)
+            {
+                indentPop();
+                writeNewline();
+            }
+            else
+                writeNewline();
+        }
+        if (dnode != null)
+        {
+            IContainerNode cnode = (IContainerNode) dnode.getChild(0);
+            write(ASEmitterTokens.DEFAULT);
+            write(ASEmitterTokens.COLON);
+            if (!EmitterUtils.isImplicit(cnode))
+                write(ASEmitterTokens.SPACE);
+            getWalker().walk(dnode);
+            indentPop();
+            writeNewline();
+        }
+        startMapping(node, node.getEndLine(), node.getEndColumn() - 1);
+        write(ASEmitterTokens.BLOCK_CLOSE);
+        endMapping(node);
+    }
+}

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/18d2fd69/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/sourcemaps/TestSourceMapStatements.java
----------------------------------------------------------------------
diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/sourcemaps/TestSourceMapStatements.java
b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/sourcemaps/TestSourceMapStatements.java
index 265b94c..4f8f158 100644
--- a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/sourcemaps/TestSourceMapStatements.java
+++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/sourcemaps/TestSourceMapStatements.java
@@ -5,6 +5,7 @@ import org.apache.flex.compiler.internal.driver.js.flexjs.FlexJSBackend;
 import org.apache.flex.compiler.internal.test.SourceMapTestBase;
 import org.apache.flex.compiler.tree.as.IForLoopNode;
 import org.apache.flex.compiler.tree.as.IIfNode;
+import org.apache.flex.compiler.tree.as.ISwitchNode;
 import org.apache.flex.compiler.tree.as.IVariableNode;
 
 import org.junit.Test;
@@ -186,6 +187,28 @@ public class TestSourceMapStatements extends SourceMapTestBase
     }
 
     //----------------------------------
+    // switch {}
+    //----------------------------------
+
+    @Test
+    public void testVisitSwitch_1()
+    {
+        ISwitchNode node = (ISwitchNode) getNode("switch(i){case 1: break;}",
+                ISwitchNode.class);
+        asBlockWalker.visitSwitch(node);
+        //switch (i) {\n  case 1:\n    break;\n}
+        assertMapping(node, 0, 0, 0, 0, 0, 8);     // switch (
+        assertMapping(node, 0, 7, 0, 8, 0, 9);     // i
+        assertMapping(node, 0, 8, 0, 9, 0, 11);    // )
+        assertMapping(node, 0, 9, 0, 11, 0, 12);   // {
+        //assertMapping(node, 0, 10, 1, 2, 1, 7);  // case
+        assertMapping(node, 0, 15, 1, 7, 1, 8);    // 1
+        //assertMapping(node, 0, 16, 1, 8, 1, 9);    // :
+        assertMapping(node, 0, 18, 2, 4, 2, 9);    // break
+        assertMapping(node, 0, 24, 3, 0, 3, 1);    // }
+    }
+
+    //----------------------------------
     // if ()
     //----------------------------------
 


Mime
View raw message