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

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

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