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