PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/syntax/ParserRuleSet.java

#
Java | 335 lines | 201 code | 46 blank | 88 comment | 15 complexity | eaba5570d0974d5df6fb1254671341a3 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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
 24package org.gjt.sp.jedit.syntax;
 25
 26//{{{ Imports
 27import java.util.*;
 28import java.util.regex.Pattern;
 29//}}}
 30
 31/**
 32 * A set of parser rules.
 33 * @author mike dillon
 34 * @version $Id: ParserRuleSet.java 5471 2006-06-22 06:31:23Z kpouer $
 35 */
 36public 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 modeName, String setName)
 51	{
 52		this.modeName = modeName;
 53		this.setName = setName;
 54		ruleMapFirst = new ParserRule[RULE_BUCKET_COUNT];
 55		ruleMapLast = new ParserRule[RULE_BUCKET_COUNT];
 56		imports = new LinkedList();
 57	} //}}}
 58
 59	//{{{ getModeName() method
 60	public String getModeName()
 61	{
 62		return modeName;
 63	} //}}}
 64
 65	//{{{ getSetName() method
 66	public String getSetName()
 67	{
 68		return setName;
 69	} //}}}
 70
 71	//{{{ getName() method
 72	public String getName()
 73	{
 74		return modeName + "::" + setName;
 75	} //}}}
 76
 77	//{{{ getProperties() method
 78	public Hashtable getProperties()
 79	{
 80		return props;
 81	} //}}}
 82
 83	//{{{ setProperties() method
 84	public void setProperties(Hashtable props)
 85	{
 86		this.props = props;
 87		_noWordSep = null;
 88	} //}}}
 89
 90	//{{{ resolveImports() method
 91	/**
 92	 * Resolves all rulesets added with {@link #addRuleSet(ParserRuleSet)}.
 93	 * @since jEdit 4.2pre3
 94	 */
 95	public void resolveImports()
 96	{
 97		Iterator iter = imports.iterator();
 98		while(iter.hasNext())
 99		{
100			ParserRuleSet ruleset = (ParserRuleSet)iter.next();
101			for(int i = 0; i < ruleset.ruleMapFirst.length; i++)
102			{
103				ParserRule rule = ruleset.ruleMapFirst[i];
104				while(rule != null)
105				{
106					addRule(rule);
107					rule = rule.next;
108				}
109			}
110
111			if(ruleset.keywords != null)
112			{
113				if(keywords == null)
114					keywords = new KeywordMap(ignoreCase);
115				keywords.add(ruleset.keywords);
116			}
117		}
118		imports.clear();
119	} //}}}
120
121	//{{{ addRuleSet() method
122	/**
123	 * Adds all rules contained in the given ruleset.
124	 * @param ruleset The ruleset
125	 * @since jEdit 4.2pre3
126	 */
127	public void addRuleSet(ParserRuleSet ruleset)
128	{
129		imports.add(ruleset);
130	} //}}}
131
132	//{{{ addRule() method
133	public void addRule(ParserRule r)
134	{
135		ruleCount++;
136
137		int key = Character.toUpperCase(r.hashChar)
138			% RULE_BUCKET_COUNT;
139		ParserRule last = ruleMapLast[key];
140		if(last == null)
141			ruleMapFirst[key] = ruleMapLast[key] = r;
142		else
143		{
144			last.next = r;
145			ruleMapLast[key] = r;
146		}
147	} //}}}
148
149	//{{{ getRules() method
150	public ParserRule getRules(char ch)
151	{
152		int key = Character.toUpperCase(ch) % RULE_BUCKET_COUNT;
153		return ruleMapFirst[key];
154	} //}}}
155
156	//{{{ getRuleCount() method
157	public int getRuleCount()
158	{
159		return ruleCount;
160	} //}}}
161
162	//{{{ getTerminateChar() method
163	/**
164	 * Returns the number of chars that can be read before the rule parsing stops.
165	 *
166	 * @return a number of chars or -1 (default value) if there is no limit
167	 */
168	public int getTerminateChar()
169	{
170		return terminateChar;
171	} //}}}
172
173	//{{{ setTerminateChar() method
174	public void setTerminateChar(int atChar)
175	{
176		terminateChar = (atChar >= 0) ? atChar : -1;
177	} //}}}
178
179	//{{{ getIgnoreCase() method
180	public boolean getIgnoreCase()
181	{
182		return ignoreCase;
183	} //}}}
184
185	//{{{ setIgnoreCase() method
186	public void setIgnoreCase(boolean b)
187	{
188		ignoreCase = b;
189	} //}}}
190
191	//{{{ getKeywords() method
192	public KeywordMap getKeywords()
193	{
194		return keywords;
195	} //}}}
196
197	//{{{ setKeywords() method
198	public void setKeywords(KeywordMap km)
199	{
200		keywords = km;
201		_noWordSep = null;
202	} //}}}
203
204	//{{{ getHighlightDigits() method
205	public boolean getHighlightDigits()
206	{
207		return highlightDigits;
208	} //}}}
209
210	//{{{ setHighlightDigits() method
211	public void setHighlightDigits(boolean highlightDigits)
212	{
213		this.highlightDigits = highlightDigits;
214	} //}}}
215
216	//{{{ getDigitRegexp() method
217	public Pattern getDigitRegexp()
218	{
219		return digitRE;
220	} //}}}
221
222	//{{{ setDigitRegexp() method
223	public void setDigitRegexp(Pattern digitRE)
224	{
225		this.digitRE = digitRE;
226	} //}}}
227
228	//{{{ getEscapeRule() method
229	public ParserRule getEscapeRule()
230	{
231		return escapeRule;
232	} //}}}
233
234	//{{{ setEscapeRule() method
235	public void setEscapeRule(ParserRule escapeRule)
236	{
237		addRule(escapeRule);
238		this.escapeRule = escapeRule;
239	} //}}}
240
241	//{{{ getDefault() method
242	public byte getDefault()
243	{
244		return defaultToken;
245	} //}}}
246
247	//{{{ setDefault() method
248	public void setDefault(byte def)
249	{
250		defaultToken = def;
251	} //}}}
252
253	//{{{ getNoWordSep() method
254	public String getNoWordSep()
255	{
256		if(_noWordSep == null)
257		{
258			_noWordSep = noWordSep;
259			if(noWordSep == null)
260				noWordSep = "";
261			if(keywords != null)
262				noWordSep += keywords.getNonAlphaNumericChars();
263		}
264		return noWordSep;
265	} //}}}
266
267	//{{{ setNoWordSep() method
268	public void setNoWordSep(String noWordSep)
269	{
270		this.noWordSep = noWordSep;
271		_noWordSep = null;
272	} //}}}
273
274	//{{{ isBuiltIn() method
275	/**
276	 * Returns if this is a built-in ruleset.
277	 * @since jEdit 4.2pre1
278	 */
279	public boolean isBuiltIn()
280	{
281		return builtIn;
282	} //}}}
283
284	//{{{ toString() method
285	public String toString()
286	{
287		return getClass().getName() + '[' + modeName + "::" + setName + ']';
288	} //}}}
289
290	//{{{ Private members
291	private static ParserRuleSet[] standard;
292
293	static
294	{
295		standard = new ParserRuleSet[Token.ID_COUNT];
296		for(byte i = 0; i < Token.ID_COUNT; i++)
297		{
298			standard[i] = new ParserRuleSet(null,null);
299			standard[i].setDefault(i);
300			standard[i].builtIn = true;
301		}
302	}
303
304	private static final int RULE_BUCKET_COUNT = 128;
305
306	private String modeName, setName;
307	private Hashtable props;
308
309	private KeywordMap keywords;
310
311	private int ruleCount;
312
313	private ParserRule[] ruleMapFirst;
314	private ParserRule[] ruleMapLast;
315
316	private LinkedList imports;
317
318	/**
319	 * The number of chars that can be read before the parsing stops.
320	 * &lt;TERMINATE AT_CHAR="1" /&gt;
321	 */
322	private int terminateChar = -1;
323	private boolean ignoreCase = true;
324	private byte defaultToken;
325	private ParserRule escapeRule;
326
327	private boolean highlightDigits;
328	private Pattern digitRE;
329
330	private String _noWordSep;
331	private String noWordSep;
332
333	private boolean builtIn;
334	//}}}
335}