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