001/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
002/* JavaCCOptions:KEEP_LINE_COL=null */
003/* Parser for matrices written in matlab format.
004
005 Copyright (c) 1998-2008 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026                                        PT_COPYRIGHT_VERSION_2
027                                        COPYRIGHTENDKEY
028 */
029package ptolemy.data.expr;
030
031/**
032 * This exception is thrown when parse errors are encountered.
033 * You can explicitly create objects of this exception type by
034 * calling the method generateParseException in the generated
035 * parser.
036 *
037 * You can modify this class to customize your error reporting
038 * mechanisms so long as you retain the public fields.
039 */
040public class ParseException extends Exception {
041
042    /**
043     * The version identifier for this Serializable class.
044     * Increment only if the <i>serialized</i> form of the
045     * class changes.
046     */
047    private static final long serialVersionUID = 1L;
048
049    /**
050     * This constructor is used by the method "generateParseException"
051     * in the generated parser.  Calling this constructor generates
052     * a new object of this type with the fields "currentToken",
053     * "expectedTokenSequences", and "tokenImage" set.
054     */
055    public ParseException(Token currentTokenVal,
056            int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
057        super(initialise(currentTokenVal, expectedTokenSequencesVal,
058                tokenImageVal));
059        currentToken = currentTokenVal;
060        expectedTokenSequences = expectedTokenSequencesVal;
061        tokenImage = tokenImageVal;
062    }
063
064    /**
065     * The following constructors are for use by you for whatever
066     * purpose you can think of.  Constructing the exception in this
067     * manner makes the exception behave in the normal way - i.e., as
068     * documented in the class "Throwable".  The fields "errorToken",
069     * "expectedTokenSequences", and "tokenImage" do not contain
070     * relevant information.  The JavaCC generated code does not use
071     * these constructors.
072     */
073
074    public ParseException() {
075        super();
076    }
077
078    /** Constructor with message. */
079    public ParseException(String message) {
080        super(message);
081    }
082
083    /**
084     * This is the last token that has been consumed successfully.  If
085     * this object has been created due to a parse error, the token
086     * followng this token will (therefore) be the first error token.
087     */
088    public Token currentToken;
089
090    /**
091     * Each entry in this array is an array of integers.  Each array
092     * of integers represents a sequence of tokens (by their ordinal
093     * values) that is expected at this point of the parse.
094     */
095    public int[][] expectedTokenSequences;
096
097    /**
098     * This is a reference to the "tokenImage" array of the generated
099     * parser within which the parse error occurred.  This array is
100     * defined in the generated ...Constants interface.
101     */
102    public String[] tokenImage;
103
104    /**
105     * It uses "currentToken" and "expectedTokenSequences" to generate a parse
106     * error message and returns it.  If this object has been created
107     * due to a parse error, and you do not catch it (it gets thrown
108     * from the parser) the correct error message
109     * gets displayed.
110     */
111    private static String initialise(Token currentToken,
112            int[][] expectedTokenSequences, String[] tokenImage) {
113        String eol = System.getProperty("line.separator", "\n");
114        StringBuffer expected = new StringBuffer();
115        int maxSize = 0;
116        for (int i = 0; i < expectedTokenSequences.length; i++) {
117            if (maxSize < expectedTokenSequences[i].length) {
118                maxSize = expectedTokenSequences[i].length;
119            }
120            for (int j = 0; j < expectedTokenSequences[i].length; j++) {
121                expected.append(tokenImage[expectedTokenSequences[i][j]])
122                        .append(' ');
123            }
124            if (expectedTokenSequences[i][expectedTokenSequences[i].length
125                    - 1] != 0) {
126                expected.append("...");
127            }
128            expected.append(eol).append("    ");
129        }
130        String retval = "Encountered \"";
131        Token tok = currentToken.next;
132        for (int i = 0; i < maxSize; i++) {
133            if (i != 0) {
134                retval += " ";
135            }
136            if (tok.kind == 0) {
137                retval += tokenImage[0];
138                break;
139            }
140            retval += " " + tokenImage[tok.kind];
141            retval += " \"";
142            retval += add_escapes(tok.image);
143            retval += " \"";
144            tok = tok.next;
145        }
146        retval += "\" at line " + currentToken.next.beginLine + ", column "
147                + currentToken.next.beginColumn;
148        retval += "." + eol;
149        if (expectedTokenSequences.length == 1) {
150            retval += "Was expecting:" + eol + "    ";
151        } else {
152            retval += "Was expecting one of:" + eol + "    ";
153        }
154        retval += expected.toString();
155        return retval;
156    }
157
158    /**
159     * The end of line string for this machine.
160     */
161    protected String eol = System.getProperty("line.separator", "\n");
162
163    /**
164     * Used to convert raw characters to their escaped version
165     * when these raw version cannot be used as part of an ASCII
166     * string literal.
167     */
168    static String add_escapes(String str) {
169        StringBuffer retval = new StringBuffer();
170        char ch;
171        for (int i = 0; i < str.length(); i++) {
172            switch (str.charAt(i)) {
173            case 0:
174                continue;
175            case '\b':
176                retval.append("\\b");
177                continue;
178            case '\t':
179                retval.append("\\t");
180                continue;
181            case '\n':
182                retval.append("\\n");
183                continue;
184            case '\f':
185                retval.append("\\f");
186                continue;
187            case '\r':
188                retval.append("\\r");
189                continue;
190            case '\"':
191                retval.append("\\\"");
192                continue;
193            case '\'':
194                retval.append("\\\'");
195                continue;
196            case '\\':
197                retval.append("\\\\");
198                continue;
199            default:
200                if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
201                    String s = "0000" + Integer.toString(ch, 16);
202                    retval.append(
203                            "\\u" + s.substring(s.length() - 4, s.length()));
204                } else {
205                    retval.append(ch);
206                }
207                continue;
208            }
209        }
210        return retval.toString();
211    }
212
213}
214/* JavaCC - OriginalChecksum=c62f658e8842e6aff5d3c476c51a0c6f (do not edit this line) */