// package programs;

/* 
 * Copyright (C) 2007  Anders Nilsson <anders.nilsson@cs.lth.se>
 *
 * This file is part of XmlSchemaCompiler.
 */

import AST.Start;
import java.io.File;
import java.io.PrintStream;

public class GenCompiler extends Parser {
	public static void main(String args[]) {

        String dir = "examples/x3d";
        String outName = "x3d";
        String[] schema = new String[1];
        boolean first = true;

        for (int i=0; i<args.length; i++) {
            if (args[i].equals("-d")) {
                dir = args[++i];
            } else if (args[i].equals("-o")) {
                outName = args[++i];
            } else {
                // OK, we suppose the remaing arg to be parsed.
                schema[0] = args[i++];
            }
        }


		Start ast = parse(schema);

		File siarasDir = new File(dir);
		if (!siarasDir.isDirectory()) {
			siarasDir.mkdir();
		}
		
		String fileName = null;
        String common = dir+File.separator+outName;
		try {
			// Generate JastAdd abstract grammar
			fileName = common+".ast";
			PrintStream pStream = new PrintStream(new File(fileName));
			ast.genAbsGrammar(pStream);

			// Generate JavaCC input
			fileName = common+".jjt";
			pStream = new PrintStream(new File(fileName));
			ast.genJavaCC(pStream);

			// Generate Parser.java
			fileName = dir+"/Parser.java";
			pStream = new PrintStream(new File(fileName));
			ast.genParser(pStream);

			// Generate PrettyPrint.java
			fileName = dir+"/PrettyPrint.java";
			pStream = new PrintStream(new File(fileName));
			ast.genPrettyPrint(pStream);

			// Generate aspects
			fileName = dir+"/GeneratedAspects.jrag";
			pStream = new PrintStream(new File(fileName));
			ast.genAspects(pStream);

		} catch (java.io.FileNotFoundException e) {
			System.out.println("Could not create file: "+fileName);
			e.printStackTrace();
		}
	}
}