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

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/syntax/ParserRuleFactory.java

#
Java | 261 lines | 190 code | 46 blank | 25 comment | 25 complexity | 8ab69ab44ab2d4597c7ec334f1b13ab1 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. * ParserRuleFactory.java - Factory object for creating ParserRules
  3. * Copyright (C) 1999 mike dillon
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.syntax;
  20. /**
  21. * Creates parser rules.
  22. * @author mike dillon
  23. * @version $Id: ParserRuleFactory.java 3801 2001-09-08 04:50:46Z spestov $
  24. */
  25. public class ParserRuleFactory
  26. {
  27. // public members
  28. public static final ParserRule createSpanRule(String begin, String end,
  29. byte id, boolean noLineBreak, boolean atLineStart,
  30. boolean excludeMatch, boolean noWordBreak)
  31. {
  32. int ruleAction = TokenMarker.SPAN |
  33. ((noLineBreak) ? TokenMarker.NO_LINE_BREAK : 0) |
  34. ((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
  35. ((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0) |
  36. ((noWordBreak) ? TokenMarker.NO_WORD_BREAK : 0);
  37. String[] strings = new String[2];
  38. strings[0] = begin;
  39. strings[1] = end;
  40. int[] ruleSeqLengths = getStringLengthArray(strings);
  41. char[] ruleChars = getCharArray(strings, ruleSeqLengths);
  42. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  43. }
  44. public static final ParserRule createDelegateSpanRule(String begin, String end,
  45. String delegateSet, byte id, boolean noLineBreak, boolean atLineStart,
  46. boolean excludeMatch, boolean noWordBreak)
  47. {
  48. int ruleAction = TokenMarker.SPAN |
  49. TokenMarker.DELEGATE |
  50. ((noLineBreak) ? TokenMarker.NO_LINE_BREAK : 0) |
  51. ((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
  52. ((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0) |
  53. ((noWordBreak) ? TokenMarker.NO_WORD_BREAK : 0);
  54. String[] strings = new String[3];
  55. strings[0] = begin;
  56. strings[1] = end;
  57. strings[2] = delegateSet;
  58. int[] ruleSeqLengths = getStringLengthArray(strings);
  59. char[] ruleChars = getCharArray(strings, ruleSeqLengths);
  60. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  61. }
  62. public static final ParserRule createEOLSpanRule(String seq, byte id,
  63. boolean atLineStart, boolean excludeMatch)
  64. {
  65. int ruleAction = TokenMarker.EOL_SPAN |
  66. ((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
  67. ((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
  68. String[] strings = new String[1];
  69. strings[0] = seq;
  70. int[] ruleSeqLengths = new int[1];
  71. char[] ruleChars;
  72. if (seq != null)
  73. {
  74. ruleSeqLengths[0] = seq.length();
  75. ruleChars = seq.toCharArray();
  76. }
  77. else
  78. {
  79. ruleChars = new char[0];
  80. }
  81. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  82. }
  83. public static final ParserRule createMarkPreviousRule(String seq, byte id,
  84. boolean atLineStart, boolean excludeMatch)
  85. {
  86. int ruleAction = TokenMarker.MARK_PREVIOUS |
  87. ((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
  88. ((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
  89. String[] strings = new String[1];
  90. strings[0] = seq;
  91. int[] ruleSeqLengths = new int[1];
  92. char[] ruleChars;
  93. if (seq != null)
  94. {
  95. ruleSeqLengths[0] = seq.length();
  96. ruleChars = seq.toCharArray();
  97. }
  98. else
  99. {
  100. ruleChars = new char[0];
  101. }
  102. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  103. }
  104. public static final ParserRule createMarkFollowingRule(String seq, byte id,
  105. boolean atLineStart, boolean excludeMatch)
  106. {
  107. int ruleAction = TokenMarker.MARK_FOLLOWING |
  108. ((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
  109. ((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
  110. String[] strings = new String[1];
  111. strings[0] = seq;
  112. int[] ruleSeqLengths = new int[1];
  113. char[] ruleChars;
  114. if (seq != null)
  115. {
  116. ruleSeqLengths[0] = seq.length();
  117. ruleChars = seq.toCharArray();
  118. }
  119. else
  120. {
  121. ruleChars = new char[0];
  122. }
  123. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  124. }
  125. public static final ParserRule createSequenceRule(String seq, byte id, boolean atLineStart)
  126. {
  127. int ruleAction = ((atLineStart) ? TokenMarker.AT_LINE_START : 0);
  128. String[] strings = new String[1];
  129. strings[0] = seq;
  130. int[] ruleSeqLengths = new int[1];
  131. char[] ruleChars;
  132. if (seq != null)
  133. {
  134. ruleSeqLengths[0] = seq.length();
  135. ruleChars = seq.toCharArray();
  136. }
  137. else
  138. {
  139. ruleChars = new char[0];
  140. }
  141. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
  142. }
  143. public static final ParserRule createEscapeRule(String seq)
  144. {
  145. int ruleAction = TokenMarker.IS_ESCAPE;
  146. String[] strings = new String[1];
  147. strings[0] = seq;
  148. int[] ruleSeqLengths = new int[1];
  149. char[] ruleChars;
  150. if (seq != null)
  151. {
  152. ruleSeqLengths[0] = seq.length();
  153. ruleChars = seq.toCharArray();
  154. }
  155. else
  156. {
  157. ruleChars = new char[0];
  158. }
  159. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, Token.NULL);
  160. }
  161. public static final ParserRule createWhitespaceRule(String seq)
  162. {
  163. int ruleAction = TokenMarker.WHITESPACE;
  164. String[] strings = new String[1];
  165. strings[0] = seq;
  166. int[] ruleSeqLengths = new int[1];
  167. char[] ruleChars;
  168. if (seq != null)
  169. {
  170. ruleSeqLengths[0] = seq.length();
  171. ruleChars = seq.toCharArray();
  172. }
  173. else
  174. {
  175. ruleChars = new char[0];
  176. }
  177. return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, Token.NULL);
  178. }
  179. // private members
  180. private static char[] getCharArray(String[] strings, int[] lengthArray)
  181. {
  182. if (lengthArray == null || lengthArray.length == 0) return new char[0];
  183. char[] chars;
  184. int charArrayLength = 0;
  185. for (int i = 0; i < lengthArray.length; i++)
  186. {
  187. charArrayLength += lengthArray[i];
  188. }
  189. chars = new char[charArrayLength];
  190. int copyOffset = 0;
  191. for (int i = 0; i < strings.length; i++)
  192. {
  193. if (strings[i] != null)
  194. {
  195. System.arraycopy(strings[i].toCharArray(),0,chars,copyOffset,lengthArray[i]);
  196. copyOffset += lengthArray[i];
  197. }
  198. }
  199. return chars;
  200. }
  201. private static int[] getStringLengthArray(String[] strings)
  202. {
  203. int[] stringLengthArray;
  204. if (strings == null) return new int[0];
  205. stringLengthArray = new int[strings.length];
  206. for (int i = 0; i < strings.length; i++)
  207. {
  208. if (strings[i] != null)
  209. {
  210. stringLengthArray[i] = strings[i].length();
  211. }
  212. }
  213. return stringLengthArray;
  214. }
  215. }