/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */

package cx2x.xcodeml.exception;

/**
 * Exception thrown during the analysis of a directive.
 *
 * @author clementval
 */
public class IllegalDirectiveException extends Exception {

  private int _directiveLine = 0;
  private int _charPos = 0;
  private String _directive;

  /**
   * Constructs a new exception with a specific detail message and clause.
   *
   * @param directive Illegal directive
   * @param message   Specific exception message.
   */
  public IllegalDirectiveException(String directive, String message) {
    super(message);
    _directive = directive;
  }

  /**
   * Constructs a new exception with a specific detail message and line number.
   *
   * @param directive Illegal directive
   * @param message   Specific exception message.
   * @param lineno    Line number of the directive.
   */
  public IllegalDirectiveException(String directive, String message, int lineno)
  {
    super(message);
    _directive = directive;
    _directiveLine = lineno;
  }

  /**
   * Constructs a new exception with a specific detail message, line number and
   * char position.
   *
   * @param directive Illegal directive
   * @param message   Specific exception message.
   * @param lineno    Line number of the directive.
   * @param charPos   Character position where the directive error happened.
   */
  public IllegalDirectiveException(String directive, String message, int lineno,
                                   int charPos)
  {
    super(message);
    _directive = directive;
    _directiveLine = lineno;
    _charPos = charPos;
  }

  /**
   * Get the illegal directive.
   *
   * @return The illegal directive.
   */
  public String getDirective() {
    return _directive;
  }

  /**
   * Set the illegal directive.
   *
   * @param directive The directive.
   */
  public void setDirective(String directive) {
    _directive = directive;
  }

  /**
   * Get the line of the code that contains the illegal directive.
   *
   * @return Line number in the XcodeML that triggered the exception.
   */
  public int getDirectiveLine() {
    return _directiveLine;
  }

  /**
   * Set the line of the code that contains the illegal directive.
   *
   * @param lineno Line number in the XcodeML that triggered the exception.
   */
  public void setDirectiveLine(int lineno) {
    _directiveLine = lineno;
  }

  /**
   * Get the character position where the directive error happened.
   *
   * @return Character position in the line.
   */
  public int getCharPosition() {
    return _charPos;
  }

  @Override
  public String getMessage() {
    String errorMessage = "Illegal directive ";

    if(_directiveLine > 0) {
      errorMessage += _directiveLine + ":" + _charPos;
    } else {
      errorMessage += "-:" + _charPos;
    }
    errorMessage += ": " + super.getMessage();
    return errorMessage;
  }
}

@@ -127,16 +127,52 @@
 ption;%0A%0A
+import org.antlr.v4.runtime.Token;%0A%0A
 /**%0A * E
@@ -402,16 +402,56 @@
 rective;
+%0A  private Token _offendingToken = null;
 %0A%0A  /**%0A
@@ -742,32 +742,414 @@
 directive;%0A  %7D%0A%0A
+  /**%0A   * Constructs a new exception with an offending token and a list of expecting%0A   * ones.%0A   *%0A   * @param offendingToken Token that break the parsing.%0A   */%0A  public IllegalDirectiveException(Token offendingToken, int lineno,%0A                                   int charPos)%0A  %7B%0A    _offendingToken = offendingToken;%0A    _directiveLine = lineno;%0A    _charPos = charPos;%0A  %7D%0A%0A
   /**%0A   * Const
@@ -3112,24 +3112,918 @@
 Message() %7B%0A
+    return (_offendingToken != null) ? getExpectingTokenMsg() : getStdMsg();%0A  %7D%0A%0A  /**%0A   * Get the standard error message if no offending token is provided.%0A   *%0A   * @return Standard error message.%0A   */%0A  private String getStdMsg() %7B%0A    String errorMessage = getMsgPrefix();%0A    errorMessage += %22: %22 + super.getMessage();%0A    return errorMessage;%0A  %7D%0A%0A  /**%0A   * Get a specific error message when an offending token is provided.%0A   *%0A   * @return Specific error message including offending and expecting tokens.%0A   */%0A  private String getExpectingTokenMsg() %7B%0A    String errorMessage = getMsgPrefix();%0A    errorMessage += %22: Expected X but found %5C%22%22 +%0A        _offendingToken.getText() + %22%5C%22%22;%0A    return errorMessage;%0A  %7D%0A%0A  /**%0A   * Get the standard prefix for the illegal directive error message.%0A   *%0A   * @return Standard error message prefix.%0A   */%0A  private String getMsgPrefix() %7B%0A
     String e
@@ -4058,17 +4058,16 @@
 tive %22;%0A
-%0A
     if(_
@@ -4204,55 +4204,8 @@
   %7D%0A
-    errorMessage += %22: %22 + super.getMessage();%0A
     

Add contructor and formatted message for offending token errors