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