// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.exception;

import bdc.scheme.SchemeException;
import bdc.scheme.Symbol;
import bdc.scheme.Writer;
import bdc.scheme.expression.Expression;
import bdc.scheme.expression.Procedure;
import bdc.util.Fmt;

public class ArgumentTypeException extends SchemeException
{
    String location;
    Object object;
    String expected;
    String received;
    Object source;

    public ArgumentTypeException (Procedure location,
                                  String    expected,
                                  Object    received)
    {
        this(location.toString(), expected, received, location.toString());
    }

    public ArgumentTypeException (Expression location,
                                  String     expected,
                                  Object     received)
    {
        this(location.toString(), expected, received, location.source);
    }

    public ArgumentTypeException (Symbol location,
                                  String expected,
                                  Object received,
                                  Object source)
    {
        this(location.toString(), expected, received, source);
    }

    public ArgumentTypeException (String location,
                                  String expected,
                                  Object received,
                                  Object source)
    {
        this.location = location;
        this.expected = expected;
        this.object   = received;
        this.source   = source;
        if (received instanceof char[]) {
            this.received = "char[]";
        }
        else if (received instanceof Object[]) {
            this.received = "Object[]";
        }
        else {
            this.received = received.getClass().getName();
        }
    }

    public String toString ()
    {
        return Fmt.S("Expected %s: Received %s(%s) in %s\n%s",
                     expected, Writer.write(object), received, location,
                     Expression.stackTrace(source));
    }
}
