1 package kawa.lang; 2 import gnu.mapping.*; 3 import gnu.lists.*; 4 import java.io.*; 5 import gnu.text.Printable; 6 7 public class ListRepeatPat extends Pattern implements Printable, Externalizable 8 { 9 Pattern element_pattern; 10 11 public ListRepeatPat () 12 { 13 } 14 15 public ListRepeatPat (Pattern element_pattern) 16 { 17 this.element_pattern = element_pattern; 18 } 19 20 public static ListRepeatPat make (Pattern element_pattern) 21 { 22 return new ListRepeatPat (element_pattern); 23 } 24 25 public void print (Consumer out) 26 { 27 out.write("#<list-repeat-pattern "); 28 element_pattern.print(out); 29 out.write('>'); 30 } 31 32 public boolean match (Object obj, Object [] vars, int start_vars) 33 { 34 41 int length = LList.listLength(obj, false); 42 if (length < 0) 43 return false; 44 45 int var_count = element_pattern.varCount (); 46 for (int i = var_count; --i >= 0; ) 47 vars[start_vars + i] = new Object [length]; 48 Object [] element_vars = new Object [var_count]; 49 for (int j = 0; j < length; j++) 50 { 51 Pair pair = (Pair) obj; 52 58 59 if (! element_pattern.match (pair.car, element_vars, 0)) 60 return false; 61 for (int i = 0; i < var_count; i++) 62 ((Object []) vars[start_vars + i]) [j] = element_vars[i]; 63 obj = pair.cdr; 64 } 65 return true; 66 } 67 68 public int varCount () { return element_pattern.varCount (); } 69 70 73 public void writeExternal(ObjectOutput out) throws IOException 74 { 75 out.writeObject(element_pattern); 76 } 77 78 public void readExternal(ObjectInput in) 79 throws IOException, ClassNotFoundException 80 { 81 element_pattern = (Pattern) in.readObject(); 82 } 83 } 84 | Popular Tags |