package xyz.ignatz.antlr.aidl;

import java.io.*;

import xyz.ignatz.antlr.aidl.recognizer.*;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class AidlToJson
{
    public static void main(String[] args)
    {
        if (args.length < 2) {
            System.out.println("AidlToJson input_filename output_directory");
            return;
        }
        CharStream input = null;
        try {
            input = CharStreams.fromFileName(args[0]);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AidlLexer lexer = new AidlLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        AidlParser parser = new AidlParser(tokens);
        ParseTree tree = parser.aidl();
        ParseTreeWalker walker = new ParseTreeWalker();
        AidlToJsonListener listener = new AidlToJsonListener();
        walker.walk(listener, tree);
        OutputStream os = null;
        try {
            os = new FileOutputStream(args[1] + listener.getName() + ".json", false);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            os.write(listener.toJson().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}