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