PageRenderTime 175ms CodeModel.GetById 148ms app.highlight 23ms RepoModel.GetById 1ms 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
  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
 20package org.gjt.sp.jedit.syntax;
 21
 22/**
 23 * Creates parser rules.
 24 * @author mike dillon
 25 * @version $Id: ParserRuleFactory.java 3801 2001-09-08 04:50:46Z spestov $
 26 */
 27public class ParserRuleFactory
 28{
 29	// public members
 30	public static final ParserRule createSpanRule(String begin, String end,
 31		byte id, boolean noLineBreak, boolean atLineStart,
 32		boolean excludeMatch, boolean noWordBreak)
 33	{
 34		int ruleAction = TokenMarker.SPAN |
 35			((noLineBreak) ? TokenMarker.NO_LINE_BREAK : 0) |
 36			((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
 37			((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0) |
 38			((noWordBreak) ? TokenMarker.NO_WORD_BREAK : 0);
 39
 40		String[] strings = new String[2];
 41		strings[0] = begin;
 42		strings[1] = end;
 43
 44		int[] ruleSeqLengths = getStringLengthArray(strings);
 45		char[] ruleChars = getCharArray(strings, ruleSeqLengths);
 46
 47		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
 48	}
 49
 50	public static final ParserRule createDelegateSpanRule(String begin, String end,
 51		String delegateSet, byte id, boolean noLineBreak, boolean atLineStart,
 52		boolean excludeMatch, boolean noWordBreak)
 53	{
 54		int ruleAction = TokenMarker.SPAN |
 55			TokenMarker.DELEGATE |
 56			((noLineBreak) ? TokenMarker.NO_LINE_BREAK : 0) |
 57			((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
 58			((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0) |
 59			((noWordBreak) ? TokenMarker.NO_WORD_BREAK : 0);
 60
 61		String[] strings = new String[3];
 62		strings[0] = begin;
 63		strings[1] = end;
 64		strings[2] = delegateSet;
 65
 66		int[] ruleSeqLengths = getStringLengthArray(strings);
 67		char[] ruleChars = getCharArray(strings, ruleSeqLengths);
 68
 69		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
 70	}
 71
 72	public static final ParserRule createEOLSpanRule(String seq, byte id,
 73		boolean atLineStart, boolean excludeMatch)
 74	{
 75		int ruleAction = TokenMarker.EOL_SPAN |
 76			((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
 77			((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
 78
 79		String[] strings = new String[1];
 80		strings[0] = seq;
 81
 82		int[] ruleSeqLengths = new int[1];
 83		char[] ruleChars;
 84		if (seq != null)
 85		{
 86			ruleSeqLengths[0] = seq.length();
 87			ruleChars = seq.toCharArray();
 88		}
 89		else
 90		{
 91			ruleChars = new char[0];
 92		}
 93
 94		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
 95	}
 96
 97	public static final ParserRule createMarkPreviousRule(String seq, byte id,
 98		boolean atLineStart, boolean excludeMatch)
 99	{
100		int ruleAction = TokenMarker.MARK_PREVIOUS |
101			((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
102			((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
103
104		String[] strings = new String[1];
105		strings[0] = seq;
106
107		int[] ruleSeqLengths = new int[1];
108		char[] ruleChars;
109		if (seq != null)
110		{
111			ruleSeqLengths[0] = seq.length();
112			ruleChars = seq.toCharArray();
113		}
114		else
115		{
116			ruleChars = new char[0];
117		}
118
119		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
120	}
121
122	public static final ParserRule createMarkFollowingRule(String seq, byte id,
123		boolean atLineStart, boolean excludeMatch)
124	{
125		int ruleAction = TokenMarker.MARK_FOLLOWING |
126			((atLineStart) ? TokenMarker.AT_LINE_START : 0) |
127			((excludeMatch) ? TokenMarker.EXCLUDE_MATCH : 0);
128
129		String[] strings = new String[1];
130		strings[0] = seq;
131
132		int[] ruleSeqLengths = new int[1];
133		char[] ruleChars;
134		if (seq != null)
135		{
136			ruleSeqLengths[0] = seq.length();
137			ruleChars = seq.toCharArray();
138		}
139		else
140		{
141			ruleChars = new char[0];
142		}
143
144		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
145	}
146
147	public static final ParserRule createSequenceRule(String seq, byte id, boolean atLineStart)
148	{
149		int ruleAction = ((atLineStart) ? TokenMarker.AT_LINE_START : 0);
150
151		String[] strings = new String[1];
152		strings[0] = seq;
153
154		int[] ruleSeqLengths = new int[1];
155		char[] ruleChars;
156		if (seq != null)
157		{
158			ruleSeqLengths[0] = seq.length();
159			ruleChars = seq.toCharArray();
160		}
161		else
162		{
163			ruleChars = new char[0];
164		}
165
166		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, id);
167	}
168
169	public static final ParserRule createEscapeRule(String seq)
170	{
171		int ruleAction = TokenMarker.IS_ESCAPE;
172
173		String[] strings = new String[1];
174		strings[0] = seq;
175
176		int[] ruleSeqLengths = new int[1];
177		char[] ruleChars;
178		if (seq != null)
179		{
180			ruleSeqLengths[0] = seq.length();
181			ruleChars = seq.toCharArray();
182		}
183		else
184		{
185			ruleChars = new char[0];
186		}
187
188		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, Token.NULL);
189	}
190
191	public static final ParserRule createWhitespaceRule(String seq)
192	{
193		int ruleAction = TokenMarker.WHITESPACE;
194
195		String[] strings = new String[1];
196		strings[0] = seq;
197
198		int[] ruleSeqLengths = new int[1];
199		char[] ruleChars;
200		if (seq != null)
201		{
202			ruleSeqLengths[0] = seq.length();
203			ruleChars = seq.toCharArray();
204		}
205		else
206		{
207			ruleChars = new char[0];
208		}
209
210		return new ParserRule(ruleChars, ruleSeqLengths, ruleAction, Token.NULL);
211	}
212
213	// private members
214	private static char[] getCharArray(String[] strings, int[] lengthArray)
215	{
216		if (lengthArray == null || lengthArray.length == 0) return new char[0];
217
218		char[] chars;
219	
220		int charArrayLength = 0;
221
222		for (int i = 0; i < lengthArray.length; i++)
223		{
224			charArrayLength += lengthArray[i];
225		}
226
227		chars = new char[charArrayLength];
228
229		int copyOffset = 0;
230
231		for (int i = 0; i < strings.length; i++)
232		{
233			if (strings[i] != null)
234			{
235				System.arraycopy(strings[i].toCharArray(),0,chars,copyOffset,lengthArray[i]);
236				copyOffset += lengthArray[i];
237			}
238		}
239
240		return chars;
241	}
242
243	private static int[] getStringLengthArray(String[] strings)
244	{
245		int[] stringLengthArray;
246
247		if (strings == null) return new int[0];
248
249		stringLengthArray = new int[strings.length];
250
251		for (int i = 0; i < strings.length; i++)
252		{
253			if (strings[i] != null)
254			{
255				stringLengthArray[i] = strings[i].length();
256			}
257		}
258
259		return stringLengthArray;
260	}
261}