PageRenderTime 94ms CodeModel.GetById 66ms app.highlight 21ms RepoModel.GetById 4ms app.codeStats 0ms

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

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