1 23 24 package org.gjt.sp.jedit.syntax; 25 26 import java.util.*; 28 import java.util.regex.Pattern ; 29 31 36 public class ParserRuleSet 37 { 38 44 public static ParserRuleSet getStandardRuleSet(byte id) 45 { 46 return standard[id]; 47 } 49 public ParserRuleSet(String modeName, String setName) 51 { 52 this.modeName = modeName; 53 this.setName = setName; 54 ruleMap = new HashMap<Character , List<ParserRule>>(); 55 imports = new ArrayList<ParserRuleSet>(); 56 } 58 public String getModeName() 60 { 61 return modeName; 62 } 64 public String getSetName() 66 { 67 return setName; 68 } 70 public String getName() 72 { 73 return modeName + "::" + setName; 74 } 76 public Hashtable<String , String > getProperties() 78 { 79 return props; 80 } 82 public void setProperties(Hashtable<String , String > props) 84 { 85 this.props = props; 86 _noWordSep = null; 87 } 89 94 public void resolveImports() 95 { 96 for (ParserRuleSet ruleset : imports) 97 { 98 if (!ruleset.imports.isEmpty()) 99 { 100 ruleset.imports.remove(this); 102 ruleset.resolveImports(); 103 } 104 105 for (List<ParserRule> rules : ruleset.ruleMap.values()) 106 { 107 for (ParserRule rule : rules) 108 { 109 addRule(rule); 110 } 111 } 112 113 if (ruleset.keywords != null) { 114 if (keywords == null) 115 keywords = new KeywordMap(ignoreCase); 116 keywords.add(ruleset.keywords); 117 } 118 } 119 imports.clear(); 120 } 122 128 public void addRuleSet(ParserRuleSet ruleset) 129 { 130 imports.add(ruleset); 131 } 133 public void addRule(ParserRule r) 135 { 136 ruleCount++; 137 Character [] keys; 138 if (null == r.upHashChars) 139 { 140 keys = new Character [1]; 141 if ((null == r.upHashChar) || (0 >= r.upHashChar.length())) 142 { 143 keys[0] = null; 144 } 145 else 146 { 147 keys[0] = Character.valueOf(r.upHashChar.charAt(0)); 148 } 149 } 150 else 151 { 152 keys = new Character [r.upHashChars.length]; 153 int i = 0; 154 for (char upHashChar : r.upHashChars) 155 { 156 keys[i++] = upHashChar; 157 } 158 } 159 for (Character key : keys) 160 { 161 List<ParserRule> rules = ruleMap.get(key); 162 if (null == rules) 163 { 164 rules = new ArrayList<ParserRule>(); 165 ruleMap.put(key,rules); 166 } 167 int ruleAmount = rules.size(); 168 rules.add(r); 169 if (ruleAmount > 0) 171 { 172 rules.get(ruleAmount).next = r; 173 } 174 } 175 } 177 181 public ParserRule getRules(char ch) 182 { 183 List<ParserRule> rules = getRules(Character.valueOf(ch)); 184 return rules.get(0); 185 } 187 public List<ParserRule> getRules(Character key) 189 { 190 Character upperKey = null == key ? null : Character.valueOf(Character.toUpperCase(key.charValue())); 191 List<ParserRule> rules = ruleMap.get(upperKey); 192 if (null == rules) 193 { 194 rules = new ArrayList<ParserRule>(); 195 } 196 else 197 { 198 rules = new ArrayList<ParserRule>(rules); 199 } 200 if (null != upperKey) 201 { 202 List<ParserRule> nullRules = ruleMap.get(null); 203 if (null != nullRules) 204 { 205 int rulesSize = rules.size(); 206 if ((0 < rulesSize) && (0 < nullRules.size())) 207 { 208 rules.get(rulesSize-1).next = nullRules.get(0); 209 } 210 rules.addAll(nullRules); 211 } 212 } 213 return rules; 214 } 216 public int getRuleCount() 218 { 219 return ruleCount; 220 } 222 228 public int getTerminateChar() 229 { 230 return terminateChar; 231 } 233 public void setTerminateChar(int atChar) 235 { 236 terminateChar = (atChar >= 0) ? atChar : -1; 237 } 239 public boolean getIgnoreCase() 241 { 242 return ignoreCase; 243 } 245 public void setIgnoreCase(boolean b) 247 { 248 ignoreCase = b; 249 } 251 public KeywordMap getKeywords() 253 { 254 return keywords; 255 } 257 public void setKeywords(KeywordMap km) 259 { 260 keywords = km; 261 _noWordSep = null; 262 } 264 public boolean getHighlightDigits() 266 { 267 return highlightDigits; 268 } 270 public void setHighlightDigits(boolean highlightDigits) 272 { 273 this.highlightDigits = highlightDigits; 274 } 276 public Pattern getDigitRegexp() 278 { 279 return digitRE; 280 } 282 public void setDigitRegexp(Pattern digitRE) 284 { 285 this.digitRE = digitRE; 286 } 288 public ParserRule getEscapeRule() 290 { 291 return escapeRule; 292 } 294 public void setEscapeRule(ParserRule escapeRule) 296 { 297 addRule(escapeRule); 298 this.escapeRule = escapeRule; 299 } 301 public byte getDefault() 303 { 304 return defaultToken; 305 } 307 public void setDefault(byte def) 309 { 310 defaultToken = def; 311 } 313 public String getNoWordSep() 315 { 316 if(_noWordSep == null) 317 { 318 _noWordSep = noWordSep; 319 if(noWordSep == null) 320 noWordSep = ""; 321 if(keywords != null) 322 noWordSep += keywords.getNonAlphaNumericChars(); 323 } 324 return noWordSep; 325 } 327 public void setNoWordSep(String noWordSep) 329 { 330 this.noWordSep = noWordSep; 331 _noWordSep = null; 332 } 334 339 public boolean isBuiltIn() 340 { 341 return builtIn; 342 } 344 public String toString() 346 { 347 return getClass().getName() + '[' + modeName + "::" + setName + ']'; 348 } 350 private static ParserRuleSet[] standard; 352 353 static 354 { 355 standard = new ParserRuleSet[Token.ID_COUNT]; 356 for(byte i = 0; i < Token.ID_COUNT; i++) 357 { 358 standard[i] = new ParserRuleSet(null,null); 359 standard[i].setDefault(i); 360 standard[i].builtIn = true; 361 } 362 } 363 364 private String modeName, setName; 365 private Hashtable<String , String > props; 366 367 private KeywordMap keywords; 368 369 private int ruleCount; 370 371 private Map<Character , List<ParserRule>> ruleMap; 372 373 private final List<ParserRuleSet> imports; 374 375 379 private int terminateChar = -1; 380 private boolean ignoreCase = true; 381 private byte defaultToken; 382 private ParserRule escapeRule; 383 384 private boolean highlightDigits; 385 private Pattern digitRE; 386 387 private String _noWordSep; 388 private String noWordSep; 389 390 private boolean builtIn; 391 } 393 | Popular Tags |