PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/syntax/ParserRule.java

#
Java | 235 lines | 154 code | 30 blank | 51 comment | 4 complexity | 4d3e08af22d4902057d3f8657d34b70e 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. /**
  26. * A parser rule.
  27. * @author mike dillon, Slava Pestov
  28. * @version $Id: ParserRule.java 4655 2003-04-28 18:52:37Z spestov $
  29. */
  30. public class ParserRule
  31. {
  32. /**
  33. * Perl5 syntax with character classes enabled.
  34. * @since jEdit 4.2pre1
  35. */
  36. // copy and paste from RESyntaxMatcher to make syntax package
  37. // independent of jEdit itself
  38. public static final RESyntax RE_SYNTAX_JEDIT
  39. = new RESyntax(RESyntax.RE_SYNTAX_PERL5)
  40. .set(RESyntax.RE_CHAR_CLASSES)
  41. .setLineSeparator("\n");
  42. //{{{ Major actions
  43. public static final int MAJOR_ACTIONS = 0x000000FF;
  44. public static final int SEQ = 0;
  45. public static final int SPAN = 1 << 1;
  46. public static final int MARK_PREVIOUS = 1 << 2;
  47. public static final int MARK_FOLLOWING = 1 << 3;
  48. public static final int EOL_SPAN = 1 << 4;
  49. //}}}
  50. //{{{ Action hints
  51. public static final int ACTION_HINTS = 0x0000FF00;
  52. public static final int EXCLUDE_MATCH = 1 << 8;
  53. public static final int NO_LINE_BREAK = 1 << 9;
  54. public static final int NO_WORD_BREAK = 1 << 10;
  55. public static final int IS_ESCAPE = 1 << 11;
  56. public static final int NO_ESCAPE = 1 << 12;
  57. public static final int REGEXP = 1 << 13;
  58. //}}}
  59. //{{{ Position match hints
  60. public static final int AT_LINE_START = 1 << 1;
  61. public static final int AT_WHITESPACE_END = 1 << 2;
  62. public static final int AT_WORD_START = 1 << 3;
  63. //}}}
  64. //{{{ Instance variables
  65. public final char hashChar;
  66. public final int startPosMatch;
  67. public final char[] start;
  68. public final RE startRegexp;
  69. public final int endPosMatch;
  70. public final char[] end;
  71. public final int action;
  72. public final byte token;
  73. public ParserRuleSet delegate;
  74. public ParserRule next;
  75. //}}}
  76. //{{{ createSequenceRule() method
  77. public static final ParserRule createSequenceRule(
  78. int posMatch, String seq, ParserRuleSet delegate, byte id)
  79. {
  80. int ruleAction = SEQ;
  81. return new ParserRule(SEQ, seq.charAt(0),
  82. posMatch, seq.toCharArray(), null,
  83. 0, null, delegate, id);
  84. } //}}}
  85. //{{{ createRegexpSequenceRule() method
  86. public static final ParserRule createRegexpSequenceRule(
  87. char hashChar, int posMatch, String seq,
  88. ParserRuleSet delegate, byte id, boolean ignoreCase)
  89. throws REException
  90. {
  91. return new ParserRule(SEQ | REGEXP, hashChar, posMatch,
  92. null, new RE("\\A" + seq,(ignoreCase ? RE.REG_ICASE : 0),
  93. RE_SYNTAX_JEDIT), 0,
  94. null, delegate, id);
  95. } //}}}
  96. //{{{ createSpanRule() method
  97. public static final ParserRule createSpanRule(
  98. int startPosMatch, String start, int endPosMatch, String end,
  99. ParserRuleSet delegate, byte id, boolean excludeMatch,
  100. boolean noLineBreak, boolean noWordBreak, boolean noEscape)
  101. {
  102. int ruleAction = SPAN |
  103. ((noLineBreak) ? NO_LINE_BREAK : 0) |
  104. ((excludeMatch) ? EXCLUDE_MATCH : 0) |
  105. ((noWordBreak) ? NO_WORD_BREAK : 0) |
  106. ((noEscape) ? NO_ESCAPE : 0);
  107. return new ParserRule(ruleAction, start.charAt(0), startPosMatch,
  108. start.toCharArray(), null,
  109. endPosMatch, end.toCharArray(),
  110. delegate, id);
  111. } //}}}
  112. //{{{ createRegexpSpanRule() method
  113. public static final ParserRule createRegexpSpanRule(
  114. char hashChar, int startPosMatch, String start,
  115. int endPosMatch, String end, ParserRuleSet delegate, byte id,
  116. boolean excludeMatch, boolean noLineBreak, boolean noWordBreak,
  117. boolean ignoreCase, boolean noEscape)
  118. throws REException
  119. {
  120. int ruleAction = SPAN | REGEXP |
  121. ((noLineBreak) ? NO_LINE_BREAK : 0) |
  122. ((excludeMatch) ? EXCLUDE_MATCH : 0) |
  123. ((noWordBreak) ? NO_WORD_BREAK : 0) |
  124. ((noEscape) ? NO_ESCAPE : 0);
  125. return new ParserRule(ruleAction, hashChar, startPosMatch, null,
  126. new RE("\\A" + start,(ignoreCase ? RE.REG_ICASE : 0),
  127. RE_SYNTAX_JEDIT), endPosMatch,
  128. end.toCharArray(), delegate, id);
  129. } //}}}
  130. //{{{ createEOLSpanRule() method
  131. public static final ParserRule createEOLSpanRule(
  132. int posMatch, String seq, ParserRuleSet delegate, byte id,
  133. boolean excludeMatch)
  134. {
  135. int ruleAction = EOL_SPAN |
  136. ((excludeMatch) ? EXCLUDE_MATCH : 0)
  137. | NO_LINE_BREAK;
  138. return new ParserRule(ruleAction, seq.charAt(0), posMatch,
  139. seq.toCharArray(), null, 0, null,
  140. delegate, id);
  141. } //}}}
  142. //{{{ createRegexpEOLSpanRule() method
  143. public static final ParserRule createRegexpEOLSpanRule(
  144. char hashChar, int posMatch, String seq, ParserRuleSet delegate,
  145. byte id, boolean excludeMatch, boolean ignoreCase)
  146. throws REException
  147. {
  148. int ruleAction = EOL_SPAN | REGEXP |
  149. ((excludeMatch) ? EXCLUDE_MATCH : 0)
  150. | NO_LINE_BREAK;
  151. return new ParserRule(ruleAction, hashChar, posMatch,
  152. null, new RE("\\A" + seq,(ignoreCase ? RE.REG_ICASE : 0),
  153. RE_SYNTAX_JEDIT), 0, null,
  154. delegate, id);
  155. } //}}}
  156. //{{{ createMarkFollowingRule() method
  157. public static final ParserRule createMarkFollowingRule(
  158. int posMatch, String seq, byte id, boolean excludeMatch)
  159. {
  160. int ruleAction = MARK_FOLLOWING |
  161. ((excludeMatch) ? EXCLUDE_MATCH : 0);
  162. return new ParserRule(ruleAction, seq.charAt(0), posMatch,
  163. seq.toCharArray(), null, 0, null, null, id);
  164. } //}}}
  165. //{{{ createMarkPreviousRule() method
  166. public static final ParserRule createMarkPreviousRule(
  167. int posMatch, String seq, byte id, boolean excludeMatch)
  168. {
  169. int ruleAction = MARK_PREVIOUS |
  170. ((excludeMatch) ? EXCLUDE_MATCH : 0);
  171. return new ParserRule(ruleAction, seq.charAt(0), posMatch,
  172. seq.toCharArray(), null, 0, null, null, id);
  173. } //}}}
  174. //{{{ createEscapeRule() method
  175. public static final ParserRule createEscapeRule(String seq)
  176. {
  177. int ruleAction = IS_ESCAPE;
  178. return new ParserRule(ruleAction, seq.charAt(0),
  179. 0, seq.toCharArray(), null, 0, null,
  180. null, Token.NULL);
  181. } //}}}
  182. //{{{ Private members
  183. private ParserRule(int action, char hashChar,
  184. int startPosMatch, char[] start, RE startRegexp,
  185. int endPosMatch, char[] end,
  186. ParserRuleSet delegate, byte token)
  187. {
  188. this.action = action;
  189. this.hashChar = hashChar;
  190. this.startPosMatch = startPosMatch;
  191. this.start = start;
  192. this.startRegexp = startRegexp;
  193. this.endPosMatch = endPosMatch;
  194. this.end = end;
  195. this.delegate = delegate;
  196. this.token = token;
  197. if(this.delegate == null)
  198. {
  199. if((action & MAJOR_ACTIONS) != SEQ)
  200. {
  201. this.delegate = ParserRuleSet.getStandardRuleSet(token);
  202. }
  203. }
  204. } //}}}
  205. }