You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by cc...@apache.org on 2015/10/07 21:26:52 UTC
[32/37] incubator-groovy git commit: ASTMatcher: Add "eventually"hook
on binary expression (to be expanded to other nodes) to illustrate how to
perform cross-context matching
ASTMatcher: Add "eventually"hook on binary expression (to be expanded to other nodes) to illustrate how to perform cross-context matching
Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/053af2e4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/053af2e4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/053af2e4
Branch: refs/heads/master
Commit: 053af2e4eae6c66adc0e87a87e0acc740206b13e
Parents: 3d6d571
Author: Cedric Champeau <ce...@gmail.com>
Authored: Wed Nov 12 17:57:10 2014 +0100
Committer: Sergei Egorov <bs...@gmail.com>
Committed: Mon Sep 28 14:33:13 2015 +0300
----------------------------------------------------------------------
.../groovy/macro/matcher/ASTMatcher.groovy | 7 ++++++
.../macro/matcher/MatchingConstraints.groovy | 4 +++-
.../groovy/macro/matcher/TreeContext.java | 4 ++++
.../internal/MatchingConstraintsBuilder.groovy | 20 ++++++++++++++++-
.../groovy/macro/matcher/ASTMatcherTest.groovy | 23 ++++++++++++++++++++
5 files changed, 56 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/053af2e4/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 e8d83f7..77bdb0d 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
@@ -544,6 +544,13 @@ class ASTMatcher extends ContextualClassCodeVisitor {
}
})
}
+ failIfNot(ifConstraint(true) {
+ if (eventually) {
+ eventually.apply(treeContext)
+ } else {
+ true
+ }
+ })
}
}
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/053af2e4/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/MatchingConstraints.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/MatchingConstraints.groovy b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/MatchingConstraints.groovy
index ed25d35..ef46b58 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/MatchingConstraints.groovy
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/MatchingConstraints.groovy
@@ -18,6 +18,7 @@ package org.codehaus.groovy.macro.matcher
import groovy.transform.CompileStatic
import groovy.transform.Immutable
+import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.macro.matcher.internal.AnyTokenMatch
import org.codehaus.groovy.macro.matcher.internal.ConstraintPredicate
import org.codehaus.groovy.syntax.Token
@@ -29,11 +30,12 @@ import org.codehaus.groovy.syntax.Token
* @since 2.4.0
*/
@CompileStatic
-@Immutable(knownImmutables = ['tokenPredicate'])
+@Immutable(knownImmutableClasses = [ConstraintPredicate])
class MatchingConstraints {
public final static ConstraintPredicate<Token> ANY_TOKEN = AnyTokenMatch.INSTANCE
final Set<String> placeholders
final ConstraintPredicate<Token> tokenPredicate
+ final ConstraintPredicate<TreeContext> eventually
}
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/053af2e4/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/TreeContext.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/TreeContext.java b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/TreeContext.java
index 4644555..6c77d25 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/TreeContext.java
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/TreeContext.java
@@ -61,6 +61,10 @@ public class TreeContext {
((List)userdata.get(key)).add(value);
}
+ public List<?> getUserdata(Object key) {
+ return getUserdata(key,true);
+ }
+
public List<?> getUserdata(Object key, boolean searchParent) {
if (userdata.containsKey(key)) {
return userdata.get(key);
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/053af2e4/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/internal/MatchingConstraintsBuilder.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/internal/MatchingConstraintsBuilder.groovy b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/internal/MatchingConstraintsBuilder.groovy
index 070013b..5060089 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/internal/MatchingConstraintsBuilder.groovy
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/internal/MatchingConstraintsBuilder.groovy
@@ -16,12 +16,15 @@
package org.codehaus.groovy.macro.matcher.internal
+import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.macro.matcher.MatchingConstraints
+import org.codehaus.groovy.macro.matcher.TreeContext
import org.codehaus.groovy.syntax.Token
class MatchingConstraintsBuilder {
Set<String> placeholders = new LinkedHashSet<>()
ConstraintPredicate<Token> tokenPredicate
+ ConstraintPredicate<TreeContext> eventually
MatchingConstraints build(@DelegatesTo(value=MatchingConstraintsBuilder, strategy=Closure.DELEGATE_ONLY) Closure spec) {
@@ -32,7 +35,9 @@ class MatchingConstraintsBuilder {
new MatchingConstraints(
placeholders: Collections.unmodifiableSet(placeholders),
- tokenPredicate: tokenPredicate)
+ tokenPredicate: tokenPredicate,
+ eventually: eventually
+ )
}
def propertyMissing(String name) {
@@ -61,4 +66,17 @@ class MatchingConstraintsBuilder {
}
this
}
+
+ MatchingConstraintsBuilder eventually(@DelegatesTo(value=TreeContext, strategy = Closure.DELEGATE_FIRST) Closure<Boolean> predicate) {
+ def clone = (Closure<Boolean>) predicate.clone()
+ clone.resolveStrategy = Closure.DELEGATE_FIRST
+ eventually = new ConstraintPredicate<TreeContext>() {
+ @Override
+ boolean apply(final TreeContext a) {
+ clone.delegate = a
+ clone.call(a)
+ }
+ }
+ this
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/053af2e4/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 8b5660e..172f457 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
@@ -746,4 +746,27 @@ class ASTMatcherTest extends GroovyTestCase {
assert ast4.matches(pattern)
}
}
+
+ void testRelationshipMatching() {
+ use (ASTMatcher) {
+ def ast1 = macro { (a + b) + (a + b ) }
+ def ast2 = macro { (a + b) - (a + b ) }
+ def ast3 = macro { (a - b) + (a - b ) }
+ def ast4 = macro { (a + b) + (a - b ) }
+ def ast5 = macro { (a - b) + (a + b ) }
+ def lhs = macro { a + b }.withConstraints { anyToken() }
+ def rhs = macro { a + b }.withConstraints { anyToken() }
+ def pattern = macro { $v{lhs} + $v{rhs} }.withConstraints {
+ eventually {
+ node.leftExpression.operation.type == node.rightExpression.operation.type
+ }
+ }
+ assert ast1.matches(pattern)
+ assert !ast2.matches(pattern)
+ assert ast3.matches(pattern)
+ assert !ast4.matches(pattern)
+ assert !ast5.matches(pattern)
+
+ }
+ }
}