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

📄 abstractjflextokenmaker.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * AbstractJFlexTokenMaker.java - Base class for token makers generated from
 * programs such as JFlex.
 */
package org.fife.ui.rsyntaxtextarea;

import javax.swing.text.Segment;

/**
 * Base class for JFlex-generated token makers.  This class attempts to factor
 * out all common code from these classes.  Many methods <em>almost</em> could
 * be factored out into this class, but cannot because they reference JFlex
 * variables that we cannot access from this class.
 */
public abstract class AbstractJFlexTokenMaker
    implements TokenMaker {

  /**
   * The first token in the returned linked list.
   */
  protected Token firstToken;

  /**
   * Used in the creation of the linked list.
   */
  protected Token currentToken;

  /**
   * Used in the creation of the linked list.
   */
  protected Token previousToken;

  /**
   * The factory that gives us our tokens to use.
   */
  protected TokenFactory tokenFactory;

  protected Segment s;

  protected int start; // Just for states.
  protected int offsetShift; // As parser always starts at 0, but our line doesn't.

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

  /**
   * Constructor.
   */
  public AbstractJFlexTokenMaker() {
    firstToken = currentToken = previousToken = null;
    tokenFactory = new DefaultTokenFactory();
  }

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

  /**
   * Adds a null token to the end of the current linked list of tokens.
   * This should be put at the end of the linked list whenever the last
   * token on the current line is NOT a multiline token.
   */
  public void addNullToken() {
    if (firstToken == null) {
      firstToken = tokenFactory.createToken();
      currentToken = firstToken;
    }
    else {
      currentToken.setNextToken(tokenFactory.createToken());
      previousToken = currentToken;
      currentToken = currentToken.getNextToken();
    }
  }

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

  /**
   * Adds the token specified to the current linked list of tokens.
   *
   * @param array The character array.
   * @param start The starting offset in the array.
   * @param end The ending offset in the array.
   * @param tokenType The token's type.
   * @param startOffset The offset in the document at which this token
   *                    occurs.
   */
  public void addToken(char[] array, int start, int end, int tokenType,
                       int startOffset) {

    if (firstToken == null) {
      firstToken = tokenFactory.createToken(array, start, end,
                                            startOffset, tokenType);
      currentToken = firstToken; // previous token is still null.
    }
    else {
      currentToken.setNextToken(tokenFactory.createToken(array,
          start, end, startOffset, tokenType));
      previousToken = currentToken;
      currentToken = currentToken.getNextToken();
    }

  }

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

  /**
   * Returns the last token on this line's type if the token is "unfinished",
   * or <code>Token.NULL</code> if it was finished.  For example, if C-style
   * syntax highlighting is being implemented, and <code>text</code>
   * contained a line of code that contained the beginning of a comment but
   * no end-comment marker ("*\/"), then this method would return
   * <code>Token.COMMENT_MULTILINE</code> for that line.  This is useful
   * for doing syntax highlighting.
   *
   * @param text The line of tokens to examine.
   * @param initialTokenType The token type to start with (i.e., the value
   *        of <code>getLastTokenTypeOnLine</code> for the line before
   *        <code>text</code>).
   * @return The last token on this line's type, or <code>Token.NULL</code>
   *         if the line was completed.
   */
  public int getLastTokenTypeOnLine(Segment text, int initialTokenType) {

    // Last param doesn't matter if we're not painting.
    Token t = getTokenList(text, initialTokenType, 0);

    while (t.getNextToken() != null) {
      t = t.getNextToken();

    }
    return t.type;

  }

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

  /**
   * Returns the first token in the linked list of tokens generated
   * from <code>text</code>.  This method must be implemented by
   * subclasses so they can correctly implement syntax highlighting.
   *
   * @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 abstract Token getTokenList(Segment text, int initialTokenType,
                                     int startOffset);

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

  /**
   * Deletes the linked list of tokens so we can begin anew.  This should
   * never have to be called by the programmer, as it is automatically
   * called whenever the user calls <code>getLastTokenTypeOnLine</code> or
   * <code>getTokenList</code>.
   */
  protected void resetTokenList() {
    firstToken = currentToken = previousToken = null;
    tokenFactory.resetAllTokens();
  }

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

  /**
   * Sets whether tokens are generated that "show" whitespace.
   *
   * @param visible Whether whitespace should be visible.
   */
  public void setWhitespaceVisible(boolean visible, RSyntaxTextArea textArea) {
    // FIXME:  Initialize with the proper sizes.
    if (visible) {
      tokenFactory = new VisibleWhitespaceTokenFactory();
    }
    else {
      tokenFactory = new DefaultTokenFactory();
      //tokenFactory = new GlyphVectorTokenFactory(textArea);
    }
  }

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

}

⌨️ 快捷键说明

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