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

import org.eclipse.xtext.nodemodel.SyntaxErrorMessage;
import org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider;

/**
 * This class provides the correct syntax error messages.
 * 
 * @author matthias
 * 
 */
public class WorthwhileSyntaxErrorMessageProvider extends SyntaxErrorMessageProvider {

	/**
	 * Error code for "No newline at end of file".
	 */
	public static final String NO_NEWLINE_AT_EOF = "NoNewlineAtEOF";

	/**
	 * Error code for "No return type specified for function"
	 */
	public static final String NO_FUNCTION_RETURN_TYPE = "NoFunctionReturnType";

	/**
	 * Returns improved syntax error messages. TODO find a better way to get improved messages
	 * 
	 * @param context
	 *                the context from which you want the error message
	 * 
	 * @return the syntax error message
	 */
	@Override
	public final SyntaxErrorMessage getSyntaxErrorMessage(final IParserErrorContext context) {
		System.out.println(context.getDefaultMessage());
		if (context.getDefaultMessage().contains("loop did not match anything at input '<EOF>'")) {

			return new SyntaxErrorMessage("Newline is missing", NO_NEWLINE_AT_EOF);

		} else if (context.getDefaultMessage().contains("loop did not match anything")
		                || context.getDefaultMessage().contains("missing EOF")) {

			return new SyntaxErrorMessage("Delete this token.", "deleteToken");
		}
		return super.getSyntaxErrorMessage(context);

	}

}

@@ -48,16 +48,61 @@
 ation;%0A%0A
+import org.antlr.runtime.EarlyExitException;%0A
 import o
@@ -585,16 +585,17 @@
 unction%22
+.
 %0A%09 */%0A%09p
@@ -688,24 +688,17 @@
 Returns 
-improved
+a
  syntax 
@@ -715,57 +715,25 @@
 ages
-. TODO find a better way to get improved messages
+ for the context.
 %0A%09 *
@@ -978,152 +978,209 @@
 %7B%0A%09%09
-System.out.println(context.getDefaultMessage());%0A%09%09if (context.getDefaultMessage().contains(%22loop did not match anything at input '%3CEOF%3E'%22
+if (context.getRecognitionException() instanceof EarlyExitException) %7B%0A%0A%09%09%09if (context.getCurrentNode().getRootNode().getTotalEndLine() == context.getCurrentNode()%0A%09%09%09                .getEndLine(
 )) %7B%0A
-%0A
+%09
 %09%09%09r
@@ -1254,167 +1254,21 @@
 F);%0A
-%0A
 %09%09
+%09
 %7D else
- if (context.getDefaultMessage().contains(%22loop did not match anything%22)%0A%09%09                %7C%7C context.getDefaultMessage().contains(%22missing EOF%22))
  %7B%0A
-%0A
+%09
 %09%09%09r
@@ -1336,17 +1336,23 @@
 en%22);%0A%09%09
-%7D
+%09%7D%0A%09%09%7D%0A
 %0A%09%09retur

Use exceptions to get the error message.