⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractregextokenmaker.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 03/22/2005
 *
 * AbstractRegExTokenMaker.java - An abstract implementation of TokenMaker that
 * uses Java regular expressions to parse input.  This engine is the basis for
 * user-defined syntax highlighting schemes.
 * Copyright (C) 2005 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.rsyntaxtextarea.custom;

import javax.swing.text.Segment;

import org.fife.ui.rsyntaxtextarea.AbstractTokenMaker;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenMap;


/**
 * A rough draft of a token maker driven by Java regular expressions.  The goal
 * is to have this class be the basis for user-defined language definitions
 * given in the form of XML files.  However, we aren't even remotely there yet.
 * You should NOT use this class.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public abstract class AbstractRegExTokenMaker extends AbstractTokenMaker {

	private int currentTokenStart;
	private int currentTokenType;

	private RuleSet defaultRuleSet;
	private RuleSet mlcRuleSet;


/*****************************************************************************/


	public AbstractRegExTokenMaker() {
	}


/*****************************************************************************/


	protected abstract RuleSet getRuleSet(int state);


/*****************************************************************************/


	/**
	 * Returns the first token in the linked list of tokens generated
	 * from <code>text</code>.
	 *
	 * @param text The text from which to get tokens.
	 * @param initialTokenType The token type we should start with.
	 * @param startOffset The offset into the document at which
	 *                    <code>text</code> starts.
	 * @return The first <code>Token</code> in a linked list representing
	 *         the syntax highlighted text.
	 */
	public final Token getTokenList(Segment text, int initialTokenType,
											int startOffset) {

		resetTokenList();

		// If this is an empty line, simply add an empty token with the
		// same type as the initialTokenType, so multiline tokens work.
		if (text.count==0) {
			switch (initialTokenType) {
				case Token.NULL:
					addNullToken();
					return firstToken;
				default:
					addToken(text.array, text.offset,text.offset-1,
							initialTokenType, startOffset);
					return firstToken;
			}
		}

		int tokenType = initialTokenType;
		String toMatch = text.toString();
		int tokenStart = 0;

		// Get the starting rule set.
		RuleSet ruleSet = getRuleSet(tokenType);
		int count = ruleSet.getRuleCount();

		// Keep looping while there's more text to match.
		int toMatchLength = toMatch.length();
		while (toMatchLength>0) {

			int maxMatchLen = 0;
			String matchText = null;
			int ruleIndex = 0;

			// Loop through the rules and find the longest match.
			for (int i=0; i<count; i++) {
				Rule rule = ruleSet.getRule(i);
				String match = rule.getMatch(toMatch);
				if (match!=null) {
					int len = match.length();
					if (len>maxMatchLen) {
						maxMatchLen = len;
						matchText = match;
						ruleIndex = i;
					}
				}
			} // End of for (int i=0; i<count; i++).

			// Ensure we got a match; assuming we have, add the token and
			// update our internal state accordingly.
			if (maxMatchLen!=0) {
				Rule rule = ruleSet.getRule(ruleIndex);
				int temp = ruleSet.getTokenTypeForIdentifier(matchText);
				// Change identifier to correct type.
				int type = temp!=-1 ? temp : rule.getType();
				char[] matchArray = matchText.toCharArray();
				addToken(matchArray, 0,maxMatchLen-1, type,
						startOffset+tokenStart);
				toMatch = toMatch.substring(maxMatchLen);
				toMatchLength -= maxMatchLen;
				tokenStart += maxMatchLen;
				if (rule.terminated==false)
					return firstToken;
				else if (tokenType!=Token.NULL) {
					ruleSet = getRuleSet(Token.NULL);
					count = ruleSet.getRuleCount();
					tokenType = Token.NULL;
				}
			}
			else {
				throw new InternalError("No match; remaining text: '" +
									toMatch + "'");
			}
				
		} // End of while (toMatchLength>0).

		// Terminated "normally," e.g., without a multiline token.
		addNullToken();
		return firstToken;

	}


/*****************************************************************************/


	/**
	 * Returns the words to highlight.
	 *
	 * @return A <code>TokenMap</code> containing the words to highlight.
	 */
	public abstract TokenMap getWordsToHighlight();


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -