PageRenderTime 97ms CodeModel.GetById 74ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

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