/**
 *
 */
package ca.uwaterloo.joos;

import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import ca.uwaterloo.joos.parser.LR1;
import ca.uwaterloo.joos.parser.LR1Parser;
import ca.uwaterloo.joos.parser.ParseTree;
import ca.uwaterloo.joos.scanner.DFA;
import ca.uwaterloo.joos.scanner.Scanner;
import ca.uwaterloo.joos.scanner.Token;

/**
 * @author Greg Wang
 *
 */
public class Main {
	private static final Logger logger = Main.getLogger(Main.class);

	public static Logger getLogger(Class<?> cls) {
		return Logger.getLogger(Main.class.getName());
	}
	
	private final Scanner scanner;
	private final LR1Parser parser;
	private final Preprocessor preprocessor;
	
	public Main() {
		logger.setLevel(Level.INFO);
		
		// Construct Preprocessor
		this.preprocessor = new Preprocessor();
		
		// Read a DFA from file
		DFA dfa = null;
		try {
			dfa = new DFA(new File("resources/joos.dfa"));
		} catch (Exception e) {
			System.err.println("ERROR: Invalid DFA File format: " + e.getLocalizedMessage() + " " + e.getClass().getName());
			System.exit(-1);
		}
		// Construct Scanner
		this.scanner = new Scanner(dfa);
		
		// Read a LR1 from file
		LR1 lr1 = null;
		try {
			lr1 = new LR1(new File("resources/joos.lr1"));
		} catch (Exception e) {
			System.err.println("ERROR: Invalid LR1 File format: " + e.getLocalizedMessage() + " " + e.getClass().getName());
			e.printStackTrace();
			System.exit(-2);
		}
		// Construct Parser
		this.parser = new LR1Parser(lr1);
	}
	
	public Object execute(File source) throws Exception {
		logger.info("Processing: " + source.getName());
		
		/* Scanning */
		// Construct a Scanner which use the DFA
		List<Token> tokens = null;

		// Scan the source codes into tokens
		tokens = this.scanner.fileToTokens(source);

		// Preprocess the tokens
		tokens = preprocessor.processTokens(tokens);

		/* Parsing */
		ParseTree parseTree = null;
		parseTree = this.parser.parseTokens(tokens);
		
		return parseTree;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Main instance = new Main();
		
		try {
			instance.execute(new File("resources/testcases/a1/J1_01.java"));
		} catch (Exception e) {
			System.err.println("ERROR: " + e.getLocalizedMessage() + " " + e.getClass().getName());
			e.printStackTrace();
			System.exit(42);
		}
	}
}

@@ -605,18 +605,17 @@
 ());%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -713,18 +713,17 @@
 cessor;%0A
-%09
 %0A
+
 %09public 
@@ -762,19 +762,17 @@
 .INFO);%0A
-%09%09
 %0A
+
 %09%09// Con
@@ -833,19 +833,17 @@
 ssor();%0A
-%09%09
 %0A
+
 %09%09// Rea
@@ -1160,19 +1160,17 @@
 r(dfa);%0A
-%09%09
 %0A
+
 %09%09// Rea
@@ -1514,18 +1514,17 @@
 r1);%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -1620,19 +1620,17 @@
 ame());%0A
-%09%09
 %0A
+
 %09%09/* Sca
@@ -1965,19 +1965,17 @@
 okens);%0A
-%09%09
 %0A
+
 %09%09return
@@ -2092,11 +2092,9 @@
 ();%0A
-%09%09
 %0A
+
 %09%09tr
@@ -2100,16 +2100,35 @@
 ry %7B%0A%09%09%09
+Object parseTree = 
 instance
@@ -2174,18 +2174,112 @@
 a1/J
-1_01.java%22
+e_1_NonJoosConstructs_AssignmentOperations_BitwiseXOR.java%22));%0A%09%09%09System.out.println(parseTree.toString(
 ));%0A

Print parseTree in main