package org.spoofax.jsglr2.parser;

import org.metaborg.parsetable.characterclasses.CharacterClassFactory;
import org.spoofax.jsglr2.parser.result.ParseFailureCause;

public class ParseException extends Exception {

    private static final long serialVersionUID = 5070826083429554841L;

    private final ParseFailureCause.Type failureType;
    private final Position position;

    public ParseException(ParseFailureCause.Type failureType, Position position, Integer character) {
        super(message(failureType, position, character));

        this.failureType = failureType;
        this.position = position;
    }

    public final ParseFailureCause cause() {
        return new ParseFailureCause(failureType, position);
    }

    private static String message(ParseFailureCause.Type failureType, Position position, Integer character) {
        StringBuilder message = new StringBuilder();

        message.append(failureType.message);

        if(position != null)
            message.append(
                " at offset " + position.offset + "(line " + position.line + ", column " + position.column + ")");

        if(character != null)
            message.append(" at character " + CharacterClassFactory.intToString(character));

        return message.toString();
    }

}

@@ -1046,16 +1046,17 @@
 fset + %22
+ 
 (line %22 
@@ -1169,16 +1169,47 @@
 .append(
+%0A                String.format(
 %22 at cha
@@ -1215,19 +1215,29 @@
 aracter 
-%22 +
+%25s (U+%2504X)%22,
  Charact
@@ -1263,16 +1263,28 @@
 oString(
+character), 
 characte

print Unicode value of character where parsing failed