import java.util.Stack;
import java.io.Reader;
import java.io.EOFException;
import java.io.IOException;

/**
 * This is the simplest possible table-driven lexer.  It recognizes two token
 * types -- unsigned integers and character-based words.  All other
 * characters are discarded.  This assumes that all input is 7-bit ASCII; any
 * other characters are errors and cause an exception.
 *
 * @author Terran Lane
 * @version 1.0
 */
public class MiniLexer extends AbstractTableLexer {

  /**
   * Initializes this Lexer to take characters from the designated Reader
   * object.  Note that this Lexer does <em>not</em> close the Reader when
   * lexing is complete (i.e., when EOF is encountered) -- that job is left
   * up to the caller.  This lexer assumes that when EOF is encountered, the
   * token stream is done (i.e., the stream won't reset and no more tokens
   * will become available).
   *
   * @param r The Reader from which to take character data.
   */
  public MiniLexer(Reader r) {
    super(r);
    _initTab();
  }

  /* ******************** end of public interface ******************** */

  protected int _startState() { return ST_INIT; }

  protected int _getEOFIdx() { return C_EOF; }

  private void _initTab() {
    _tab=new _LexAct[ST_NUM_STATES][129];
    for (int i=0;i<128;++i) {
      if (Character.isDigit((char)i)) {
	_tab[ST_INIT][i]=new _LexAct(ST_ININT,A_SHIFT,BaseToken.T_UNKN);
	_tab[ST_ININT][i]=new _LexAct(ST_ININT,A_SHIFT,BaseToken.T_INT);
	_tab[ST_INWORD][i]=new _LexAct(ST_ININT,A_RET,BaseToken.T_WORD);
      }
      else if (Character.isLetter((char)i)) {
	_tab[ST_INIT][i]=new _LexAct(ST_INWORD,A_SHIFT,BaseToken.T_UNKN);
	_tab[ST_ININT][i]=new _LexAct(ST_INWORD,A_RET,BaseToken.T_INT);
	_tab[ST_INWORD][i]=new _LexAct(ST_INWORD,A_SHIFT,BaseToken.T_WORD);
      }
      else {
	_tab[ST_INIT][i]=new _LexAct(ST_INIT,A_DROP,BaseToken.T_UNKN);
	_tab[ST_ININT][i]=new _LexAct(ST_INIT,A_DRET,BaseToken.T_INT);
	_tab[ST_INWORD][i]=new _LexAct(ST_INIT,A_DRET,BaseToken.T_WORD);
      }
    }
    _tab[ST_INIT][C_EOF]=new _LexAct(ST_INIT,A_DROP,BaseToken.T_UNKN);
    _tab[ST_ININT][C_EOF]=new _LexAct(ST_INIT,A_DRET,BaseToken.T_INT);
    _tab[ST_INWORD][C_EOF]=new _LexAct(ST_INIT,A_DRET,BaseToken.T_WORD);
  }

  protected static final int ST_INIT=0;		// initial state
  protected static final int ST_ININT=1;	// parsing an int
  protected static final int ST_INWORD=2;	// parsing a word
  protected static final int ST_NUM_STATES=3;

  private static final int C_EOF=128;		// special char code for EOF

}
