package edu.kit.iti.formal.pse.worthwhile.common.tests;

import java.io.StringReader;
import java.util.List;

import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;

import com.google.inject.Injector;

import edu.kit.iti.formal.pse.worthwhile.WorthwhileStandaloneSetup;
import edu.kit.iti.formal.pse.worthwhile.model.ast.ASTNode;
import edu.kit.iti.formal.pse.worthwhile.model.ast.Block;
import edu.kit.iti.formal.pse.worthwhile.model.ast.Expression;
import edu.kit.iti.formal.pse.worthwhile.model.ast.Program;
import edu.kit.iti.formal.pse.worthwhile.model.ast.Statement;
import edu.kit.iti.formal.pse.worthwhile.model.ast.VariableDeclaration;

public class TestASTProvider {

	public static Program getRootASTNode(String parseText) {
		// from http://wiki.pse.ndreke.de/proof_of_concept_ast_output
		Injector guiceInjector = new WorthwhileStandaloneSetup()
		.createInjectorAndDoEMFRegistration();
		IParser parser = guiceInjector.getInstance(IParser.class);

		IParseResult result = parser.parse(new StringReader(parseText));

		// System.out.println("Getting syntax errors ...");
		// Iterable<INode> errors = result.getSyntaxErrors();
		Program root = null;
		
		if (!result.hasSyntaxErrors()) {
		    root = (Program) result.getRootASTElement();
		}
		
		return root;
	}

	/**
	 * @return A simple, human-readable formula that evaluates to true (i.e. is
	 *         always satisfiable) and covers a lot of the constructs supported
	 *         by the language
	 */
	public static Expression getTestFormula() {
		return parseFormulaString("((!false || ((9 % 4) = 1)) && true && ((3 + 3) = 6)");
	}

	public static Expression getNegatedTestFormula() {
		return parseFormulaString("!((!false || ((9 % 4) = 1)) && true && ((3 + 3) = 6)");
	}

	public static Expression parseFormulaString(String formulaString) {
		ASTNode n = TestASTProvider.getRootASTNode("{\nBoolean x := " +
				formulaString + "\n" +
				"}\n");

		if (n == null) {
		    return null;
		}

		List<Statement> statements = ((Block) ((Program) n).getMainBlock()
				.getStatements().get(0)).getStatements();

		return ((VariableDeclaration) (statements.get(0))).getInitialValue();
	}
}

@@ -703,17 +703,20 @@
 ider %7B%0A%0A
-%09
+    
 public s
@@ -764,17 +764,16 @@
 Text) %7B%0A
-%09
 %09// from
@@ -827,17 +827,16 @@
 _output%0A
-%09
 %09Injecto
@@ -884,19 +884,16 @@
 eSetup()
-%0A%09%09
 .createI
@@ -923,17 +923,16 @@
 tion();%0A
-%09
 %09IParser
@@ -984,17 +984,16 @@
 lass);%0A%0A
-%09
 %09IParseR
@@ -1051,17 +1051,16 @@
 ext));%0A%0A
-%09
 %09// Syst
@@ -1104,17 +1104,16 @@
  ...%22);%0A
-%09
 %09// Iter
@@ -1159,17 +1159,16 @@
 rors();%0A
-%09
 %09Program
@@ -1181,20 +1181,17 @@
 = null;%0A
-%09%09%0A%09
+%0A
 %09if (!re
@@ -1212,25 +1212,24 @@
 Errors()) %7B%0A
-%09
 %09    root = 
@@ -1270,16 +1270,12 @@
 ();%0A
-%09
 %09%7D%0A
-%09%09%0A%09
+%0A
 %09ret
@@ -1288,18 +1288,27 @@
 ot;%0A
-%09%7D%0A%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * @
@@ -1378,28 +1378,16 @@
 (i.e. is
-%0A%09 *        
  always 
@@ -1418,16 +1418,31 @@
 a lot of
+%0A     *        
  the con
@@ -1462,20 +1462,8 @@
 rted
-%0A%09 *        
  by 
@@ -1479,14 +1479,20 @@
 age%0A
-%09 */%0A%09
+     */%0A    
 publ
@@ -1523,33 +1523,32 @@
 TestFormula() %7B%0A
-%09
 %09return parseFor
@@ -1606,37 +1606,43 @@
 (3 + 3) = 6)%22);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public static Ex
@@ -1676,17 +1676,16 @@
 ula() %7B%0A
-%09
 %09return 
@@ -1760,21 +1760,27 @@
 = 6)%22);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public s
@@ -1839,17 +1839,16 @@
 ring) %7B%0A
-%09
 %09ASTNode
@@ -1903,21 +1903,17 @@
 x := %22 +
-%0A%09%09%09%09
+ 
 formulaS
@@ -1926,21 +1926,17 @@
 + %22%5Cn%22 +
-%0A%09%09%09%09
+ 
 %22%7D%5Cn%22);%0A
@@ -1936,17 +1936,16 @@
 %7D%5Cn%22);%0A%0A
-%09
 %09if (n =
@@ -1954,17 +1954,16 @@
 null) %7B%0A
-%09
 %09    ret
@@ -1976,14 +1976,12 @@
 ll;%0A
-%09
 %09%7D%0A%0A
-%09
 %09Lis
@@ -2047,13 +2047,8 @@
 ck()
-%0A%09%09%09%09
 .get
@@ -2086,17 +2086,16 @@
 nts();%0A%0A
-%09
 %09return 
@@ -2157,13 +2157,16 @@
 alue();%0A
-%09
+    
 %7D%0A%7D%0A

Apply Worthwhile formatter (whitespace only)