All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.rsta.ac.common.TokenScanner Maven / Gradle / Ivy

Go to download

A library adding code completion and other advanced features for Java, JavaScript, Perl, and other languages to RSyntaxTextArea.

There is a newer version: 3.3.0
Show newest version
/*
 * 01/11/2010
 *
 * 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.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class TokenScanner {

	private RSyntaxDocument doc;
	private Element root;
	private Token t;
	private int line;


	public TokenScanner(RSyntaxTextArea textArea) {
		this((RSyntaxDocument)textArea.getDocument());
	}


	public TokenScanner(RSyntaxDocument doc) {
		this.doc = doc;
		root = doc.getDefaultRootElement();
		line = 0;
		t = null;//textArea.getTokenListForLine(line++);
	}


	/**
	 * Returns the document being parsed.
	 *
	 * @return The document.
	 */
	public RSyntaxDocument getDocument() {
		return doc;
	}


	/**
	 * Returns the next non-whitespace, non-comment token in the text area.
	 *
	 * @return The next token, or null if we are at the end of
	 *         its document.
	 */
	public Token next() {
		Token next = nextRaw();
		while (next!=null && (next.isWhitespace() || next.isComment())) {
			next = nextRaw();
		}
		return next;
	}


	/**
	 * Returns the next token in the text area.
	 *
	 * @return The next token, or null if we are at the end of
	 *         its document.
	 */
	private Token nextRaw() {
		if (t==null || !t.isPaintable()) {
			int lineCount = root.getElementCount();
			while (line




© 2015 - 2024 Weber Informatics LLC | Privacy Policy