PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 0ms

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

#
Java | 113 lines | 48 code | 12 blank | 53 comment | 6 complexity | fd4a0d8a2befd3535e039829f1f12dc3 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 4755 2003-06-03 22:02:40Z 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	//{{{ Protected members
 75	protected Token firstToken, lastToken;
 76
 77	//{{{ getParserRuleSet() method
 78	protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
 79	{
 80		while(context != null)
 81		{
 82			if(!context.rules.isBuiltIn())
 83				return context.rules;
 84
 85			context = context.parent;
 86		}
 87
 88		return null;
 89	} //}}}
 90
 91	//{{{ createToken() method
 92	protected Token createToken(byte id, int offset, int length,
 93		TokenMarker.LineContext context)
 94	{
 95		return new Token(id,offset,length,getParserRuleSet(context));
 96	} //}}}
 97
 98	//{{{ addToken() method
 99	protected void addToken(Token token, TokenMarker.LineContext context)
100	{
101		if(firstToken == null)
102		{
103			firstToken = lastToken = token;
104		}
105		else
106		{
107			lastToken.next = token;
108			lastToken = lastToken.next;
109		}
110	} //}}}
111
112	//}}}
113}