git.dumitru.net fructose / master src / parser / ParserError.ts
master

Tree @master (Download .tar.gz)

ParserError.ts @masterraw · history · blame

import DocumentLocation from "../DocumentLocation";

export default class ParserError {
  public readonly reason: string;
  public readonly location: DocumentLocation;
  public readonly parentError: ParserError | null;

  constructor(reason: string, location: DocumentLocation, parentError: ParserError | null) {
    this.reason = reason;
    this.location = location;
    this.parentError = parentError;
  }

  public toString(): string {
    function prettyPrintErrorStack(e: ParserError | null): string {
      if (e === null) {
        return "";
      }

      if (e.parentError === null) {
        return `at ${e.location}.`;
      }

      return `at ${e.location},\n` + prettyPrintErrorStack(e.parentError);
    }

    if (this.parentError === null) {
      return `error: ${this.reason}, at ${this.location}, at top level.`;
    }

    return `error: ${this.reason}, at ${this.location}, and previously in:\n` +
      prettyPrintErrorStack(this.parentError);
  }
}