/jEdit/tags/before-selection-manager/org/gjt/sp/jedit/syntax/ParserRuleSet.java

# · Java · 326 lines · 201 code · 46 blank · 79 comment · 15 complexity · 29adfd51ff0f77116cd2e2a3644a5382 MD5 · raw file

  1. /*
  2. * ParserRuleSet.java - A set of parser rules
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999 mike dillon
  7. * Portions copyright (C) 2001, 2002 Slava Pestov
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.syntax;
  24. //{{{ Imports
  25. import gnu.regexp.RE;
  26. import java.util.*;
  27. //}}}
  28. /**
  29. * A set of parser rules.
  30. * @author mike dillon
  31. * @version $Id: ParserRuleSet.java 4759 2003-06-05 00:01:49Z spestov $
  32. */
  33. public class ParserRuleSet
  34. {
  35. //{{{ getStandardRuleSet() method
  36. /**
  37. * Returns a parser rule set that highlights everything with the
  38. * specified token type.
  39. * @param id The token type
  40. */
  41. public static ParserRuleSet getStandardRuleSet(byte id)
  42. {
  43. return standard[id];
  44. } //}}}
  45. //{{{ ParserRuleSet constructor
  46. public ParserRuleSet(String modeName, String setName)
  47. {
  48. this.modeName = modeName;
  49. this.setName = setName;
  50. ruleMapFirst = new ParserRule[RULE_BUCKET_COUNT];
  51. ruleMapLast = new ParserRule[RULE_BUCKET_COUNT];
  52. imports = new LinkedList();
  53. } //}}}
  54. //{{{ getModeName() method
  55. public String getModeName()
  56. {
  57. return modeName;
  58. } //}}}
  59. //{{{ getSetName() method
  60. public String getSetName()
  61. {
  62. return setName;
  63. } //}}}
  64. //{{{ getName() method
  65. public String getName()
  66. {
  67. return modeName + "::" + setName;
  68. } //}}}
  69. //{{{ getProperties() method
  70. public Hashtable getProperties()
  71. {
  72. return props;
  73. } //}}}
  74. //{{{ setProperties() method
  75. public void setProperties(Hashtable props)
  76. {
  77. this.props = props;
  78. _noWordSep = null;
  79. } //}}}
  80. //{{{ resolveImports() method
  81. /**
  82. * Resolves all rulesets added with {@link #addRuleSet(ParserRuleSet)}.
  83. * @since jEdit 4.2pre3
  84. */
  85. public void resolveImports()
  86. {
  87. Iterator iter = imports.iterator();
  88. while(iter.hasNext())
  89. {
  90. ParserRuleSet ruleset = (ParserRuleSet)iter.next();
  91. for(int i = 0; i < ruleset.ruleMapFirst.length; i++)
  92. {
  93. ParserRule rule = ruleset.ruleMapFirst[i];
  94. while(rule != null)
  95. {
  96. addRule(rule);
  97. rule = rule.next;
  98. }
  99. }
  100. if(ruleset.keywords != null)
  101. {
  102. if(keywords == null)
  103. keywords = new KeywordMap(ignoreCase);
  104. keywords.add(ruleset.keywords);
  105. }
  106. }
  107. imports.clear();
  108. } //}}}
  109. //{{{ addRuleSet() method
  110. /**
  111. * Adds all rules contained in the given ruleset.
  112. * @param ruleset The ruleset
  113. * @since jEdit 4.2pre3
  114. */
  115. public void addRuleSet(ParserRuleSet ruleset)
  116. {
  117. imports.add(ruleset);
  118. } //}}}
  119. //{{{ addRule() method
  120. public void addRule(ParserRule r)
  121. {
  122. ruleCount++;
  123. int key = Character.toUpperCase(r.hashChar)
  124. % RULE_BUCKET_COUNT;
  125. ParserRule last = ruleMapLast[key];
  126. if(last == null)
  127. ruleMapFirst[key] = ruleMapLast[key] = r;
  128. else
  129. {
  130. last.next = r;
  131. ruleMapLast[key] = r;
  132. }
  133. } //}}}
  134. //{{{ getRules() method
  135. public ParserRule getRules(char ch)
  136. {
  137. int key = Character.toUpperCase(ch) % RULE_BUCKET_COUNT;
  138. return ruleMapFirst[key];
  139. } //}}}
  140. //{{{ getRuleCount() method
  141. public int getRuleCount()
  142. {
  143. return ruleCount;
  144. } //}}}
  145. //{{{ getTerminateChar() method
  146. public int getTerminateChar()
  147. {
  148. return terminateChar;
  149. } //}}}
  150. //{{{ setTerminateChar() method
  151. public void setTerminateChar(int atChar)
  152. {
  153. terminateChar = (atChar >= 0) ? atChar : -1;
  154. } //}}}
  155. //{{{ getIgnoreCase() method
  156. public boolean getIgnoreCase()
  157. {
  158. return ignoreCase;
  159. } //}}}
  160. //{{{ setIgnoreCase() method
  161. public void setIgnoreCase(boolean b)
  162. {
  163. ignoreCase = b;
  164. } //}}}
  165. //{{{ getKeywords() method
  166. public KeywordMap getKeywords()
  167. {
  168. return keywords;
  169. } //}}}
  170. //{{{ setKeywords() method
  171. public void setKeywords(KeywordMap km)
  172. {
  173. keywords = km;
  174. _noWordSep = null;
  175. } //}}}
  176. //{{{ getHighlightDigits() method
  177. public boolean getHighlightDigits()
  178. {
  179. return highlightDigits;
  180. } //}}}
  181. //{{{ setHighlightDigits() method
  182. public void setHighlightDigits(boolean highlightDigits)
  183. {
  184. this.highlightDigits = highlightDigits;
  185. } //}}}
  186. //{{{ getDigitRegexp() method
  187. public RE getDigitRegexp()
  188. {
  189. return digitRE;
  190. } //}}}
  191. //{{{ setDigitRegexp() method
  192. public void setDigitRegexp(RE digitRE)
  193. {
  194. this.digitRE = digitRE;
  195. } //}}}
  196. //{{{ getEscapeRule() method
  197. public ParserRule getEscapeRule()
  198. {
  199. return escapeRule;
  200. } //}}}
  201. //{{{ setEscapeRule() method
  202. public void setEscapeRule(ParserRule escapeRule)
  203. {
  204. addRule(escapeRule);
  205. this.escapeRule = escapeRule;
  206. } //}}}
  207. //{{{ getDefault() method
  208. public byte getDefault()
  209. {
  210. return defaultToken;
  211. } //}}}
  212. //{{{ setDefault() method
  213. public void setDefault(byte def)
  214. {
  215. defaultToken = def;
  216. } //}}}
  217. //{{{ getNoWordSep() method
  218. public String getNoWordSep()
  219. {
  220. if(_noWordSep == null)
  221. {
  222. _noWordSep = noWordSep;
  223. if(noWordSep == null)
  224. noWordSep = "";
  225. if(keywords != null)
  226. noWordSep += keywords.getNonAlphaNumericChars();
  227. }
  228. return noWordSep;
  229. } //}}}
  230. //{{{ setNoWordSep() method
  231. public void setNoWordSep(String noWordSep)
  232. {
  233. this.noWordSep = noWordSep;
  234. _noWordSep = null;
  235. } //}}}
  236. //{{{ isBuiltIn() method
  237. /**
  238. * Returns if this is a built-in ruleset.
  239. * @since jEdit 4.2pre1
  240. */
  241. public boolean isBuiltIn()
  242. {
  243. return builtIn;
  244. } //}}}
  245. //{{{ toString() method
  246. public String toString()
  247. {
  248. return getClass().getName() + "[" + modeName + "::" + setName + "]";
  249. } //}}}
  250. //{{{ Private members
  251. private static ParserRuleSet[] standard;
  252. static
  253. {
  254. standard = new ParserRuleSet[Token.ID_COUNT];
  255. for(byte i = 0; i < standard.length; i++)
  256. {
  257. standard[i] = new ParserRuleSet(null,null);
  258. standard[i].setDefault(i);
  259. standard[i].builtIn = true;
  260. }
  261. }
  262. private static final int RULE_BUCKET_COUNT = 128;
  263. private String modeName, setName;
  264. private Hashtable props;
  265. private KeywordMap keywords;
  266. private int ruleCount;
  267. private ParserRule[] ruleMapFirst;
  268. private ParserRule[] ruleMapLast;
  269. private LinkedList imports;
  270. private int terminateChar = -1;
  271. private boolean ignoreCase = true;
  272. private byte defaultToken;
  273. private ParserRule escapeRule;
  274. private boolean highlightDigits;
  275. private RE digitRE;
  276. private String _noWordSep;
  277. private String noWordSep;
  278. private boolean builtIn;
  279. //}}}
  280. }