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();
	}
	
}

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)
						.replace("'<EOF>'", "end of file")
						.replace("input", "token");
		if (expecting!=-1 && !result.contains("expecting")) {
			result += " expecting " + tokenNames[expecting];
		}
		return result;
	}
	
	@Override
	public String toString() {
		return getMessage();
	}
	
}

improve parser message readability