001/*
002 * Units of Measurement Systems for Java
003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
008 *
009 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
010 *
011 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
012 *
013 * 3. Neither the name of JSR-363, Units of Measurement nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
014 *
015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
016 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
017 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
018 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
019 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
020 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
021 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
022 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
023 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
024 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026package systems.uom.ucum.internal.format;
027
028import javax.measure.MeasurementException;
029
030/**
031 * This exception is thrown when token errors are encountered.
032 * You can explicitly create objects of this exception type by
033 * calling the method raiseTokenException in the generated
034 * parser.
035 *
036 * You can modify this class to customize your error reporting
037 * mechanisms so long as you retain the public fields.
038 * 
039 * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
040 * @author  <a href="mailto:units@catmedia.us">Werner Keil</a>
041 * @version 0.5.5, Mar 15, 2017
042 */
043public class TokenException extends MeasurementException {
044//TODO try to merge this with ParserException
045        /**
046         * The Serialization identifier for this class.
047     * Increment only if the <i>serialized</i> form of the
048         * class changes.
049         */
050        private static final long serialVersionUID = 2932151235799168061L;
051
052  /**
053   * This constructor is used by the method "raiseTokenException"
054   * in the generated parser.  Calling this constructor generates
055   * a new object of this type with the fields "currentToken",
056   * "expectedTokenSequences", and "tokenImage" set.
057   */
058  public TokenException(Token currentTokenVal,
059                        int[][] expectedTokenSequencesVal,
060                        String[] tokenImageVal
061                       )
062  {
063    super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
064    currentToken = currentTokenVal;
065    expectedTokenSequences = expectedTokenSequencesVal;
066    tokenImage = tokenImageVal;
067  }
068
069  /**
070   * The following constructors are for use by you for whatever
071   * purpose you can think of.  Constructing the exception in this
072   * manner makes the exception behave in the normal way - i.e., as
073   * documented in the class "Throwable".  The fields "errorToken",
074   * "expectedTokenSequences", and "tokenImage" do not contain
075   * relevant information.  The JavaCC generated code does not use
076   * these constructors.
077   */
078
079  public TokenException() {
080    super();
081  }
082
083  /** Constructor with message. */
084  public TokenException(String message) {
085    super(message);
086  }
087
088
089  /**
090   * This is the last token that has been consumed successfully.  If
091   * this object has been created due to a parse error, the token
092   * followng this token will (therefore) be the first error token.
093   */
094  public Token currentToken;
095
096  /**
097   * Each entry in this array is an array of integers.  Each array
098   * of integers represents a sequence of tokens (by their ordinal
099   * values) that is expected at this point of the parse.
100   */
101  public int[][] expectedTokenSequences;
102
103  /**
104   * This is a reference to the "tokenImage" array of the generated
105   * parser within which the parse error occurred.  This array is
106   * defined in the generated ...Constants interface.
107   */
108  public String[] tokenImage;
109
110  /**
111   * It uses "currentToken" and "expectedTokenSequences" to generate a parse
112   * error message and returns it.  If this object has been created
113   * due to a parse error, and you do not catch it (it gets thrown
114   * from the parser) the correct error message
115   * gets displayed.
116   */
117  private static String initialise(Token currentToken,
118                           int[][] expectedTokenSequences,
119                           String[] tokenImage) {
120    String eol = System.getProperty("line.separator", "\n");
121    StringBuilder expected = new StringBuilder();
122    int maxSize = 0;
123      for (int[] expectedTokenSequence : expectedTokenSequences) {
124          if (maxSize < expectedTokenSequence.length) {
125              maxSize = expectedTokenSequence.length;
126          }
127          for (int anExpectedTokenSequence : expectedTokenSequence) {
128              expected.append(tokenImage[anExpectedTokenSequence]).append(' ');
129          }
130          if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) {
131              expected.append("...");
132          }
133          expected.append(eol).append("    ");
134      }
135    String retval = "Encountered \"";
136    Token tok = currentToken.next;
137    for (int i = 0; i < maxSize; i++) {
138      if (i != 0) retval += " ";
139      if (tok.kind == 0) {
140        retval += tokenImage[0];
141        break;
142      }
143      retval += " " + tokenImage[tok.kind];
144      retval += " \"";
145      retval += add_escapes(tok.image);
146      retval += " \"";
147      tok = tok.next;
148    }
149    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
150    retval += "." + eol;
151    if (expectedTokenSequences.length == 1) {
152      retval += "Was expecting:" + eol + "    ";
153    } else {
154      retval += "Was expecting one of:" + eol + "    ";
155    }
156    retval += expected.toString();
157    return retval;
158  }
159
160  /**
161   * The end of line string for this machine.
162   */
163  protected String eol = System.getProperty("line.separator", "\n");
164
165  /**
166   * Used to convert raw characters to their escaped version
167   * when these raw version cannot be used as part of an ASCII
168   * string literal.
169   */
170  static String add_escapes(String str) {
171      StringBuilder retval = new StringBuilder();
172      char ch;
173      for (int i = 0; i < str.length(); i++) {
174        switch (str.charAt(i))
175        {
176           case 0 :
177              continue;
178           case '\b':
179              retval.append("\\b");
180              continue;
181           case '\t':
182              retval.append("\\t");
183              continue;
184           case '\n':
185              retval.append("\\n");
186              continue;
187           case '\f':
188              retval.append("\\f");
189              continue;
190           case '\r':
191              retval.append("\\r");
192              continue;
193           case '\"':
194              retval.append("\\\"");
195              continue;
196           case '\'':
197              retval.append("\\\'");
198              continue;
199           case '\\':
200              retval.append("\\\\");
201              continue;
202           default:
203              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
204                 String s = "0000" + Integer.toString(ch, 16);
205                 retval.append("\\u").append(s.substring(s.length() - 4, s.length()));
206              } else {
207                 retval.append(ch);
208              }
209        }
210      }
211      return retval.toString();
212   }
213
214}
215/* JavaCC - OriginalChecksum=c67b0f8ee6c642900399352b33f90efd (do not edit this line) */