01: package kawa.lang;
02:
03: import gnu.bytecode.*;
04: import gnu.expr.Compilation;
05: import gnu.text.Printable;
06:
07: /**
08: * A Pattern is used to match against objects.
09: * E.g. it can be used to match against macro arguments.
10: * @author Per Bothner
11: */
12:
13: abstract public class Pattern implements Printable {
14: /**
15: * Match this Pattern against an object.
16: * @param obj object to match against this pattern
17: * @return null on failure, or an array of bound pattern variables.
18: */
19: public Object[] match(Object obj) {
20: Object[] vars = new Object[varCount()];
21: return match(obj, vars, 0) ? vars : null;
22: }
23:
24: /** Match this Pattern against an Object.
25: * @param obj the Object to match against
26: * @param vars the "pattern variable" values extracted from obj go here
27: * @param start_vars where in vars to strt putting the varCount() values
28: * @return true iff the match succeeded.
29: */
30: abstract public boolean match(Object obj, Object[] vars,
31: int start_vars);
32:
33: abstract public int varCount();
34:
35: static public ClassType typePattern = ClassType
36: .make("kawa.lang.Pattern");
37: private static Type[] matchArgs = { Type.pointer_type,
38: Compilation.objArrayType, Type.int_type };
39: static public final Method matchPatternMethod = typePattern
40: .addMethod("match", matchArgs, Type.boolean_type,
41: Access.PUBLIC);
42: }
|