import java.io.*;
import java.util.Date;
import ilog.language.syntax.*;
import ilog.language.util.Location;
import ilog.language.io.StreamTokenizer;

public class TermTokenizer implements Tokenizer
{
  BufferedReader reader;
  StreamTokenizer input;
  String file = "stdin";

  TermTokenizer () throws IOException
    {
      setupReader(new InputStreamReader(System.in));
      interactive = true;
      banner();
      prompt();
    }

  TermTokenizer (File file) throws IOException
    {
      setupReader(new FileReader(file));
      this.file = file.toString();
      interactive = false;
      banner();
      System.out.println("*** Parsing file: "+file);
    }

  private void setupReader (Reader rd)
    {
      setReader(rd);
      input = new StreamTokenizer(reader);
      input.ordinaryChars("+-./");
      input.quoteChar('\'',0); // no escape allowed
    }

  public final int lineNumber()
    {
      return input.getLineNumber();
    }

  public final void setReader (Reader rd)
    {
      reader = new BufferedReader(rd);
    }

  public final Reader getReader ()
    {
      return reader;
    }

  public static boolean interactive;
  static String prompt = "> ";

  public static final void prompt ()
    {
      if (interactive)
        {
          System.out.print(prompt);
        }
    }

  static public final void setPrompt(String p)
    {
      prompt = p;
    }

  static String banner = "*** Welcome to the dynamic operator Term Parser!\n"+
                         "*** Version of "+ (new Date());

  public static final void banner ()
    {
      System.out.println(banner);
    }

  final boolean isOtherChar (int c)
    {
      return input.isOrdinaryChar(c)
        && !(input.isWhitespaceChar(c) || c == '(' || c == ')' || c == ',');
    }

  final ParseNode locate (ParseNode node)
    {
      return ((ParseNode)node.setStart(input.tokenStart()).setEnd(input.tokenEnd()))
	.setFile(file);
    }

  final ParseNode locate (ParseNode node, Location start)
    {
      return ((ParseNode)node.setStart(start).setEnd(input.tokenEnd()))
	.setFile(file);
    }

  public ParseNode nextToken() throws IOException
    {
      ParseNode t = null;

      switch (input.nextToken())
        {
        case StreamTokenizer.TT_EOF:
          reader.close();
          locate(t = GenericParser.E_O_I);
          break;
        case '\'': case '"':
        case StreamTokenizer.TT_WORD:
	  if (input.sval == "exit")
	    t = GenericParser.literalToken("exit");
	  else
	    t = GenericParser.symbolToken("FUNCTOR",input.sval);
	  locate(t);
          break;
        case StreamTokenizer.TT_NUMBER:
          if (input.isInteger)
            t = GenericParser.numberToken("NUMBER",(int)input.nval);
          else
            t = GenericParser.numberToken("NUMBER",input.nval);
	  locate(t);
          break;
        case '(': case ')': case ',': case '.':
          locate(t = GenericParser.literalToken(String.valueOf((char)input.ttype)));
          break;
        default: // read the longest possible token and return it as a symbol
	  Location start = input.tokenStart();
          StringBuffer functor = new StringBuffer(String.valueOf((char)input.ttype));
          input.spaceIsSignificant(true);
	  while (isOtherChar(input.peek()))
	    functor.append(String.valueOf((char)input.nextToken()));	  
          input.spaceIsSignificant(false);
          t = GenericParser.symbolToken("FUNCTOR",functor.toString());
	  locate(t,start);
          break;
        }
      return t;
    }
}
