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