001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2025, 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 */
030package tech.units.indriya.format;
031
032import javax.measure.MeasurementError;
033
034/** Token Manager Error. 
035 * @version 2.0
036 * @author Werner Keil
037 */
038public class TokenMgrError extends MeasurementError {
039
040  /**
041   * The Serialization identifier for this class. Increment only if the <i>serialized</i> form of the class changes.
042   */
043  private static final long serialVersionUID = -3348968864772188432L;
044
045  /*
046   * Ordinals for various reasons why an Error of this type can be thrown.
047   */
048
049  /**
050   * Lexical error occurred.
051   */
052  public static final int LEXICAL_ERROR = 0;
053
054  /**
055   * An attempt was made to create a second instance of a static token manager.
056   */
057  public static final int STATIC_LEXER_ERROR = 1;
058
059  /**
060   * Tried to change to an invalid lexical state.
061   */
062  public static final int INVALID_LEXICAL_STATE = 2;
063
064  /**
065   * Detected (and bailed out of) an infinite loop in the token manager.
066   */
067  public static final int LOOP_DETECTED = 3;
068
069  /**
070   * Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given string
071   */
072  protected static String addEscapes(String str) {
073    StringBuilder retval = new StringBuilder();
074    char ch;
075    for (int i = 0; i < str.length(); i++) {
076      switch (str.charAt(i)) {
077        case 0:
078          continue;
079        case '\b':
080          retval.append("\\b");
081          continue;
082        case '\t':
083          retval.append("\\t");
084          continue;
085        case '\n':
086          retval.append("\\n");
087          continue;
088        case '\f':
089          retval.append("\\f");
090          continue;
091        case '\r':
092          retval.append("\\r");
093          continue;
094        case '\"':
095          retval.append("\\\"");
096          continue;
097        case '\'':
098          retval.append("\\\'");
099          continue;
100        case '\\':
101          retval.append("\\\\");
102          continue;
103        default:
104          if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
105            String s = "0000" + Integer.toString(ch, 16);
106            retval.append("\\u").append(s.substring(s.length() - 4, s.length()));
107          } else {
108            retval.append(ch);
109          }
110      }
111    }
112    return retval.toString();
113  }
114
115  /**
116   * Returns a detailed message for the Error when it is thrown by the token manager to indicate a lexical error. Parameters : EOFSeen : indicates if
117   * EOF caused the lexical error curLexState : lexical state in which this error occurred errorLine : line number when the error occurred errorColumn
118   * : column number when the error occurred errorAfter : prefix that was seen before this error occurred curchar : the offending character Note: You
119   * can customize the lexical error message by modifying this method.
120   */
121  protected static String lexicalError(boolean EOFSeen, int errorLine, int errorColumn, String errorAfter, char curChar) {
122    return ("Lexical error at line " + errorLine + ", column " + errorColumn + ".  Encountered: "
123        + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int) curChar + "), ") + "after : \""
124        + addEscapes(errorAfter) + "\"");
125  }
126
127  /*
128   * Constructors of various flavors follow.
129   */
130
131  /** No arg constructor. */
132  public TokenMgrError() {
133  }
134  
135  /** Constructor with message. */
136  public TokenMgrError(String message) {
137    super(message);
138  }
139
140  /** Constructor with message and reason. */
141  public TokenMgrError(String message, int reason) {
142    super(message);
143    @SuppressWarnings("unused")
144        int errorCode = reason; // TODO use?
145  }
146
147  /** Full Constructor. */
148  public TokenMgrError(boolean EOFSeen, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
149    this(lexicalError(EOFSeen, errorLine, errorColumn, errorAfter, curChar), reason);
150  }
151}
152/*
153 * JavaCC - OriginalChecksum=8a6e5be586cca28053ad55584e013006 (do not edit this
154 * line)
155 */