groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cchamp...@apache.org
Subject [25/37] incubator-groovy git commit: ASTMatcher: Add support for if/else
Date Wed, 07 Oct 2015 19:26:45 GMT
ASTMatcher: Add support for if/else


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/8c1f2634
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/8c1f2634
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/8c1f2634

Branch: refs/heads/master
Commit: 8c1f263477e31f6a5339655b94734c0e5f98dd03
Parents: 817bbef
Author: Cedric Champeau <cedric.champeau@gmail.com>
Authored: Fri Oct 17 13:39:20 2014 +0200
Committer: Sergei Egorov <bsideup@gmail.com>
Committed: Mon Sep 28 14:33:11 2015 +0300

----------------------------------------------------------------------
 .../groovy/macro/matcher/ASTMatcher.groovy      | 25 +++++++++++++++++++-
 .../groovy/macro/matcher/ASTMatcherTest.groovy  | 22 +++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/8c1f2634/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
index 7b5262c..d715737 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
@@ -20,7 +20,9 @@ import groovy.transform.TypeCheckingMode
 import org.codehaus.groovy.ast.*
 import org.codehaus.groovy.ast.expr.*
 import org.codehaus.groovy.ast.stmt.BlockStatement
+import org.codehaus.groovy.ast.stmt.EmptyStatement
 import org.codehaus.groovy.ast.stmt.ExpressionStatement
+import org.codehaus.groovy.ast.stmt.IfStatement
 import org.codehaus.groovy.ast.stmt.Statement
 import org.codehaus.groovy.classgen.BytecodeExpression
 import org.codehaus.groovy.control.SourceUnit
@@ -391,8 +393,8 @@ class ASTMatcher extends ClassCodeVisitorSupport {
 
     @Override
     void visitExpressionStatement(final ExpressionStatement statement) {
-        visitStatement(statement)
         doWithNode(statement.expression.class, ((ExpressionStatement) current).expression)
{
+            visitStatement(statement)
             statement.expression.visit(this)
         }
     }
@@ -835,4 +837,25 @@ class ASTMatcher extends ClassCodeVisitorSupport {
     public void visitBytecodeExpression(final BytecodeExpression cle) {
         super.visitBytecodeExpression(cle);
     }
+
+    @Override
+    void visitIfElse(final IfStatement ifElse) {
+        doWithNode(IfStatement, current) {
+            visitStatement(ifElse)
+            def cur = (IfStatement) current
+            def bool = ifElse.booleanExpression
+            def ifBlock = ifElse.ifBlock
+            def elseBlock = ifElse.elseBlock
+            doWithNode(bool.class, cur.booleanExpression) {
+                bool.visit(this)
+            }
+            doWithNode(ifBlock.class, cur.ifBlock) {
+                ifBlock.visit(this)
+            }
+            failIfNot(elseBlock && cur.elseBlock || !elseBlock && !cur.elseBlock)
+            doWithNode(elseBlock.class, cur.elseBlock) {
+                elseBlock.visit(this)
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/8c1f2634/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
index ef7c489..eec19b9 100644
--- a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
+++ b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
@@ -26,6 +26,7 @@ import org.codehaus.groovy.ast.expr.MethodPointerExpression
 import org.codehaus.groovy.ast.expr.StaticMethodCallExpression
 import org.codehaus.groovy.ast.expr.UnaryMinusExpression
 import org.codehaus.groovy.ast.expr.UnaryPlusExpression
+import org.codehaus.groovy.ast.stmt.IfStatement
 import org.codehaus.groovy.control.CompilePhase
 import org.codehaus.groovy.macro.transform.MacroClass
 
@@ -515,4 +516,25 @@ class ASTMatcherTest extends GroovyTestCase {
         assert !ASTMatcher.matches(ast8, ast9)
         assert !ASTMatcher.matches(ast1, ast10)
     }
+
+    void testIf() {
+        def ast1 = macro { if (a) b }
+        def ast2 = macro { if (a) b }
+        def ast3 = macro { if (a) c }
+        def ast4 = macro { if (b) b }
+        def ast5 = macro { if (a) { b } }
+        def ast6 = macro { if (a) { b } else { c }}
+        def ast7 = macro { if (a) { b } else { c }}
+        def ast8 = macro { if (a) { b } else { d }}
+        assert ast1 instanceof IfStatement
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast4)
+        assert !ASTMatcher.matches(ast1, ast5)
+        assert !ASTMatcher.matches(ast1, ast6)
+        assert ASTMatcher.matches(ast6, ast7)
+        assert !ASTMatcher.matches(ast7, ast8)
+    }
 }


Mime
View raw message