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