1 8 9 package net.sourceforge.chaperon.model.grammar; 10 11 import net.sourceforge.chaperon.model.Violations; 12 import net.sourceforge.chaperon.model.symbol.Nonterminal; 13 import net.sourceforge.chaperon.model.symbol.SymbolList; 14 import net.sourceforge.chaperon.model.symbol.SymbolSet; 15 import net.sourceforge.chaperon.model.symbol.Terminal; 16 17 import java.io.Serializable ; 18 19 25 public class Production implements Serializable , Cloneable 26 { 27 private Nonterminal ntsymbol = new Nonterminal("noname"); 28 private SymbolList definition = new SymbolList(); 29 private Terminal precedence = null; 30 private String location = null; 31 32 35 public Production() {} 36 37 42 public Production(Nonterminal ntsymbol) 43 { 44 setSymbol(ntsymbol); 45 } 46 47 53 public Production(Nonterminal ntsymbol, SymbolList definition) 54 { 55 setSymbol(ntsymbol); 56 setDefinition(definition); 57 } 58 59 64 public void setSymbol(Nonterminal ntsymbol) 65 { 66 if (ntsymbol==null) 67 throw new NullPointerException (); 68 69 this.ntsymbol = ntsymbol; 70 } 71 72 77 public Nonterminal getSymbol() 78 { 79 return ntsymbol; 80 } 81 82 87 public void setDefinition(SymbolList definition) 88 { 89 if (definition==null) 90 throw new NullPointerException (); 91 92 this.definition = definition; 93 } 94 95 100 public SymbolList getDefinition() 101 { 102 return definition; 103 } 104 105 110 public int getLength() 111 { 112 return definition.getSymbolCount(); 113 } 114 115 120 public void setPrecedence(Terminal tsymbol) 121 { 122 precedence = tsymbol; 123 } 124 125 130 public Terminal getPrecedence() 131 { 132 return precedence; 133 } 134 135 140 public boolean hasPrecedence() 141 { 142 return precedence!=null; 143 } 144 145 150 public SymbolSet getSymbols() 151 { 152 SymbolSet set = new SymbolSet(); 153 154 set.addSymbol(ntsymbol); 155 set.addSymbol(definition); 156 if (precedence!=null) 157 set.addSymbol(precedence); 158 159 return set; 160 } 161 162 167 public void setLocation(String location) 168 { 169 this.location = location; 170 } 171 172 177 public String getLocation() 178 { 179 return location; 180 } 181 182 187 public Violations validate() 188 { 189 Violations violations = new Violations(); 190 191 if (ntsymbol==null) 192 violations.addViolation("No symbol is for the left side defined", location); 193 194 197 return violations; 198 } 199 200 207 public boolean equals(Object o) 208 { 209 if (o==this) 210 return true; 211 212 if (o instanceof Production) 213 { 214 Production production = (Production)o; 215 216 if (precedence!=null) 217 return (ntsymbol.equals(production.ntsymbol)) && 218 (definition.equals(production.definition)) && 219 (precedence.equals(production.precedence)); 220 else 221 return (ntsymbol.equals(production.ntsymbol)) && 222 (definition.equals(production.definition)) && (production.precedence==null); 223 } 224 225 return false; 226 } 227 228 233 public String toString() 234 { 235 StringBuffer buffer = new StringBuffer (); 236 237 buffer.append(ntsymbol+" := "); 238 for (int j = 0; j<definition.getSymbolCount(); j++) 239 buffer.append(definition.getSymbol(j)+" "); 240 241 if (hasPrecedence()) 242 buffer.append("[precedence="+getPrecedence()+"]"); 243 244 return buffer.toString(); 245 } 246 247 252 public Object clone() 253 { 254 Production clone = new Production(); 255 256 clone.ntsymbol = ntsymbol; 257 clone.definition = (SymbolList)definition.clone(); 258 clone.precedence = precedence; 259 clone.location = location; 260 261 return clone; 262 } 263 } 264 | Popular Tags |