package edu.kit.iti.formal.pse.worthwhile.validation;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.validation.Check;

import com.google.inject.Inject;

import de.itemis.xtext.typesystem.ITypesystem;
import edu.kit.iti.formal.pse.worthwhile.model.ast.FunctionDeclaration;


public class WorthwhileJavaValidator extends AbstractWorthwhileJavaValidator {

//	@Check
//	public void checkGreetingStartsWithCapital(Greeting greeting) {
//		if (!Character.isUpperCase(greeting.getName().charAt(0))) {
//			warning("Name should start with a capital", MyDslPackage.Literals.GREETING__NAME);
//		}
//	}
	
	
	@Check
	public void checkFunctionDeclerationValidReturnStatment(FunctionDeclaration functionDeclaration) {
		ValidatorASTNodeVisitor validatorASTNodeVisitor = new ValidatorASTNodeVisitor();
		validatorASTNodeVisitor.visit(functionDeclaration);
		if(!validatorASTNodeVisitor.getValidReturnFound()) {
			error("Function has no valid return statement.", null);
		}
	}
	@Inject
	private ITypesystem ts;
	
	@Check
	public void checkTypesystemRules( EObject x ) {
	  ts.checkTypesystemConstraints(x, this);
	}


}

@@ -52,46 +52,8 @@
 n;%0A%0A
-import org.eclipse.emf.ecore.EObject;%0A
 impo
@@ -173,16 +173,76 @@
 system;%0A
+import edu.kit.iti.formal.pse.worthwhile.model.ast.ASTNode;%0A
 import e
@@ -306,16 +306,84 @@
 ation;%0A%0A
+/**%0A * Performs semantic validation on a Worthwhile program.%0A * %0A */
 %0Apublic 
@@ -458,252 +458,267 @@
 r %7B%0A
-%0A//%09@Check%0A//%09public void checkGreetingStartsWithCapital(Greeting greeting) %7B%0A//%09%09if (!Character.isUpperCase(greeting.getName().charAt(0))) %7B%0A//%09%09%09warning(%22Name should start with a capital%22, MyDslPackage.Literals.GREETING__NAME);%0A//%09%09%7D%0A//%09%7D%0A%09%0A%09
+%09%0A%09/**%0A%09 * The language typesystem.%0A%09 */%0A%09@Inject%0A%09private ITypesystem ts;%0A%0A%09/**%0A%09 * Checks if a function declaration contains a return statement on all code paths.%0A%09 * %0A%09 * @param functionDeclaration%0A%09 *                The function declaration to check.%0A%09 */
 %0A%09@C
@@ -722,32 +722,38 @@
 %09@Check%0A%09public 
+final 
 void checkFuncti
@@ -785,16 +785,22 @@
 tatment(
+final 
 Function
@@ -975,16 +975,17 @@
 n);%0A%09%09if
+ 
 (!valida
@@ -1097,43 +1097,148 @@
 %0A%09%7D%0A
-%09@Inject%0A%09private ITypesystem ts;%0A%09
+%0A%09/**%0A%09 * Checks if the given AST node conforms to the typesystem rules.%0A%09 * %0A%09 * @param node%0A%09 *                The AST node to check.%0A%09 */
 %0A%09@C
@@ -1250,16 +1250,22 @@
 %09public 
+final 
 void che
@@ -1286,26 +1286,32 @@
 les(
- EObject x 
+final ASTNode node
 ) %7B%0A%09
-  
+%09
 ts.c
@@ -1336,17 +1336,20 @@
 traints(
-x
+node
 , this);
@@ -1353,11 +1353,10 @@
 s);%0A%09%7D%0A%0A
-%0A
 %7D%0A

Call typesystem from validator