1 8 9 package net.sourceforge.chaperon.process.extended; 10 11 import net.sourceforge.chaperon.model.extended.Element; 12 import net.sourceforge.chaperon.model.extended.Pattern; 13 14 22 public class Item 23 { 24 public Item next = null; 25 26 public String symbol; 28 public Pattern previous; 29 30 public Pattern pattern; 32 33 public int position; 35 public static final int SHIFT = 0; 36 public static final int GOTO = 1; 37 public static final int REDUCE = 2; 38 39 public Pattern lookahead; 41 public boolean end = false; 42 43 public Item(String symbol, Pattern pattern, int position, Pattern lookahead) 44 { 45 if (symbol==null) 46 throw new IllegalArgumentException ("Symbol is null"); 47 48 if (pattern==null) 49 throw new IllegalArgumentException ("Pattern is null"); 50 51 if ((position<0) && (position>2)) 52 throw new IllegalArgumentException ("Only item positions 0,1 and 2 are allowed"); 53 54 if (lookahead instanceof Element) 55 throw new IllegalStateException ("bla"); 56 57 this.symbol = symbol; 58 this.previous = null; 59 this.pattern = pattern; 60 this.position = position; 61 62 this.lookahead = lookahead; 64 } 65 66 public Item(Pattern previous, Pattern pattern, int position, Pattern lookahead) 67 { 68 if (previous==null) 69 throw new IllegalArgumentException ("Previous pattern is null"); 70 71 if (pattern==null) 72 throw new IllegalArgumentException ("Pattern is null"); 73 74 if ((position<0) && (position>2)) 75 throw new IllegalArgumentException ("Only item positions 0,1 and 2 are allowed"); 76 77 if (lookahead instanceof Element) 78 throw new IllegalStateException ("bla"); 79 80 this.symbol = null; 81 this.previous = previous; 82 this.pattern = pattern; 83 this.position = position; 84 85 this.lookahead = lookahead; 87 } 88 89 public Item getFollowItem() 90 { 91 if (position<REDUCE) 92 { 93 if (symbol!=null) 94 return new Item(symbol, pattern, position+1, lookahead); 95 96 return new Item(previous, pattern, position+1, lookahead); 97 } 98 else 99 return null; 100 } 101 102 public boolean equals(Object o) 103 { 104 if (o instanceof Item) 105 { 106 Item item = (Item)o; 107 108 if (symbol!=null) 109 return (item.symbol!=null) && symbol.equals(item.symbol) && (pattern==item.pattern) && 110 (position==item.position) && (lookahead==item.lookahead); 111 112 return (previous==item.previous) && (pattern==item.pattern) && (position==item.position) && 113 (lookahead==item.lookahead); 114 } 115 116 return false; 117 } 118 119 public String toString() 120 { 121 StringBuffer buffer = new StringBuffer (); 122 123 if (symbol!=null) 124 buffer.append(symbol); 125 else 126 buffer.append(previous); 127 128 buffer.append(" := "); 129 130 if (position==SHIFT) 131 buffer.append("."); 132 133 buffer.append(pattern); 134 buffer.append(" "); 135 136 if (position==GOTO) 137 buffer.append("."); 138 139 buffer.append(pattern); 140 141 if (position==REDUCE) 142 buffer.append("."); 143 144 buffer.append(","); 145 buffer.append(lookahead); 146 147 return buffer.toString(); 148 } 149 } 150 | Popular Tags |