PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/syntax/ParserRuleSet.java

#
Java | 335 lines | 201 code | 46 blank | 88 comment | 15 complexity | eaba5570d0974d5df6fb1254671341a3 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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 java.util.*;
  26. import java.util.regex.Pattern;
  27. //}}}
  28. /**
  29. * A set of parser rules.
  30. * @author mike dillon
  31. * @version $Id: ParserRuleSet.java 5471 2006-06-22 06:31:23Z kpouer $
  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. /**
  147. * Returns the number of chars that can be read before the rule parsing stops.
  148. *
  149. * @return a number of chars or -1 (default value) if there is no limit
  150. */
  151. public int getTerminateChar()
  152. {
  153. return terminateChar;
  154. } //}}}
  155. //{{{ setTerminateChar() method
  156. public void setTerminateChar(int atChar)
  157. {
  158. terminateChar = (atChar >= 0) ? atChar : -1;
  159. } //}}}
  160. //{{{ getIgnoreCase() method
  161. public boolean getIgnoreCase()
  162. {
  163. return ignoreCase;
  164. } //}}}
  165. //{{{ setIgnoreCase() method
  166. public void setIgnoreCase(boolean b)
  167. {
  168. ignoreCase = b;
  169. } //}}}
  170. //{{{ getKeywords() method
  171. public KeywordMap getKeywords()
  172. {
  173. return keywords;
  174. } //}}}
  175. //{{{ setKeywords() method
  176. public void setKeywords(KeywordMap km)
  177. {
  178. keywords = km;
  179. _noWordSep = null;
  180. } //}}}
  181. //{{{ getHighlightDigits() method
  182. public boolean getHighlightDigits()
  183. {
  184. return highlightDigits;
  185. } //}}}
  186. //{{{ setHighlightDigits() method
  187. public void setHighlightDigits(boolean highlightDigits)
  188. {
  189. this.highlightDigits = highlightDigits;
  190. } //}}}
  191. //{{{ getDigitRegexp() method
  192. public Pattern getDigitRegexp()
  193. {
  194. return digitRE;
  195. } //}}}
  196. //{{{ setDigitRegexp() method
  197. public void setDigitRegexp(Pattern digitRE)
  198. {
  199. this.digitRE = digitRE;
  200. } //}}}
  201. //{{{ getEscapeRule() method
  202. public ParserRule getEscapeRule()
  203. {
  204. return escapeRule;
  205. } //}}}
  206. //{{{ setEscapeRule() method
  207. public void setEscapeRule(ParserRule escapeRule)
  208. {
  209. addRule(escapeRule);
  210. this.escapeRule = escapeRule;
  211. } //}}}
  212. //{{{ getDefault() method
  213. public byte getDefault()
  214. {
  215. return defaultToken;
  216. } //}}}
  217. //{{{ setDefault() method
  218. public void setDefault(byte def)
  219. {
  220. defaultToken = def;
  221. } //}}}
  222. //{{{ getNoWordSep() method
  223. public String getNoWordSep()
  224. {
  225. if(_noWordSep == null)
  226. {
  227. _noWordSep = noWordSep;
  228. if(noWordSep == null)
  229. noWordSep = "";
  230. if(keywords != null)
  231. noWordSep += keywords.getNonAlphaNumericChars();
  232. }
  233. return noWordSep;
  234. } //}}}
  235. //{{{ setNoWordSep() method
  236. public void setNoWordSep(String noWordSep)
  237. {
  238. this.noWordSep = noWordSep;
  239. _noWordSep = null;
  240. } //}}}
  241. //{{{ isBuiltIn() method
  242. /**
  243. * Returns if this is a built-in ruleset.
  244. * @since jEdit 4.2pre1
  245. */
  246. public boolean isBuiltIn()
  247. {
  248. return builtIn;
  249. } //}}}
  250. //{{{ toString() method
  251. public String toString()
  252. {
  253. return getClass().getName() + '[' + modeName + "::" + setName + ']';
  254. } //}}}
  255. //{{{ Private members
  256. private static ParserRuleSet[] standard;
  257. static
  258. {
  259. standard = new ParserRuleSet[Token.ID_COUNT];
  260. for(byte i = 0; i < Token.ID_COUNT; i++)
  261. {
  262. standard[i] = new ParserRuleSet(null,null);
  263. standard[i].setDefault(i);
  264. standard[i].builtIn = true;
  265. }
  266. }
  267. private static final int RULE_BUCKET_COUNT = 128;
  268. private String modeName, setName;
  269. private Hashtable props;
  270. private KeywordMap keywords;
  271. private int ruleCount;
  272. private ParserRule[] ruleMapFirst;
  273. private ParserRule[] ruleMapLast;
  274. private LinkedList imports;
  275. /**
  276. * The number of chars that can be read before the parsing stops.
  277. * &lt;TERMINATE AT_CHAR="1" /&gt;
  278. */
  279. private int terminateChar = -1;
  280. private boolean ignoreCase = true;
  281. private byte defaultToken;
  282. private ParserRule escapeRule;
  283. private boolean highlightDigits;
  284. private Pattern digitRE;
  285. private String _noWordSep;
  286. private String noWordSep;
  287. private boolean builtIn;
  288. //}}}
  289. }