package metalexer.jflex;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.SortedSet;
import java.util.TreeSet;

import metalexer.*;
import metalexer.ast.Component;

/**
 * A utility for producing the output file corresponding to a given input file.
 * Note that the output should be checked manually before using it as a test.
 */
public class ComponentErrorTestTool {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java metalexer.ComponentErrorTestTool {basename}");
            System.exit(1);
        }
        String basename = args[0];
        try {
            FileLoader loader = new FileLoader("test/backend-jflex");
            SortedSet<CompilationProblem> problems = new TreeSet<CompilationProblem>();
            Component actual = loader.loadComponent(basename, problems);
            assert(actual.subtreeNodesHaveCorrectParents());
            actual = actual.processInheritance(loader, problems);
            assert(actual.subtreeNodesHaveCorrectParents());

            PrintWriter out = new PrintWriter(new FileWriter("test/backend-jflex/" + basename + ".out"));
            for(CompilationError error : ProblemUtil.extractErrors(problems)) {
                out.println(ScannerUtil.stringifyValue(error));
            }
            out.close();

            System.exit(0);
        } catch(IOException e) {
            e.printStackTrace();
            System.exit(2);
        }
    }
}
