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