Skip to content

Instantly share code, notes, and snippets.

@melix
Created November 12, 2014 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/6875e12077908129f400 to your computer and use it in GitHub Desktop.
Save melix/6875e12077908129f400 to your computer and use it in GitHub Desktop.
AST pattern matching with relaxed constraints 2
void testInlineMacroCombinationWithConstraints() {
use(ASTMatcher) {
def ast1 = macro { a + b }
def ast2 = macro { b + b }
def ast3 = macro { b + c }
def ast4 = macro { b - b }
def pattern = macro {
$v { macro { a }.withConstraints { placeholder a } } + b
}.withConstraints {
anyToken()
}
assert pattern instanceof BinaryExpression
assert ast1.matches(pattern)
assert ast2.matches(pattern)
assert !ast3.matches(pattern)
assert ast4.matches(pattern)
}
}
@melix
Copy link
Author

melix commented Nov 12, 2014

The same pattern can be written:

def pattern = macro {
                a + b
            }.withConstraints {
                placeholder a
                anyToken()
            }

The idea of the previous example is more to highlight the ability to put constraints on specific parts of the pattern and combine ASTs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment