source: branches/1.1.x/src/org/armedbear/lisp/WrongNumberOfArgumentsException.java

Last change on this file was 13885, checked in by ehuelsmann, 13 years ago

Report the actual parameters passed in a function call with mismatched
parameter counts.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 * WrongNumberOfArgumentsException.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: WrongNumberOfArgumentsException.java 13885 2012-02-29 22:35:41Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public final class WrongNumberOfArgumentsException extends ProgramError
39{
40    private Operator operator;
41    private int expectedMinArgs;
42    private int expectedMaxArgs;
43    private LispObject actualArgs;
44    private String message;
45
46    public WrongNumberOfArgumentsException(Operator operator) {
47        this(operator, -1);
48    }
49
50    public WrongNumberOfArgumentsException(Operator operator, LispObject args,
51            int expectedMin, int expectedMax) {
52        // This is really just an ordinary PROGRAM-ERROR, broken out into its
53        // own Java class as a convenience for the implementation.
54        super(StandardClass.PROGRAM_ERROR);
55        this.operator = operator;
56  this.expectedMinArgs = expectedMin;
57  this.expectedMaxArgs = expectedMax;
58        this.actualArgs = args;
59        setFormatControl(getMessage());
60        setFormatArguments(NIL);
61    }
62
63    public WrongNumberOfArgumentsException(Operator operator,
64            int expectedMin, int expectedMax) {
65        this(operator, null, expectedMin, expectedMax);
66    }
67       
68    public WrongNumberOfArgumentsException(Operator operator, int expectedArgs) {
69        this(operator, expectedArgs, expectedArgs);
70    }
71
72    public WrongNumberOfArgumentsException(Operator operator, LispObject args,
73            int expectedArgs) {
74        this(operator, args, expectedArgs, expectedArgs);
75    }
76   
77    public WrongNumberOfArgumentsException(String message) {
78        super(StandardClass.PROGRAM_ERROR);
79  if(message == null) {
80      throw new NullPointerException("message can not be null");
81  }
82  this.message = message;
83        setFormatControl(getMessage());
84        setFormatArguments(NIL);
85    }
86
87    @Override
88    public String getMessage()
89    {
90  if(message != null) {
91      return message;
92  }
93        StringBuilder sb =
94            new StringBuilder("Wrong number of arguments for "
95                              + operator.princToString());
96  if(expectedMinArgs >= 0 || expectedMaxArgs >= 0) {
97      sb.append("; ");
98           
99            if (expectedMinArgs == expectedMaxArgs) {
100                sb.append(expectedMinArgs);
101            } else if (expectedMaxArgs < 0) {
102                sb.append("at least ");
103                sb.append(expectedMinArgs);
104            } else if (expectedMinArgs < 0) {
105                sb.append("at most ");
106                sb.append(expectedMaxArgs);
107            } else {
108                sb.append("between ").append(expectedMinArgs);
109                sb.append(" and ").append(expectedMaxArgs);
110            }
111           
112      sb.append(" expected");
113  }
114        if (actualArgs != null) {
115            sb.append(" -- provided: ");
116            sb.append(actualArgs.princToString());
117        }
118        sb.append('.');
119        return message = sb.toString();
120    }
121}
Note: See TracBrowser for help on using the repository browser.