PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/syntax/ParserRule.java

#
Java | 272 lines | 186 code | 30 blank | 56 comment | 6 complexity | 0debf81df8d932e506ad8f95a207414d 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. * ParserRule.java - Parser rule for the token marker
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999 mike dillon
  7. * Portions copyright (C) 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. import gnu.regexp.*;
  25. import org.gjt.sp.jedit.search.RESearchMatcher;
  26. /**
  27. * A parser rule.
  28. * @author mike dillon, Slava Pestov
  29. * @version $Id: ParserRule.java 4315 2002-08-20 18:39:22Z spestov $
  30. */
  31. public class ParserRule
  32. {
  33. //{{{ Major actions (total: 8)
  34. public static final int MAJOR_ACTIONS = 0x000000FF;
  35. public static final int SEQ = 0;
  36. public static final int SPAN = 1 << 1;
  37. public static final int MARK_PREVIOUS = 1 << 2;
  38. public static final int MARK_FOLLOWING = 1 << 3;
  39. public static final int EOL_SPAN = 1 << 4;
  40. // public static final int MAJOR_ACTION_5 = 1 << 5;
  41. // public static final int MAJOR_ACTION_6 = 1 << 6;
  42. // public static final int MAJOR_ACTION_7 = 1 << 7;
  43. //}}}
  44. //{{{ Action hints (total: 8)
  45. public static final int ACTION_HINTS = 0x0000FF00;
  46. public static final int EXCLUDE_MATCH = 1 << 8;
  47. public static final int AT_LINE_START = 1 << 9;
  48. public static final int AT_WHITESPACE_END = 1 << 10;
  49. public static final int AT_WORD_START = 1 << 11;
  50. public static final int NO_LINE_BREAK = 1 << 12;
  51. public static final int NO_WORD_BREAK = 1 << 13;
  52. public static final int IS_ESCAPE = 1 << 14;
  53. public static final int REGEXP = 1 << 15;
  54. //}}}
  55. //{{{ Instance variables
  56. public final char hashChar;
  57. public final char[] start;
  58. // only for SEQ_REGEXP and SPAN_REGEXP rules
  59. public final RE startRegexp;
  60. public final char[] end;
  61. public final int action;
  62. public final byte token;
  63. public ParserRule next;
  64. //}}}
  65. //{{{ getDelegateRuleSet() method
  66. /**
  67. * Returns the parser rule set used to highlight text matched by this
  68. * rule. Only applicable for <code>SEQ</code>, <code>SPAN</code>,
  69. * <code>EOL_SPAN</code>, and <code>MARK_FOLLOWING</code> rules.
  70. *
  71. * @param tokenMarker The token marker
  72. */
  73. public ParserRuleSet getDelegateRuleSet(TokenMarker tokenMarker)
  74. {
  75. // don't worry
  76. if(delegate == null)
  77. {
  78. if((action & MAJOR_ACTIONS) == SEQ)
  79. return null;
  80. else
  81. return ParserRuleSet.getStandardRuleSet(token);
  82. }
  83. else
  84. {
  85. ParserRuleSet delegateSet = tokenMarker.getRuleSet(delegate);
  86. if(delegateSet == null)
  87. {
  88. return ParserRuleSet.getStandardRuleSet(
  89. Token.NULL);
  90. }
  91. else
  92. return delegateSet;
  93. }
  94. } //}}}
  95. //{{{ createSequenceRule() method
  96. public static final ParserRule createSequenceRule(String seq,
  97. String delegate, byte id, boolean atLineStart,
  98. boolean atWhitespaceEnd, boolean atWordStart)
  99. {
  100. int ruleAction = SEQ |
  101. ((atLineStart) ? AT_LINE_START : 0) |
  102. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  103. ((atWordStart) ? AT_WORD_START : 0);
  104. return new ParserRule(ruleAction, seq.charAt(0),
  105. seq.toCharArray(), null, null,
  106. delegate, id);
  107. } //}}}
  108. //{{{ createRegexpSequenceRule() method
  109. public static final ParserRule createRegexpSequenceRule(char hashChar,
  110. String seq, String delegate, byte id, boolean atLineStart,
  111. boolean atWhitespaceEnd, boolean atWordStart, boolean ignoreCase)
  112. throws REException
  113. {
  114. int ruleAction = SEQ | REGEXP |
  115. ((atLineStart) ? AT_LINE_START : 0) |
  116. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  117. ((atWordStart) ? AT_WORD_START : 0);
  118. return new ParserRule(ruleAction, hashChar,
  119. null, new RE("\\A" + seq,(ignoreCase ? RE.REG_ICASE : 0),
  120. RESearchMatcher.RE_SYNTAX_JEDIT),
  121. null, delegate, id);
  122. } //}}}
  123. //{{{ createSpanRule() method
  124. public static final ParserRule createSpanRule(String begin, String end,
  125. String delegate, byte id, boolean noLineBreak,
  126. boolean atLineStart, boolean atWhitespaceEnd, boolean atWordStart,
  127. boolean excludeMatch, boolean noWordBreak)
  128. {
  129. int ruleAction = SPAN |
  130. ((noLineBreak) ? NO_LINE_BREAK : 0) |
  131. ((atLineStart) ? AT_LINE_START : 0) |
  132. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  133. ((atWordStart) ? AT_WORD_START : 0) |
  134. ((excludeMatch) ? EXCLUDE_MATCH : 0) |
  135. ((noWordBreak) ? NO_WORD_BREAK : 0);
  136. return new ParserRule(ruleAction, begin.charAt(0),
  137. begin.toCharArray(), null,
  138. end.toCharArray(), delegate, id);
  139. } //}}}
  140. //{{{ createRegexpSpanRule() method
  141. public static final ParserRule createRegexpSpanRule(char hashChar,
  142. String begin, String end, String delegate, byte id,
  143. boolean noLineBreak, boolean atLineStart,
  144. boolean atWhitespaceEnd, boolean atWordStart,
  145. boolean excludeMatch, boolean noWordBreak, boolean ignoreCase)
  146. throws REException
  147. {
  148. int ruleAction = SPAN | REGEXP |
  149. ((noLineBreak) ? NO_LINE_BREAK : 0) |
  150. ((atLineStart) ? AT_LINE_START : 0) |
  151. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  152. ((atWordStart) ? AT_WORD_START : 0) |
  153. ((excludeMatch) ? EXCLUDE_MATCH : 0) |
  154. ((noWordBreak) ? NO_WORD_BREAK : 0);
  155. return new ParserRule(ruleAction, hashChar,
  156. null, new RE("\\A" + begin,(ignoreCase ? RE.REG_ICASE : 0),
  157. RESearchMatcher.RE_SYNTAX_JEDIT),
  158. end.toCharArray(), delegate, id);
  159. } //}}}
  160. //{{{ createEOLSpanRule() method
  161. public static final ParserRule createEOLSpanRule(String seq,
  162. String delegate, byte id, boolean atLineStart,
  163. boolean atWhitespaceEnd, boolean atWordStart,
  164. boolean excludeMatch)
  165. {
  166. int ruleAction = EOL_SPAN |
  167. ((atLineStart) ? AT_LINE_START : 0) |
  168. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  169. ((atWordStart) ? AT_WORD_START : 0) |
  170. ((excludeMatch) ? EXCLUDE_MATCH : 0)
  171. | NO_LINE_BREAK;
  172. return new ParserRule(ruleAction, seq.charAt(0),
  173. seq.toCharArray(), null, null,
  174. delegate, id);
  175. } //}}}
  176. //{{{ createRegexpEOLSpanRule() method
  177. public static final ParserRule createRegexpEOLSpanRule(
  178. char hashChar, String seq, String delegate, byte id,
  179. boolean atLineStart, boolean atWhitespaceEnd, boolean atWordStart,
  180. boolean excludeMatch, boolean ignoreCase)
  181. throws REException
  182. {
  183. int ruleAction = EOL_SPAN |
  184. ((atLineStart) ? AT_LINE_START : 0) |
  185. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  186. ((atWordStart) ? AT_WORD_START : 0) |
  187. ((excludeMatch) ? EXCLUDE_MATCH : 0)
  188. | NO_LINE_BREAK;
  189. return new ParserRule(ruleAction, hashChar,
  190. null, new RE("\\A" + seq,(ignoreCase ? RE.REG_ICASE : 0)), null,
  191. delegate, id);
  192. } //}}}
  193. //{{{ createMarkFollowingRule() method
  194. public static final ParserRule createMarkFollowingRule(String seq,
  195. byte id, boolean atLineStart, boolean atWhitespaceEnd,
  196. boolean atWordStart, boolean excludeMatch)
  197. {
  198. int ruleAction = MARK_FOLLOWING |
  199. ((atLineStart) ? AT_LINE_START : 0) |
  200. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  201. ((atWordStart) ? AT_WORD_START : 0) |
  202. ((excludeMatch) ? EXCLUDE_MATCH : 0);
  203. return new ParserRule(ruleAction, seq.charAt(0),
  204. seq.toCharArray(), null, null,
  205. null, id);
  206. } //}}}
  207. //{{{ createMarkPreviousRule() method
  208. public static final ParserRule createMarkPreviousRule(String seq,
  209. byte id, boolean atLineStart, boolean atWhitespaceEnd,
  210. boolean atWordStart, boolean excludeMatch)
  211. {
  212. int ruleAction = MARK_PREVIOUS |
  213. ((atLineStart) ? AT_LINE_START : 0) |
  214. ((atWhitespaceEnd) ? AT_WHITESPACE_END : 0) |
  215. ((atWordStart) ? AT_WORD_START : 0) |
  216. ((excludeMatch) ? EXCLUDE_MATCH : 0);
  217. return new ParserRule(ruleAction, seq.charAt(0),
  218. seq.toCharArray(), null, null,
  219. null, id);
  220. } //}}}
  221. //{{{ createEscapeRule() method
  222. public static final ParserRule createEscapeRule(String seq)
  223. {
  224. int ruleAction = IS_ESCAPE;
  225. return new ParserRule(ruleAction, seq.charAt(0),
  226. seq.toCharArray(), null, null,
  227. null, Token.NULL);
  228. } //}}}
  229. //{{{ Private members
  230. private String delegate;
  231. private ParserRule(int action, char hashChar, char[] start,
  232. RE startRegexp, char[] end, String delegate, byte token)
  233. {
  234. this.hashChar = hashChar;
  235. this.start = start;
  236. this.startRegexp = startRegexp;
  237. this.end = end;
  238. this.delegate = delegate;
  239. this.action = action;
  240. this.token = token;
  241. } //}}}
  242. }