Pattern.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package kawa.lang;
  2. import gnu.bytecode.*;
  3. import gnu.expr.Compilation;
  4. import gnu.lists.Consumer;
  5. import gnu.kawa.format.Printable;
  6. /**
  7. * A Pattern is used to match against objects.
  8. * E.g. it can be used to match against macro arguments.
  9. * @author Per Bothner
  10. */
  11. abstract public class Pattern implements Printable
  12. {
  13. /**
  14. * Match this Pattern against an object.
  15. * @param obj object to match against this pattern
  16. * @return null on failure, or an array of bound pattern variables.
  17. */
  18. public Object[] match (Object obj)
  19. {
  20. Object[] vars = new Object [varCount ()];
  21. return match (obj, vars, 0) ? vars : null;
  22. }
  23. /** Match this Pattern against an Object.
  24. * @param obj the Object to match against
  25. * @param vars the "pattern variable" values extracted from obj go here
  26. * @param start_vars where in vars to strt putting the varCount() values
  27. * @return true iff the match succeeded.
  28. */
  29. abstract public boolean match (Object obj, Object[] vars, int start_vars);
  30. abstract public int varCount ();
  31. static public ClassType typePattern = ClassType.make("kawa.lang.Pattern");
  32. private static Type[] matchArgs =
  33. { Type.pointer_type, Compilation.objArrayType, Type.intType };
  34. static public final Method matchPatternMethod
  35. = typePattern.addMethod("match", matchArgs, Type.booleanType, Access.PUBLIC);
  36. public void print(Consumer out) {
  37. out.write(toString());
  38. }
  39. }