PageRenderTime 313ms CodeModel.GetById 271ms app.highlight 6ms RepoModel.GetById 17ms app.codeStats 17ms

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

#
Java | 140 lines | 57 code | 14 blank | 69 comment | 6 complexity | b5be20f93903490c829e500fa7eb271f MD5 | raw file
  1/*
  2 * DefaultTokenHandler.java - Builds a linked list of Token objects
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2002 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.syntax;
 24
 25import javax.swing.text.Segment;
 26
 27/**
 28 * Builds a linked list of tokens without any additional processing.
 29 *
 30 * @author Slava Pestov
 31 * @version $Id: DefaultTokenHandler.java 4902 2003-10-26 19:43:58Z spestov $
 32 * @since jEdit 4.1pre1
 33 */
 34public class DefaultTokenHandler implements TokenHandler
 35{
 36	//{{{ reset() method
 37	/**
 38	 * Clears the list of tokens.
 39	 */
 40	public void init()
 41	{
 42		lastToken = firstToken = null;
 43	} //}}}
 44
 45	//{{{ getTokens() method
 46	/**
 47	 * Returns the first syntax token.
 48	 * @since jEdit 4.1pre1
 49	 */
 50	public Token getTokens()
 51	{
 52		return firstToken;
 53	} //}}}
 54
 55	//{{{ handleToken() method
 56	/**
 57	 * Called by the token marker when a syntax token has been parsed.
 58	 * @param seg The segment containing the text
 59	 * @param id The token type (one of the constants in the
 60	 * {@link Token} class).
 61	 * @param offset The start offset of the token
 62	 * @param length The number of characters in the token
 63	 * @param context The line context
 64	 * @since jEdit 4.2pre3
 65	 */
 66	public void handleToken(Segment seg, byte id, int offset, int length,
 67		TokenMarker.LineContext context)
 68	{
 69		Token token = createToken(id,offset,length,context);
 70		if(token != null)
 71			addToken(token,context);
 72	} //}}}
 73
 74	//{{{ getLineContext() method
 75	/**
 76	 * The token handler can compare this object with the object
 77	 * previously given for this line to see if the token type at the end
 78	 * of the line has changed (meaning subsequent lines might need to be
 79	 * retokenized).
 80	 * @since jEdit 4.2pre6
 81	 */
 82	public TokenMarker.LineContext getLineContext()
 83	{
 84		return lineContext;
 85	} //}}}
 86
 87	//{{{ setLineContext() method
 88	/**
 89	 * The token handler can compare this object with the object
 90	 * previously given for this line to see if the token type at the end
 91	 * of the line has changed (meaning subsequent lines might need to be
 92	 * retokenized).
 93	 * @since jEdit 4.2pre6
 94	 */
 95	public void setLineContext(TokenMarker.LineContext lineContext)
 96	{
 97		this.lineContext = lineContext;
 98	} //}}}
 99
100	//{{{ Protected members
101	protected Token firstToken, lastToken;
102	protected TokenMarker.LineContext lineContext;
103
104	//{{{ getParserRuleSet() method
105	protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
106	{
107		while(context != null)
108		{
109			if(!context.rules.isBuiltIn())
110				return context.rules;
111
112			context = context.parent;
113		}
114
115		return null;
116	} //}}}
117
118	//{{{ createToken() method
119	protected Token createToken(byte id, int offset, int length,
120		TokenMarker.LineContext context)
121	{
122		return new Token(id,offset,length,getParserRuleSet(context));
123	} //}}}
124
125	//{{{ addToken() method
126	protected void addToken(Token token, TokenMarker.LineContext context)
127	{
128		if(firstToken == null)
129		{
130			firstToken = lastToken = token;
131		}
132		else
133		{
134			lastToken.next = token;
135			lastToken = lastToken.next;
136		}
137	} //}}}
138
139	//}}}
140}