package com.redhat.ceylon.compiler.typechecker.parser;

import org.antlr.runtime.RecognitionException;

public class ParseError extends RecognitionError {
	
	private CeylonParser parser;
	private int code;
	private int expecting;
	
	public ParseError(CeylonParser parser, RecognitionException re, String[] tn) {
		this(parser, re, tn, -1);
		expecting = parser.expecting;
		parser.expecting=-1;
	}
	
    public ParseError(CeylonParser parser, RecognitionException re, String[] tn, int code) {
        super(re, tn);
        this.parser = parser;
        this.code = code;
    }

	public String getToken() {
		return recognitionException.token.getText();
	}
	
	public String getHeader() {
		return parser.getErrorHeader(recognitionException);
	}
	
    @Override
    public int getCode() {
        return code;
    }
    
	@Override 
	public String getMessage() {
		String result = "incorrect syntax: " + parser.getErrorMessage(recognitionException, tokenNames);
		if (expecting!=-1) {
			result += " expecting " + tokenNames[expecting];
		}
		return result;
	}
	
	@Override
	public String toString() {
		return getMessage();
	}
	
}

@@ -896,16 +896,21 @@
 ax: %22 + 
+%0A%09%09%09%09
 parser.g
@@ -961,29 +961,136 @@
 mes)
-;%0A%09%09if (expecting!=-1
+%0A%09%09%09%09%09%09.replace(%22'%3CEOF%3E'%22, %22end of file%22)%0A%09%09%09%09%09%09.replace(%22input%22, %22token%22);%0A%09%09if (expecting!=-1 && !result.contains(%22expecting%22)
 ) %7B%0A

improve parser message readability