ListRepeatPat.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package kawa.lang;
  2. import gnu.lists.*;
  3. import java.io.*;
  4. import gnu.kawa.format.Printable;
  5. public class ListRepeatPat extends Pattern implements Printable, Externalizable
  6. {
  7. Pattern element_pattern;
  8. public ListRepeatPat ()
  9. {
  10. }
  11. public ListRepeatPat (Pattern element_pattern)
  12. {
  13. this.element_pattern = element_pattern;
  14. }
  15. public static ListRepeatPat make (Pattern element_pattern)
  16. {
  17. return new ListRepeatPat (element_pattern);
  18. }
  19. public void print (Consumer out)
  20. {
  21. out.write("#<list-repeat-pattern ");
  22. element_pattern.print(out);
  23. out.write('>');
  24. }
  25. public boolean match (Object obj, Object[] vars, int start_vars)
  26. {
  27. /* DEBUGGING
  28. System.err.print ("(match ");
  29. print (System.err);
  30. System.err.print (" on ");
  31. System.err.print(obj);
  32. System.err.print (")\n");
  33. */
  34. int length = LList.listLength(obj, false);
  35. if (length < 0)
  36. return false;
  37. int var_count = element_pattern.varCount ();
  38. for (int i = var_count; --i >= 0; )
  39. vars[start_vars + i] = new Object [length];
  40. Object[] element_vars = new Object [var_count];
  41. for (int j = 0; j < length; j++)
  42. {
  43. Pair pair = (Pair) obj;
  44. /* DEBUGGING
  45. System.err.print ("(sub-match ["+j+"] ");
  46. System.err.print(element_pattern);
  47. System.err.print (" on ");
  48. System.err.print(pair.getCar());
  49. */
  50. if (! element_pattern.match (pair.getCar(), element_vars, 0))
  51. return false;
  52. for (int i = 0; i < var_count; i++)
  53. ((Object[]) vars[start_vars + i]) [j] = element_vars[i];
  54. obj = pair.getCdr();
  55. }
  56. return true;
  57. }
  58. public int varCount () { return element_pattern.varCount (); }
  59. /**
  60. * @serialData Write the element_pattern (using writeObject).
  61. */
  62. public void writeExternal(ObjectOutput out) throws IOException
  63. {
  64. out.writeObject(element_pattern);
  65. }
  66. public void readExternal(ObjectInput in)
  67. throws IOException, ClassNotFoundException
  68. {
  69. element_pattern = (Pattern) in.readObject();
  70. }
  71. }