PageRenderTime 64ms CodeModel.GetById 57ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/syntax/DefaultTokenHandler.java

#
Java | 111 lines | 47 code | 11 blank | 53 comment | 7 complexity | 06f8b2b46a906606170c56a59f675ad6 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
 25/**
 26 * An implementation of the <code>TokenHandler</code> interface that
 27 * builds a linked list of tokens.
 28 *
 29 * @author Slava Pestov
 30 * @version $Id: DefaultTokenHandler.java 4240 2002-06-04 08:48:13Z spestov $
 31 * @since jEdit 4.1pre1
 32 */
 33public class DefaultTokenHandler implements TokenHandler
 34{
 35	//{{{ reset() method
 36	/**
 37	 * Clears the list of tokens.
 38	 */
 39	public void init()
 40	{
 41		lastToken = firstToken = null;
 42	} //}}}
 43
 44	//{{{ getTokens() method
 45	/**
 46	 * Returns the first syntax token.
 47	 * @since jEdit 4.1pre1
 48	 */
 49	public Token getTokens()
 50	{
 51		return firstToken;
 52	} //}}}
 53
 54	//{{{ handleToken() method
 55	/**
 56	 * Called by the token marker when a syntax token has been parsed.
 57	 * @param id The token type (one of the constants in the
 58	 * <code>Token</code> class).
 59	 * @param offset The start offset of the token
 60	 * @param length The number of characters in the token
 61	 * @param context The line context
 62	 * @since jEdit 4.1pre1
 63	 */
 64	public void handleToken(byte id, int offset, int length,
 65		TokenMarker.LineContext context)
 66	{
 67		Token token = createToken(id,offset,length,context);
 68		if(token != null)
 69			addToken(token,context);
 70	} //}}}
 71
 72	//{{{ Protected members
 73	protected Token firstToken, lastToken;
 74
 75	//{{{ getParserRuleSet() method
 76	protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
 77	{
 78		while(context != null)
 79		{
 80			if(context.rules.getMode() != null)
 81				return context.rules;
 82
 83			context = context.parent;
 84		}
 85
 86		return null;
 87	} //}}}
 88
 89	//{{{ createToken() method
 90	protected Token createToken(byte id, int offset, int length,
 91		TokenMarker.LineContext context)
 92	{
 93		return new Token(id,offset,length,getParserRuleSet(context));
 94	} //}}}
 95
 96	//{{{ addToken() method
 97	protected void addToken(Token token, TokenMarker.LineContext context)
 98	{
 99		if(firstToken == null)
100		{
101			firstToken = lastToken = token;
102		}
103		else
104		{
105			lastToken.next = token;
106			lastToken = lastToken.next;
107		}
108	} //}}}
109
110	//}}}
111}