// FILE. . . . . /home/hak/hlt/src/hlt/osfv2/io/TaxonomyLoader.java
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . Hak-Laptop
// STARTED ON. . Mon Sep 02 15:36:47 2013

/**
 * This class is used to load an encoded taxonomy from a file. It
 * consists in a simple tokenizer for reading the contents of an encoded
 * taxonomy that was saved in the specific format defined by the
 * <tt>save</tt> method in the <tt>osf.exec.Taxonomy</tt> class.
 * 
 * @version     Last modified on Tue Jan 21 08:07:51 2014 by hak
 * @author      <a href="mailto:hak@acm.org">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; <a href="http://www.hassan-ait-kaci.net/">by the author</a>
 */

package hlt.osf.io;

import java.io.IOException;
import java.io.Reader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import hlt.language.util.Span;
import hlt.language.util.Location;

import hlt.language.io.StreamTokenizer;
import hlt.language.io.IO;

import hlt.language.tools.Misc;
// import hlt.language.tools.Debug;

public class TaxonomyLoader
{
  private String _file;
  private FileReader _reader;
  private StreamTokenizer _st;

  private int _tokenType;
  private int _intValue;
  private String _symbolValue;

  private int _rank = -1;
  private boolean _eof = false;

  public final static byte EOF = StreamTokenizer.TT_EOF;
  public final static byte EOL = StreamTokenizer.TT_EOL;
  public final static byte WORD = StreamTokenizer.TT_WORD;
  public final static byte NUMBER = StreamTokenizer.TT_NUMBER;

  public final int tokenType ()
    {
      return _tokenType;
    }

  public final int intValue ()
    {
      return _intValue;
    }

  public final String symbolValue ()
    {
      return _symbolValue;
    }

  public final boolean eof ()
    {
      return _eof;
    }

  public TaxonomyLoader (String file) throws FileNotFoundException
    {
      _file = file;
      _reader = new FileReader(file);
      setReader(_reader);
    }

  public final void setReader (Reader reader)
    {
      _st = new StreamTokenizer(reader);
      _st.eolIsSignificant(true);
      _st.quoteChar('\'');
      _st.quoteChar('"');
      _st.wordChars("-_@");

      // Debug.setDebugFlag(true);
    }

  public final Reader getReader ()
    {
      return _reader;
    }

  public final int lineNumber ()
    {
      return _st.lineno();
    }

  public final Location tokenStart ()
    {
      return _st.tokenStart();
    }

  public final Location tokenEnd ()
    {
      return _st.tokenEnd();
    }

  public final void error () throws BadTokenException
    {
      throw new BadTokenException((new Span(tokenStart(),tokenEnd())).toString());
    }

  final void eol ()
    {
      _rank++;
    }

  final void setEof ()
    {
      _eof = true;
    }

  public final void close () throws IOException
    {
      _reader.close();
    }

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

  public final boolean nextToken () throws IOException
    {
      _tokenType = _st.nextToken();

      // if (Debug.flagIsOn())
      // 	{
      // 	  System.out.println("Stream tokenizer's current state: "+_st);
      // 	}

      switch (_tokenType)
        {
        case StreamTokenizer.TT_SPECIAL:
          return nextToken();
        case StreamTokenizer.TT_EOF:
	  // if (Debug.flagIsOn())
	  //   System.out.println("READ END OF FILE");
	  setEof();
          break;
        case StreamTokenizer.TT_EOL:
	  eol();
	  break;
        case StreamTokenizer.TT_NUMBER:
	  _intValue = (int)_st.nval;
          break;
        case '"': case '\'':
	  _tokenType = StreamTokenizer.TT_WORD;
        case StreamTokenizer.TT_WORD:
	  _symbolValue = _st.sval;
	  break;
	default:
	  return false;
        }

      return true;
    }
}

