//TxtParser.java

package code.loader.parser;

import java.io.*;
import java.util.*;

import code.type.*;
import code.table.*;
import code.symbols.*;
import code.instr.*;
import code.loader.parser.txtParser.*;
import code.loader.except.*;
import code.modules.CurryModule;


/**
 * Parse and get the txt based module information from the stream provided. <P>
 * <p/>
 * <B>Class Responsibilities</B> :
 * <OL>
 * <LI> Parse the txt based compiled module</LI>
 * <LI> return back important information of the module through public
 * functions</LI>
 * </OL>
 * <B>Class Collaborators</B> :
 * <OL>
 * <LI> symbol package </LI>
 * <LI> type package </LI>
 * </OL>
 *
 * @author jimeng
 * @since Jan 09, 2003
 */

public class TxtParser implements ModuleParser {
    private Vector imported = null;
    private Vector allSymbols = null;
    private boolean compiled;
    private int timestamp;
    private String source = null;
    private String moduleName = null ;


    /**
     * @param in Description of the Parameter
     * @throws ParseException Description of the Exception
     */
    public void parse(BufferedReader in) throws ParseException {
        //to contain the imported and allSymbols Vectors returned by parser
        Object[] a = null;
        try {
            TxtCupParser parser_obj = new TxtCupParser(new Yylex(in));
            a = (Object[]) parser_obj.parse().value;
        } catch (DuplicatedSymbol e) {
	    // It should print a nice message and recover 
            throw new ParseException(e.getMessage());
        } catch (Exception e) {
            throw new ParseException(e.getMessage());
        }
        imported = (Vector) a[0];
        allSymbols = (Vector) a[1];
        compiled = ((Boolean) a[2]).booleanValue();
        source = (String) a[3];
        moduleName = (String) a[4];
        timestamp = ((Integer)a[5]).intValue();
    }

    public Vector getImported() { return imported; }

    public Vector getAllSymbols() { return allSymbols; }

    public boolean isCompiled() { return compiled; }

    public String moduleName() { return moduleName; }

    public int getTimestamp() { return timestamp;}


    // Because of the way text parsing works we actually have to parse
    // the whole file here. The binary format and XML format shouldn't
    // have this problem.
    /**
     * Initializes the parser for incremental parsing of the file.
     *
     * @param in Reader that is prepared to read in the module file's contents.
     * @throws ParseException Description Needed.
     */
    public void initParser(BufferedReader in) throws ParseException {
        parse(in);
    }

}
