processing.app.syntax.TokenMarker Maven / Gradle / Ivy
/*
* TokenMarker.java - Generic token marker
* Copyright (C) 1998, 1999 Slava Pestov
*
* You may use and modify this package for any purpose. Redistribution is
* permitted, in both source and binary form, provided that this notice
* remains intact in all source distributions of this package.
*/
package processing.app.syntax;
import javax.swing.text.Segment;
/**
* A token marker that splits lines of text into tokens. Each token carries
* a length field and an indentification tag that can be mapped to a color
* for painting that token.
*
* For performance reasons, the linked list of tokens is reused after each
* line is tokenized. Therefore, the return value of markTokens
* should only be used for immediate painting. Notably, it cannot be
* cached.
*
* @author Slava Pestov
*/
public abstract class TokenMarker {
public interface TokenListener {
void addToken(int length, byte id);
}
// Only needed during markTokensImpl() call so addToken() can be forwarded
private TokenListener tokenListener = null;
public final void setTokenListener(TokenListener listener) {
this.tokenListener = listener;
}
public final TokenMarkerState createStateInstance() {
return new TokenMarkerState(this);
}
/**
* Creates a new TokenMarker
. This DOES NOT create
* a lineInfo array; an initial call to insertLines()
* does that.
*/
protected TokenMarker() { }
abstract public void addColoring(String keyword, String coloring);
/**
* An abstract method that splits a line up into tokens. It
* should parse the line, and call addToken()
to
* add syntax tokens to the token list. Then, it should return
* the initial token type for the next line.
*
* For example if the current line contains the start of a
* multiline comment that doesn't end on that line, this method
* should return the comment token type so that it continues on
* the next line.
*
* @param token The initial token type for this line
* @param line The line to be tokenized
* @param lineIndex The index of the line in the document,
* starting at 0
* @return The initial token type for the next line
*/
protected abstract byte markTokensImpl(byte token, Segment line,
int lineIndex);
protected final void addToken(int length, byte id) {
if (tokenListener != null) {
tokenListener.addToken(length, id);
}
}
/**
* Returns if the token marker supports tokens that span multiple
* lines. If this is true, the object using this token marker is
* required to pass all lines in the document to the
* markTokens()
method (in turn).
*
* The default implementation returns true; it should be overridden
* to return false on simpler token markers for increased speed.
*/
public boolean supportsMultilineTokens() {
return true;
}
}