package com.icona.console.main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import com.icona.antlr.main.ObjectiveCLexer;
import com.icona.antlr.main.ObjectiveCParser;
import com.icona.gui.ConverterApp;
import com.icona.tree.nodes.SourceFile;
public class ConsoleMain {
/**
* @param args
* @throws IOException
*/
public class ConversionObjects{
CharStream input;
ObjectiveCLexer lex;
CommonTokenStream tokens;
ObjectiveCParser parser;
String filename;
public void setFileName(String filename){
this.filename = filename;
}
public void setInputStream(String sourceFile) {
try {
input = new ANTLRFileStream(sourceFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doConversion() {
lex= new ObjectiveCLexer(input);
tokens = new CommonTokenStream(lex);
parser = new ObjectiveCParser(tokens);
try {
parser.translation_unit();
} catch (RecognitionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ConversionObjects() {
input = null;
lex = new ObjectiveCLexer();
tokens = new CommonTokenStream();
parser = new ObjectiveCParser(null);
}
public void writeToFile(String destinationFile) {
FileWriter fw = null;
try {
fw = new FileWriter(destinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter Bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(Bw);
SourceFile src = parser.getSrc();
System.out.println(src.toString());
pw.print("public class " + filename + "{\n");
pw.print(src.toString());
pw.print("public static void main(String[] args){\n");
pw.print("\tSystem.out.println(\"The program compiles\");\n");
pw.print("}\n");
pw.print("}");
try {
Bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.close();
}
}
public static void main(String[] args) throws IOException {
// com.icona.gui.ConverterApp.launch(ConverterApp.class, args); //Launch GUI - have it commented if you don't need conversion via gui
ConsoleMain cm = new ConsoleMain();
String filename="input";
ConsoleMain.ConversionObjects cb = cm.new ConversionObjects(); //Objects that do conversion(Lexer, parser etc) have been placed into one fancy class
cb.setFileName(filename); //set filename for name of the class in code
cb.setInputStream("test_input/"+filename+".txt"); //pass the absolute path with extension
cb.doConversion(); // conversion goes in this method
String destinationFile = "test_input/output/"+filename+".java"; //absolute path of destination file
cb.writeToFile(destinationFile); //have the parser write the source to file
/***the old goodies***/
/*CharStream input = new ANTLRFileStream("test_input/"+filename+".c");
ObjectiveCLexer lex= new ObjectiveCLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
ObjectiveCParser parser = new ObjectiveCParser(tokens);
try {
parser.translation_unit();
} catch (RecognitionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileWriter fw=new FileWriter("test_input/output/"+filename+".java",false);
BufferedWriter Bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(Bw);
SourceFile src=parser.getSrc();
System.out.println(src.toString());
pw.print("public class "+filename+"{\n");
pw.print(src.toString());
pw.print("public static void main(String[] args){\n");
pw.print("\tSystem.out.println(\"The program compiles\");\n");
pw.print("}\n");
pw.print("}");
Bw.close();
fw.close();
pw.close();
//########################
/*String cmd = "javac test_input/output/"+filename+".java";
System.out.println(cmd);
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
try {
pr.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
//#######################
* */
}
}