KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > syntax > ParserRule


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
24 package org.gjt.sp.jedit.syntax;
25
26 import java.util.Arrays JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30 import java.util.regex.PatternSyntaxException JavaDoc;
31
32 /**
33  * A parser rule.
34  * @author mike dillon, Slava Pestov
35  * @version $Id: ParserRule.java 8103 2006-11-21 00:03:41Z vampire0 $
36  */

37 public class ParserRule
38 {
39
40     //{{{ Major actions
41
public static final int MAJOR_ACTIONS = 0x000000FF;
42     public static final int SEQ = 0;
43     public static final int SPAN = 1 << 1;
44     public static final int MARK_PREVIOUS = 1 << 2;
45     public static final int MARK_FOLLOWING = 1 << 3;
46     public static final int EOL_SPAN = 1 << 4;
47     //}}}
48

49     //{{{ Action hints
50
public static final int ACTION_HINTS = 0x0000FF00;
51     public static final int EXCLUDE_MATCH = 1 << 8;
52     public static final int NO_LINE_BREAK = 1 << 9;
53     public static final int NO_WORD_BREAK = 1 << 10;
54     public static final int IS_ESCAPE = 1 << 11;
55     public static final int NO_ESCAPE = 1 << 12;
56     public static final int REGEXP = 1 << 13;
57     //}}}
58

59     //{{{ Position match hints
60
public static final int AT_LINE_START = 1 << 1;
61     public static final int AT_WHITESPACE_END = 1 << 2;
62     public static final int AT_WORD_START = 1 << 3;
63     //}}}
64

65     //{{{ Instance variables
66
public final String JavaDoc hashChar;
67     /** This is hashChar.toUpperCase() because it's often used like that. */
68     public final String JavaDoc upHashChar;
69     public final char[] hashChars;
70     /** This is for (int i=0 ; i&lt;hashChars.length ; i++) { upHashChars[i] = Character.toUpperCase(hashChars[i]) } because it's often used like that. */
71     public final char[] upHashChars;
72     public final int startPosMatch;
73     public final char[] start;
74     public final Pattern JavaDoc startRegexp;
75
76     public final int endPosMatch;
77     public final char[] end;
78
79     public final int action;
80     public final byte token;
81
82     public ParserRuleSet delegate;
83
84     /**
85     * @deprecated As the linking between rules is not anymore done within the rule but external. See {@link ParserRuleSet#getRules(Character)}
86     */

87     public ParserRule next;
88     //}}}
89

90     //{{{ createSequenceRule() method
91
public static final ParserRule createSequenceRule(
92         int posMatch, String JavaDoc seq, ParserRuleSet delegate, byte id)
93     {
94         return new ParserRule(SEQ, seq.substring(0,1),
95             posMatch, seq.toCharArray(), null,
96             0, null, delegate, id);
97     } //}}}
98

99     //{{{ createRegexpSequenceRule() method
100
/**
101      * @deprecated Use {@link #createRegexpSequenceRule(String,int,String,ParserRuleSet,byte,boolean)} instead
102      */

103     public static final ParserRule createRegexpSequenceRule(
104         char hashChar, int posMatch, String JavaDoc seq,
105         ParserRuleSet delegate, byte id, boolean ignoreCase)
106         throws PatternSyntaxException JavaDoc
107     {
108         return createRegexpSequenceRule(String.valueOf(hashChar), posMatch,
109             seq, delegate, id, ignoreCase);
110     } //}}}
111

112     //{{{ createRegexpSequenceRule() method
113
public static final ParserRule createRegexpSequenceRule(
114         String JavaDoc hashChar, int posMatch, String JavaDoc seq,
115         ParserRuleSet delegate, byte id, boolean ignoreCase)
116         throws PatternSyntaxException JavaDoc
117     {
118         return new ParserRule(SEQ | REGEXP, hashChar, posMatch,
119             null, Pattern.compile(seq,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
120             0, null, delegate, id);
121     } //}}}
122

123     //{{{ createRegexpSequenceRule() method
124
public static final ParserRule createRegexpSequenceRule(
125         int posMatch, char[] hashChars, String JavaDoc seq,
126         ParserRuleSet delegate, byte id, boolean ignoreCase)
127         throws PatternSyntaxException JavaDoc
128     {
129         return new ParserRule(hashChars, SEQ | REGEXP, posMatch,
130             null, Pattern.compile(seq,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
131             0, null, delegate, id);
132     } //}}}
133

134     //{{{ createSpanRule() method
135
public static final ParserRule createSpanRule(
136         int startPosMatch, String JavaDoc start, int endPosMatch, String JavaDoc end,
137         ParserRuleSet delegate, byte id, boolean excludeMatch,
138         boolean noLineBreak, boolean noWordBreak, boolean noEscape)
139     {
140         int ruleAction = SPAN |
141             ((noLineBreak) ? NO_LINE_BREAK : 0) |
142             ((excludeMatch) ? EXCLUDE_MATCH : 0) |
143             ((noWordBreak) ? NO_WORD_BREAK : 0) |
144             ((noEscape) ? NO_ESCAPE : 0);
145
146         return new ParserRule(ruleAction, start.substring(0,1), startPosMatch,
147             start.toCharArray(), null,
148             endPosMatch, end.toCharArray(),
149             delegate, id);
150     } //}}}
151

152     //{{{ createRegexpSpanRule() method
153
/**
154      * @deprecated Use {@link #createRegexpSpanRule(String,int,String,int,String,ParserRuleSet,byte,boolean,boolean,boolean,boolean,boolean)} instead
155      */

156     public static final ParserRule createRegexpSpanRule(
157         char hashChar, int startPosMatch, String JavaDoc start,
158         int endPosMatch, String JavaDoc end, ParserRuleSet delegate, byte id,
159         boolean excludeMatch, boolean noLineBreak, boolean noWordBreak,
160         boolean ignoreCase, boolean noEscape)
161         throws PatternSyntaxException JavaDoc
162     {
163         return createRegexpSpanRule(String.valueOf(hashChar),startPosMatch,
164             start,endPosMatch,end,delegate,id,excludeMatch,
165             noLineBreak,noWordBreak,ignoreCase,noEscape);
166     } //}}}
167

168     //{{{ createRegexpSpanRule() method
169
public static final ParserRule createRegexpSpanRule(
170         String JavaDoc hashChar, int startPosMatch, String JavaDoc start,
171         int endPosMatch, String JavaDoc end, ParserRuleSet delegate, byte id,
172         boolean excludeMatch, boolean noLineBreak, boolean noWordBreak,
173         boolean ignoreCase, boolean noEscape)
174         throws PatternSyntaxException JavaDoc
175     {
176         int ruleAction = SPAN | REGEXP |
177             ((noLineBreak) ? NO_LINE_BREAK : 0) |
178             ((excludeMatch) ? EXCLUDE_MATCH : 0) |
179             ((noWordBreak) ? NO_WORD_BREAK : 0) |
180             ((noEscape) ? NO_ESCAPE : 0);
181
182         return new ParserRule(ruleAction, hashChar, startPosMatch, null,
183             Pattern.compile(start,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
184             endPosMatch, end.toCharArray(), delegate, id);
185     } //}}}
186

187     //{{{ createRegexpSpanRule() method
188
public static final ParserRule createRegexpSpanRule(
189         int startPosMatch, char[] hashChars, String JavaDoc start,
190         int endPosMatch, String JavaDoc end, ParserRuleSet delegate, byte id,
191         boolean excludeMatch, boolean noLineBreak, boolean noWordBreak,
192         boolean ignoreCase, boolean noEscape)
193         throws PatternSyntaxException JavaDoc
194     {
195         int ruleAction = SPAN | REGEXP |
196             ((noLineBreak) ? NO_LINE_BREAK : 0) |
197             ((excludeMatch) ? EXCLUDE_MATCH : 0) |
198             ((noWordBreak) ? NO_WORD_BREAK : 0) |
199             ((noEscape) ? NO_ESCAPE : 0);
200
201         return new ParserRule(hashChars, ruleAction, startPosMatch, null,
202             Pattern.compile(start,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
203             endPosMatch, end.toCharArray(), delegate, id);
204     } //}}}
205

206     //{{{ createEOLSpanRule() method
207
public static final ParserRule createEOLSpanRule(
208         int posMatch, String JavaDoc seq, ParserRuleSet delegate, byte id,
209         boolean excludeMatch)
210     {
211         int ruleAction = EOL_SPAN |
212             ((excludeMatch) ? EXCLUDE_MATCH : 0)
213             | NO_LINE_BREAK;
214
215         return new ParserRule(ruleAction, seq.substring(0,1), posMatch,
216             seq.toCharArray(), null, 0, null,
217             delegate, id);
218     } //}}}
219

220     //{{{ createRegexpEOLSpanRule() method
221
/**
222      * @deprecated Use {@link #createRegexpEOLSpanRule(String,int,String,ParserRuleSet,byte,boolean,boolean)} instead
223      */

224     public static final ParserRule createRegexpEOLSpanRule(
225         char hashChar, int posMatch, String JavaDoc seq, ParserRuleSet delegate,
226         byte id, boolean excludeMatch, boolean ignoreCase)
227         throws PatternSyntaxException JavaDoc
228     {
229         return createRegexpEOLSpanRule(String.valueOf(hashChar),
230             posMatch,seq,delegate,id,excludeMatch,ignoreCase);
231     } //}}}
232

233     //{{{ createRegexpEOLSpanRule() method
234
public static final ParserRule createRegexpEOLSpanRule(
235         String JavaDoc hashChar, int posMatch, String JavaDoc seq, ParserRuleSet delegate,
236         byte id, boolean excludeMatch, boolean ignoreCase)
237         throws PatternSyntaxException JavaDoc
238     {
239         int ruleAction = EOL_SPAN | REGEXP |
240             ((excludeMatch) ? EXCLUDE_MATCH : 0)
241             | NO_LINE_BREAK;
242
243         return new ParserRule(ruleAction, hashChar, posMatch,
244             null, Pattern.compile(seq,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
245             0, null, delegate, id);
246     } //}}}
247

248     //{{{ createRegexpEOLSpanRule() method
249
public static final ParserRule createRegexpEOLSpanRule(
250         int posMatch, char[] hashChars, String JavaDoc seq, ParserRuleSet delegate,
251         byte id, boolean excludeMatch, boolean ignoreCase)
252         throws PatternSyntaxException JavaDoc
253     {
254         int ruleAction = EOL_SPAN | REGEXP |
255             ((excludeMatch) ? EXCLUDE_MATCH : 0)
256             | NO_LINE_BREAK;
257
258         return new ParserRule(hashChars, ruleAction, posMatch,
259             null, Pattern.compile(seq,(ignoreCase ? Pattern.CASE_INSENSITIVE : 0)),
260             0, null, delegate, id);
261     } //}}}
262

263     //{{{ createMarkFollowingRule() method
264
public static final ParserRule createMarkFollowingRule(
265         int posMatch, String JavaDoc seq, byte id, boolean excludeMatch)
266     {
267         int ruleAction = MARK_FOLLOWING |
268             ((excludeMatch) ? EXCLUDE_MATCH : 0);
269
270         return new ParserRule(ruleAction, seq.substring(0,1), posMatch,
271             seq.toCharArray(), null, 0, null, null, id);
272     } //}}}
273

274     //{{{ createMarkPreviousRule() method
275
public static final ParserRule createMarkPreviousRule(
276         int posMatch, String JavaDoc seq, byte id, boolean excludeMatch)
277     {
278         int ruleAction = MARK_PREVIOUS |
279             ((excludeMatch) ? EXCLUDE_MATCH : 0);
280
281         return new ParserRule(ruleAction, seq.substring(0,1), posMatch,
282             seq.toCharArray(), null, 0, null, null, id);
283     } //}}}
284

285     //{{{ createEscapeRule() method
286
public static final ParserRule createEscapeRule(String JavaDoc seq)
287     {
288         int ruleAction = IS_ESCAPE;
289
290         return new ParserRule(ruleAction, seq.substring(0,1),
291             0, seq.toCharArray(), null, 0, null,
292             null, Token.NULL);
293     } //}}}
294

295     //{{{ toString() method
296
public String JavaDoc toString()
297     {
298         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
299         result.append(getClass().getName()).append("[action=");
300         switch (action & MAJOR_ACTIONS)
301         {
302             case SEQ: result.append("SEQ"); break;
303             case SPAN: result.append("SPAN"); break;
304             case MARK_PREVIOUS: result.append("MARK_PREVIOUS"); break;
305             case MARK_FOLLOWING: result.append("MARK_FOLLOWING"); break;
306             case EOL_SPAN: result.append("EOL_SPAN"); break;
307             default: result.append("UNKNOWN"); break;
308         }
309         int actionHints = action & ACTION_HINTS;
310         result.append("[EXCLUDE_MATCH=").append((actionHints & EXCLUDE_MATCH) != 0);
311         result.append(",NO_LINE_BREAK=").append((actionHints & NO_LINE_BREAK) != 0);
312         result.append(",NO_WORD_BREAK=").append((actionHints & NO_WORD_BREAK) != 0);
313         result.append(",IS_ESCAPE=").append((actionHints & IS_ESCAPE) != 0);
314         result.append(",NO_ESCAPE=").append((actionHints & NO_ESCAPE) != 0);
315         result.append(",REGEXP=").append((actionHints & REGEXP) != 0);
316         result.append("],hashChar=").append(hashChar);
317         result.append(",upHashChar=").append(upHashChar);
318         result.append(",hashChars=").append(Arrays.toString(hashChars));
319         result.append(",upHashChars=").append(Arrays.toString(upHashChars));
320         result.append(",startPosMatch=");
321         result.append("[AT_LINE_START=").append((startPosMatch & AT_LINE_START) != 0);
322         result.append(",AT_WHITESPACE_END=").append((startPosMatch & AT_WHITESPACE_END) != 0);
323         result.append(",AT_WORD_START=").append((startPosMatch & AT_WORD_START) != 0);
324         result.append("],start=").append(null==start?null:String.valueOf(start));
325         result.append(",startRegexp=").append(startRegexp);
326         result.append(",endPosMatch=");
327         result.append("[AT_LINE_START=").append((endPosMatch & AT_LINE_START) != 0);
328         result.append(",AT_WHITESPACE_END=").append((endPosMatch & AT_WHITESPACE_END) != 0);
329         result.append(",AT_WORD_START=").append((endPosMatch & AT_WORD_START) != 0);
330         result.append("],end=").append(null==end?null:String.valueOf(end));
331         result.append(",delegate=").append(delegate);
332         result.append(",token=").append(Token.tokenToString(token)).append(']');
333         return result.toString();
334     } //}}}
335

336     //{{{ Private members
337
private ParserRule(int action, String JavaDoc hashChar,
338         int startPosMatch, char[] start, Pattern JavaDoc startRegexp,
339         int endPosMatch, char[] end,
340         ParserRuleSet delegate, byte token)
341     {
342         this.action = action;
343         this.hashChar = hashChar;
344         this.upHashChar = null == hashChar ? null : hashChar.toUpperCase();
345         this.hashChars = null;
346         this.upHashChars = null;
347         this.startPosMatch = startPosMatch;
348         this.start = start;
349         this.startRegexp = startRegexp;
350         this.endPosMatch = endPosMatch;
351         this.end = end;
352         this.delegate = delegate;
353         this.token = token;
354
355         if(this.delegate == null)
356         {
357             if((action & MAJOR_ACTIONS) != SEQ)
358             {
359                 this.delegate = ParserRuleSet.getStandardRuleSet(token);
360             }
361         }
362     }
363
364     private ParserRule(char[] hashChars, int action,
365         int startPosMatch, char[] start, Pattern JavaDoc startRegexp,
366         int endPosMatch, char[] end,
367         ParserRuleSet delegate, byte token)
368     {
369         this.action = action;
370         this.hashChar = null;
371         this.upHashChar = null;
372         Set JavaDoc<Character JavaDoc> hashCharsSet = new HashSet JavaDoc<Character JavaDoc>();
373         for (char c : hashChars)
374         {
375             hashCharsSet.add(c);
376         }
377         this.hashChars = new char[hashCharsSet.size()];
378         int i = 0;
379         for (Character JavaDoc c : hashCharsSet)
380         {
381             this.hashChars[i++] = c;
382         }
383         hashCharsSet = new HashSet JavaDoc<Character JavaDoc>();
384         for (char c : hashChars)
385         {
386             hashCharsSet.add(Character.toUpperCase(c));
387         }
388         this.upHashChars = new char[hashCharsSet.size()];
389         i = 0;
390         for (Character JavaDoc c : hashCharsSet)
391         {
392             this.upHashChars[i++] = c;
393         }
394         this.startPosMatch = startPosMatch;
395         this.start = start;
396         this.startRegexp = startRegexp;
397         this.endPosMatch = endPosMatch;
398         this.end = end;
399         this.delegate = delegate;
400         this.token = token;
401
402         if(this.delegate == null)
403         {
404             if((action & MAJOR_ACTIONS) != SEQ)
405             {
406                 this.delegate = ParserRuleSet.getStandardRuleSet(token);
407             }
408         }
409     } //}}}
410
}
411
Popular Tags