#include <stdio.h>
#include <stdlib.h>

#include "myname.h"
#include "token.h"
#include "fatal_error.h"


/*
 * These functions all print error messages and then terminate with
 * the EXIT_FAILURE code
 */

/*
 * prints the line number and newline
 */
void fatal_error_lineno (token_t token) {
  fprintf(stderr, " at line %lli\n", token.lineno);
}


/*
 * indicates a syntax error
 */
void fatal_error_syntax_lineno (token_t token) {
  fprintf(stderr, " at line %lli\n", token.lineno);
}


/*
 * a keyword was expected but something else was found
 */
void fatal_error_expected_keyword (token_t token) {
  fprintf(stderr, "%s: error: expected keyword, found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a number/identifier was found but a keyword was found
 */
void fatal_error_expected_value_keyword (token_t token) {
  fprintf(stderr,
	  "%s: error: expected identifier or number, found keyword '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a number/identifier was expected but something else was found
 */
void fatal_error_expected_value (token_t token) {
  fprintf(stderr, "%s: error: expected number or identifier, found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * an identifier was expected but something else was found
 */
void fatal_error_expected_identifier (token_t token) {
  fprintf(stderr, "%s: error: expected identifier, found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * an identifier was expected but a keyword was found
 */
void fatal_error_expected_identifier_keyword (token_t token) {
  fprintf(stderr, "%s: error: expected identifier, found keyword '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a number/identifier was expected but something else was found
 */
void fatal_error_expected_printable (token_t token) {
  fprintf(stderr, "%s: error: expected value or string, found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a string was expected but something else was found
 */
void fatal_error_expected_string (token_t token) {
  fprintf(stderr, "%s: error: expected string, found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a '[' was expected but something else was found
 */
void fatal_error_expected_openbrace (token_t token) {
  fprintf(stderr, "%s: error: expected '[', found '%s'",
	  myname, token.value);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a '[' was found where it shouldn't be
 */
void fatal_error_unexpected_openbrace (token_t token) {
  fprintf(stderr, "%s: error: unexpected '['", myname);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * a ']' was found where it shouldn't be
 */
void fatal_error_unexpected_closebrace (token_t token) {
  fprintf(stderr, "%s: error: unexpected ']'", myname);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * the input file ended in a syntactically unacceptable place
 */
void fatal_error_unexpected_eof (token_t token) {
  fprintf(stderr, "%s: error: unexpected EOF", myname);
  fatal_error_syntax_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * we are sure the variable cannot possibly be defined at this point
 */
void fatal_error_notdefined (token_t token) {
  fprintf(stderr, "%s: error: variable '%s' is not defined",
	  myname, token.value);
  fatal_error_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * an illegal character was encountered outside of a comment or string
 */
void fatal_error_illegalchar (token_t token) {
  fprintf(stderr, "%s: error: illegal character", myname);
  fatal_error_lineno(token);

  exit(EXIT_FAILURE);
}


/*
 * the given file is a directory
 */
void fatal_directory () {
  fprintf(stderr, "%s: error: the specified file is a directory\n", myname);

  exit(EXIT_FAILURE);
}

