* Copyright (C) 2011 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
package org.fife.rsta.ac.common;
import javax.swing.text.Element;
import org.fife.rsta.ac.LanguageSupport;
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
* Returns non-whitespace, non-comment tokens from an {@link RSyntaxDocument},
* one at a time. This can be used by simplistic {@link LanguageSupport}s to
* "parse" for simple, easily-identifiable tokens, such as curly braces and
* {@link Token#VARIABLE}s. For example, to identify code blocks for languages
* structured like C and Java, you can use this class in conjunction with
* {@link CodeBlock} and {@link VariableDeclaration} to create an
* easily-parsable model of your source code.
public class TokenScanner {
private RSyntaxDocument doc;
public TokenScanner(RSyntaxTextArea textArea) {
this((RSyntaxDocument)textArea.getDocument());
public TokenScanner(RSyntaxDocument doc) {
root = doc.getDefaultRootElement();
t = null;//textArea.getTokenListForLine(line++);
* Returns the document being parsed.
public RSyntaxDocument getDocument() {
* Returns the next non-whitespace, non-comment token in the text area.
* @return The next token, or <code>null</code> if we are at the end of
while (next!=null && (next.isWhitespace() || next.isComment())) {
* Returns the next token in the text area.
* @return The next token, or <code>null</code> if we are at the end of
private Token nextRaw() {
if (t==null || !t.isPaintable()) {
int lineCount = root.getElementCount();
while (line<lineCount && (t==null || !t.isPaintable())) {
t = doc.getTokenListForLine(line++);