Interface ExpressionMatcher.Context

All Known Implementing Classes:
OrExpressionSolver.Matchers
Enclosing interface:
ExpressionMatcher<T extends PatternExpression>

public static interface ExpressionMatcher.Context
A context for defining expression matcher succinctly

Implementations of this interface have easy access to factory methods for each kind of PatternExpression. Additionally, the class itself provide a convenient container for saving important sub-matchers, so that important sub-expression can be readily retrieved. For example:


 static class MyMatchers implements ExpressionMatcher.Context {
 	ExpressionMatcher<ConstantValue> shamt = var(ConstantValue.class);
 	ExpressionMatcher<LeftShiftExpression> exp = shl(var(), shamt);
 }
 
 static final MyMatchers MATCHERS = new MyMatchers();
 
 public long getConstantShift(PatternExpression expression) {
 	Map<ExpressionMatcher<?>, PatternExpression> result = MATCHERS.exp.match(expression);
 	if (result == null) {
 		return -1;
 	}
 	return MATCHERS.shamt.get(result).getValue();
 }
 

Saving a sub-matcher to a field (as in the example) also permits that sub-matcher to appear in multiple places. In that case, the sub-matcher must match identical expressions wherever it appears. For example, if cv matches any constant value, then plus(cv, cv) would match 2 + 2, but not 2 + 3.