KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ParserRuleSet.java - A set of parser rules
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999 mike dillon
7  * Portions copyright (C) 2001, 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 //{{{ Imports
27
import java.util.*;
28 import java.util.regex.Pattern JavaDoc;
29 //}}}
30

31 /**
32  * A set of parser rules.
33  * @author mike dillon
34  * @version $Id: ParserRuleSet.java 8103 2006-11-21 00:03:41Z vampire0 $
35  */

36 public class ParserRuleSet
37 {
38     //{{{ getStandardRuleSet() method
39
/**
40      * Returns a parser rule set that highlights everything with the
41      * specified token type.
42      * @param id The token type
43      */

44     public static ParserRuleSet getStandardRuleSet(byte id)
45     {
46         return standard[id];
47     } //}}}
48

49     //{{{ ParserRuleSet constructor
50
public ParserRuleSet(String JavaDoc modeName, String JavaDoc setName)
51     {
52         this.modeName = modeName;
53         this.setName = setName;
54         ruleMap = new HashMap<Character JavaDoc, List<ParserRule>>();
55         imports = new ArrayList<ParserRuleSet>();
56     } //}}}
57

58     //{{{ getModeName() method
59
public String JavaDoc getModeName()
60     {
61         return modeName;
62     } //}}}
63

64     //{{{ getSetName() method
65
public String JavaDoc getSetName()
66     {
67         return setName;
68     } //}}}
69

70     //{{{ getName() method
71
public String JavaDoc getName()
72     {
73         return modeName + "::" + setName;
74     } //}}}
75

76     //{{{ getProperties() method
77
public Hashtable<String JavaDoc, String JavaDoc> getProperties()
78     {
79         return props;
80     } //}}}
81

82     //{{{ setProperties() method
83
public void setProperties(Hashtable<String JavaDoc, String JavaDoc> props)
84     {
85         this.props = props;
86         _noWordSep = null;
87     } //}}}
88

89     //{{{ resolveImports() method
90
/**
91      * Resolves all rulesets added with {@link #addRuleSet(ParserRuleSet)}.
92      * @since jEdit 4.2pre3
93      */

94     public void resolveImports()
95     {
96         for (ParserRuleSet ruleset : imports)
97         {
98             if (!ruleset.imports.isEmpty())
99             {
100                 //prevent infinite recursion
101
ruleset.imports.remove(this);
102                 ruleset.resolveImports();
103             }
104             
105             for (List<ParserRule> rules : ruleset.ruleMap.values())
106             {
107                 for (ParserRule rule : rules)
108                 {
109                     addRule(rule);
110                 }
111             }
112
113             if (ruleset.keywords != null) {
114                 if (keywords == null)
115                     keywords = new KeywordMap(ignoreCase);
116                 keywords.add(ruleset.keywords);
117             }
118         }
119         imports.clear();
120     } //}}}
121

122     //{{{ addRuleSet() method
123
/**
124      * Adds all rules contained in the given ruleset.
125      * @param ruleset The ruleset
126      * @since jEdit 4.2pre3
127      */

128     public void addRuleSet(ParserRuleSet ruleset)
129     {
130         imports.add(ruleset);
131     } //}}}
132

133     //{{{ addRule() method
134
public void addRule(ParserRule r)
135     {
136         ruleCount++;
137         Character JavaDoc[] keys;
138         if (null == r.upHashChars)
139         {
140             keys = new Character JavaDoc[1];
141             if ((null == r.upHashChar) || (0 >= r.upHashChar.length()))
142             {
143                 keys[0] = null;
144             }
145             else
146             {
147                 keys[0] = Character.valueOf(r.upHashChar.charAt(0));
148             }
149         }
150         else
151         {
152             keys = new Character JavaDoc[r.upHashChars.length];
153             int i = 0;
154             for (char upHashChar : r.upHashChars)
155             {
156                 keys[i++] = upHashChar;
157             }
158         }
159         for (Character JavaDoc key : keys)
160         {
161             List<ParserRule> rules = ruleMap.get(key);
162             if (null == rules)
163             {
164                 rules = new ArrayList<ParserRule>();
165                 ruleMap.put(key,rules);
166             }
167             int ruleAmount = rules.size();
168             rules.add(r);
169             // fill the deprecated ParserRule.next pointer
170
if (ruleAmount > 0)
171             {
172                 rules.get(ruleAmount).next = r;
173             }
174         }
175     } //}}}
176

177     //{{{ getRules() method
178
/**
179     * @deprecated As the linking between rules is not anymore done within the rule, use {@link #getRules(Character)} instead
180     */

181     public ParserRule getRules(char ch)
182     {
183         List<ParserRule> rules = getRules(Character.valueOf(ch));
184         return rules.get(0);
185     } //}}}
186

187     //{{{ getRules() method
188
public List<ParserRule> getRules(Character JavaDoc key)
189     {
190         Character JavaDoc upperKey = null == key ? null : Character.valueOf(Character.toUpperCase(key.charValue()));
191         List<ParserRule> rules = ruleMap.get(upperKey);
192         if (null == rules)
193         {
194             rules = new ArrayList<ParserRule>();
195         }
196         else
197         {
198             rules = new ArrayList<ParserRule>(rules);
199         }
200         if (null != upperKey)
201         {
202             List<ParserRule> nullRules = ruleMap.get(null);
203             if (null != nullRules)
204             {
205                 int rulesSize = rules.size();
206                 if ((0 < rulesSize) && (0 < nullRules.size()))
207                 {
208                     rules.get(rulesSize-1).next = nullRules.get(0);
209                 }
210                 rules.addAll(nullRules);
211             }
212         }
213         return rules;
214     } //}}}
215

216     //{{{ getRuleCount() method
217
public int getRuleCount()
218     {
219         return ruleCount;
220     } //}}}
221

222     //{{{ getTerminateChar() method
223
/**
224      * Returns the number of chars that can be read before the rule parsing stops.
225      *
226      * @return a number of chars or -1 (default value) if there is no limit
227      */

228     public int getTerminateChar()
229     {
230         return terminateChar;
231     } //}}}
232

233     //{{{ setTerminateChar() method
234
public void setTerminateChar(int atChar)
235     {
236         terminateChar = (atChar >= 0) ? atChar : -1;
237     } //}}}
238

239     //{{{ getIgnoreCase() method
240
public boolean getIgnoreCase()
241     {
242         return ignoreCase;
243     } //}}}
244

245     //{{{ setIgnoreCase() method
246
public void setIgnoreCase(boolean b)
247     {
248         ignoreCase = b;
249     } //}}}
250

251     //{{{ getKeywords() method
252
public KeywordMap getKeywords()
253     {
254         return keywords;
255     } //}}}
256

257     //{{{ setKeywords() method
258
public void setKeywords(KeywordMap km)
259     {
260         keywords = km;
261         _noWordSep = null;
262     } //}}}
263

264     //{{{ getHighlightDigits() method
265
public boolean getHighlightDigits()
266     {
267         return highlightDigits;
268     } //}}}
269

270     //{{{ setHighlightDigits() method
271
public void setHighlightDigits(boolean highlightDigits)
272     {
273         this.highlightDigits = highlightDigits;
274     } //}}}
275

276     //{{{ getDigitRegexp() method
277
public Pattern JavaDoc getDigitRegexp()
278     {
279         return digitRE;
280     } //}}}
281

282     //{{{ setDigitRegexp() method
283
public void setDigitRegexp(Pattern JavaDoc digitRE)
284     {
285         this.digitRE = digitRE;
286     } //}}}
287

288     //{{{ getEscapeRule() method
289
public ParserRule getEscapeRule()
290     {
291         return escapeRule;
292     } //}}}
293

294     //{{{ setEscapeRule() method
295
public void setEscapeRule(ParserRule escapeRule)
296     {
297         addRule(escapeRule);
298         this.escapeRule = escapeRule;
299     } //}}}
300

301     //{{{ getDefault() method
302
public byte getDefault()
303     {
304         return defaultToken;
305     } //}}}
306

307     //{{{ setDefault() method
308
public void setDefault(byte def)
309     {
310         defaultToken = def;
311     } //}}}
312

313     //{{{ getNoWordSep() method
314
public String JavaDoc getNoWordSep()
315     {
316         if(_noWordSep == null)
317         {
318             _noWordSep = noWordSep;
319             if(noWordSep == null)
320                 noWordSep = "";
321             if(keywords != null)
322                 noWordSep += keywords.getNonAlphaNumericChars();
323         }
324         return noWordSep;
325     } //}}}
326

327     //{{{ setNoWordSep() method
328
public void setNoWordSep(String JavaDoc noWordSep)
329     {
330         this.noWordSep = noWordSep;
331         _noWordSep = null;
332     } //}}}
333

334     //{{{ isBuiltIn() method
335
/**
336      * Returns if this is a built-in ruleset.
337      * @since jEdit 4.2pre1
338      */

339     public boolean isBuiltIn()
340     {
341         return builtIn;
342     } //}}}
343

344     //{{{ toString() method
345
public String JavaDoc toString()
346     {
347         return getClass().getName() + '[' + modeName + "::" + setName + ']';
348     } //}}}
349

350     //{{{ Private members
351
private static ParserRuleSet[] standard;
352
353     static
354     {
355         standard = new ParserRuleSet[Token.ID_COUNT];
356         for(byte i = 0; i < Token.ID_COUNT; i++)
357         {
358             standard[i] = new ParserRuleSet(null,null);
359             standard[i].setDefault(i);
360             standard[i].builtIn = true;
361         }
362     }
363
364     private String JavaDoc modeName, setName;
365     private Hashtable<String JavaDoc, String JavaDoc> props;
366
367     private KeywordMap keywords;
368
369     private int ruleCount;
370
371     private Map<Character JavaDoc, List<ParserRule>> ruleMap;
372
373     private final List<ParserRuleSet> imports;
374
375     /**
376      * The number of chars that can be read before the parsing stops.
377      * &lt;TERMINATE AT_CHAR="1" /&gt;
378      */

379     private int terminateChar = -1;
380     private boolean ignoreCase = true;
381     private byte defaultToken;
382     private ParserRule escapeRule;
383
384     private boolean highlightDigits;
385     private Pattern JavaDoc digitRE;
386
387     private String JavaDoc _noWordSep;
388     private String JavaDoc noWordSep;
389
390     private boolean builtIn;
391     //}}}
392
}
393
Popular Tags