Package qat.parser.qashparser

Source Code of qat.parser.qashparser.QASHToken

package qat.parser.qashparser;

import java.lang.Object;
import java.lang.StringBuffer;
import java.io.StreamTokenizer;

public class QASHToken extends Object {
  public static final char TT_EOL = (char)StreamTokenizer.TT_EOL;
  public static final char TT_EOF = (char)StreamTokenizer.TT_EOF;
  public static final char TT_NUMBER = (char)StreamTokenizer.TT_NUMBER;
  public static final char TT_WORD = (char)StreamTokenizer.TT_WORD;
  public static final char TT_STRING = '"';
  public static final char ARG_COUNT = (char)54321;
  private StringBuffer value;
  public int line;
  public char ttype;
 
  public QASHToken() {
    this.value = new StringBuffer();
    this.ttype=0;
  }
 
  public QASHToken(String s, int line) {
    this(s,0,line);
    ttype = getTTYPE(s);
  }
 
  public QASHToken(double token, char ttype, int line) {
    this(Integer.toString((int)token),ttype,line);
  }
 
  public QASHToken(char token, char ttype, int line) {
    this(new StringBuffer().append(token),(char)ttype,line);
  }
 
  public QASHToken(String token, int ttype, int line) {
    this(token,(char)ttype,line);
  }
 
  public QASHToken(String token, char ttype, int line) {
    this(new StringBuffer().append(token),ttype,line);
  }
 
  public QASHToken(StringBuffer token, char ttype, int line) {
    this.value = token;
    this.line = line;
    this.ttype = ttype;
  }
 
  /**
   * This method will return TT_STRING OR TT_NUMBER
   * based on the value of the token. Tokens are treated as
   * numbers unless proven otherwise.
   */
  public static char getTTYPE(QASHToken token) {
    return getTTYPE(token.toString());
  }
 
  /**
   * This method will return TT_STRING OR TT_NUMBER
   * based on the value of the token. Tokens are treated as
   * numbers unless proven otherwise.
   */
  public static char getTTYPE(String s) {
    try {
      Integer.parseInt(s);
      return TT_NUMBER;
    }
    catch (NumberFormatException e) {
      return TT_STRING;
    }
  }
 
  public int intValue() {
    return Integer.parseInt(this.toString());
  }
  public String toString() {
    return value.toString();
  }
 
  public int compareTo(QASHToken t) {
    return this.toString().compareTo(t.toString());
  }
 
  public String detail() {
    return value+" (line "+Integer.toString(line)+")";
  }
 
  public boolean equals(Object o) {
    if (ttype==((QASHToken)o).ttype)
      //      if (value.toString().intern().equals(o.toString().intern()))
      if (this.toString().equals(o.toString()))
        return true;
      else
        return false;
    else
      return false;
  }
 
  public QASHToken append(QASHToken token) {
    if (token==null)
      return this;
    // check if either string is empty
    if (token.value.length()==0)
      return this;
    if (value.length()==0)
      return token;
    // try numerical append, if it fails, try string append
    if ((ttype==TT_NUMBER)&(token.ttype==TT_NUMBER)) {
      try {
        value = new StringBuffer(Integer.toString((this.intValue()+token.intValue())));
      }
      catch (NumberFormatException e) {
        value.append(token.value.toString());
      }
    }
    else {
      value.append(token.value.toString());
    }
    return this;
  }
 
  public Object clone() {
    return new QASHToken(this.toString(),ttype,line);
  }
 
  public boolean startsWith(String s) {
    return value.toString().startsWith(s);
  }
}

TOP

Related Classes of qat.parser.qashparser.QASHToken

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.