1 8 9 package net.sourceforge.chaperon.model.lexicon; 10 11 import net.sourceforge.chaperon.model.Violations; 12 import net.sourceforge.chaperon.model.pattern.Pattern; 13 import net.sourceforge.chaperon.model.symbol.Symbol; 14 15 22 public class Lexeme 23 { 24 private Symbol symbol = null; 25 private Pattern definition = null; 26 private String location = null; 27 28 31 public Lexeme() {} 32 33 38 public Lexeme(Symbol symbol) 39 { 40 setSymbol(symbol); 41 } 42 43 48 public void setSymbol(Symbol symbol) 49 { 50 this.symbol = symbol; 51 } 52 53 58 public Symbol getSymbol() 59 { 60 return symbol; 61 } 62 63 68 public void setDefinition(Pattern definition) 69 { 70 this.definition = definition; 71 } 72 73 78 public Pattern getDefinition() 79 { 80 return definition; 81 } 82 83 88 public void setLocation(String location) 89 { 90 this.location = location; 91 } 92 93 98 public String getLocation() 99 { 100 return location; 101 } 102 103 108 public Violations validate() 109 { 110 Violations violations = new Violations(); 111 112 if (definition==null) 113 { 114 if (symbol!=null) 115 violations.addViolation("Lexeme "+symbol+" contains no definition", location); 116 else 117 violations.addViolation("Lexeme contains no definition", location); 118 } 119 120 if ((symbol!=null) && (symbol.getName().equals("error"))) 121 violations.addViolation("Symbol with name \"error\" is not allowed", location); 122 123 return violations; 124 } 125 126 131 public String toString() 132 { 133 if (symbol!=null) 134 return symbol.toString()+" = \""+definition+"\""; 135 136 return "/* noname */ = \""+definition+"\""; 137 } 138 139 146 public Object clone() throws CloneNotSupportedException 147 { 148 Lexeme clone = new Lexeme(); 149 150 clone.symbol = symbol; 151 if (definition!=null) 152 clone.definition = (Pattern)definition.clone(); 153 154 clone.location = location; 155 156 return clone; 157 } 158 } 159 | Popular Tags |