001/* 002 * Units of Measurement Systems 003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil and others. 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-363, Units of Measurement nor the names of their contributors may be used to 017 * endorse or promote products 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 systems.uom.ucum.internal.format; 031 032import javax.measure.MeasurementException; 033 034/** 035 * This exception is thrown when token errors are encountered. 036 * You can explicitly create objects of this exception type by 037 * calling the method raiseTokenException in the generated 038 * parser. 039 * 040 * You can modify this class to customize your error reporting 041 * mechanisms so long as you retain the public fields. 042 * 043 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 044 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 045 * @version 0.5.5, Mar 15, 2017 046 */ 047public class TokenException extends MeasurementException { 048//TODO try to merge this with ParserException 049 /** 050 * The Serialization identifier for this class. 051 * Increment only if the <i>serialized</i> form of the 052 * class changes. 053 */ 054 private static final long serialVersionUID = 2932151235799168061L; 055 056 /** 057 * This constructor is used by the method "raiseTokenException" 058 * in the generated parser. Calling this constructor generates 059 * a new object of this type with the fields "currentToken", 060 * "expectedTokenSequences", and "tokenImage" set. 061 */ 062 public TokenException(Token currentTokenVal, 063 int[][] expectedTokenSequencesVal, 064 String[] tokenImageVal 065 ) 066 { 067 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); 068 currentToken = currentTokenVal; 069 expectedTokenSequences = expectedTokenSequencesVal; 070 tokenImage = tokenImageVal; 071 } 072 073 /** 074 * The following constructors are for use by you for whatever 075 * purpose you can think of. Constructing the exception in this 076 * manner makes the exception behave in the normal way - i.e., as 077 * documented in the class "Throwable". The fields "errorToken", 078 * "expectedTokenSequences", and "tokenImage" do not contain 079 * relevant information. The JavaCC generated code does not use 080 * these constructors. 081 */ 082 083 public TokenException() { 084 super(); 085 } 086 087 /** Constructor with message. */ 088 public TokenException(String message) { 089 super(message); 090 } 091 092 093 /** 094 * This is the last token that has been consumed successfully. If 095 * this object has been created due to a parse error, the token 096 * followng this token will (therefore) be the first error token. 097 */ 098 public Token currentToken; 099 100 /** 101 * Each entry in this array is an array of integers. Each array 102 * of integers represents a sequence of tokens (by their ordinal 103 * values) that is expected at this point of the parse. 104 */ 105 public int[][] expectedTokenSequences; 106 107 /** 108 * This is a reference to the "tokenImage" array of the generated 109 * parser within which the parse error occurred. This array is 110 * defined in the generated ...Constants interface. 111 */ 112 public String[] tokenImage; 113 114 /** 115 * It uses "currentToken" and "expectedTokenSequences" to generate a parse 116 * error message and returns it. If this object has been created 117 * due to a parse error, and you do not catch it (it gets thrown 118 * from the parser) the correct error message 119 * gets displayed. 120 */ 121 private static String initialise(Token currentToken, 122 int[][] expectedTokenSequences, 123 String[] tokenImage) { 124 String eol = System.getProperty("line.separator", "\n"); 125 StringBuilder expected = new StringBuilder(); 126 int maxSize = 0; 127 for (int[] expectedTokenSequence : expectedTokenSequences) { 128 if (maxSize < expectedTokenSequence.length) { 129 maxSize = expectedTokenSequence.length; 130 } 131 for (int anExpectedTokenSequence : expectedTokenSequence) { 132 expected.append(tokenImage[anExpectedTokenSequence]).append(' '); 133 } 134 if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { 135 expected.append("..."); 136 } 137 expected.append(eol).append(" "); 138 } 139 String retval = "Encountered \""; 140 Token tok = currentToken.next; 141 for (int i = 0; i < maxSize; i++) { 142 if (i != 0) retval += " "; 143 if (tok.kind == 0) { 144 retval += tokenImage[0]; 145 break; 146 } 147 retval += " " + tokenImage[tok.kind]; 148 retval += " \""; 149 retval += add_escapes(tok.image); 150 retval += " \""; 151 tok = tok.next; 152 } 153 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 154 retval += "." + eol; 155 if (expectedTokenSequences.length == 1) { 156 retval += "Was expecting:" + eol + " "; 157 } else { 158 retval += "Was expecting one of:" + eol + " "; 159 } 160 retval += expected.toString(); 161 return retval; 162 } 163 164 /** 165 * The end of line string for this machine. 166 */ 167 protected String eol = System.getProperty("line.separator", "\n"); 168 169 /** 170 * Used to convert raw characters to their escaped version 171 * when these raw version cannot be used as part of an ASCII 172 * string literal. 173 */ 174 static String add_escapes(String str) { 175 StringBuilder retval = new StringBuilder(); 176 char ch; 177 for (int i = 0; i < str.length(); i++) { 178 switch (str.charAt(i)) 179 { 180 case 0 : 181 continue; 182 case '\b': 183 retval.append("\\b"); 184 continue; 185 case '\t': 186 retval.append("\\t"); 187 continue; 188 case '\n': 189 retval.append("\\n"); 190 continue; 191 case '\f': 192 retval.append("\\f"); 193 continue; 194 case '\r': 195 retval.append("\\r"); 196 continue; 197 case '\"': 198 retval.append("\\\""); 199 continue; 200 case '\'': 201 retval.append("\\\'"); 202 continue; 203 case '\\': 204 retval.append("\\\\"); 205 continue; 206 default: 207 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 208 String s = "0000" + Integer.toString(ch, 16); 209 retval.append("\\u").append(s.substring(s.length() - 4, s.length())); 210 } else { 211 retval.append(ch); 212 } 213 } 214 } 215 return retval.toString(); 216 } 217 218} 219/* JavaCC - OriginalChecksum=c67b0f8ee6c642900399352b33f90efd (do not edit this line) */