001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2020, Units of Measurement project. 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.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:werner@units.tech">Werner Keil</a> 044 * @version 1.0, Jan 11, 2020 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 @SuppressWarnings("unused") 089 private 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 @SuppressWarnings("unused") 096 private String[] tokenImage; 097 098 /** 099 * It uses "currentToken" and "expectedTokenSequences" to generate a parse error message and returns it. If this object has been created due to a 100 * parse error, and you do not catch it (it gets thrown from the parser) the correct error message gets displayed. 101 */ 102 private static String initialise(Token currentToken, int[][] expectedTokenSequences, String[] tokenImage) { 103 String eol = System.getProperty("line.separator", "\n"); 104 StringBuilder expected = new StringBuilder(); 105 int maxSize = 0; 106 for (int[] expectedTokenSequence : expectedTokenSequences) { 107 if (maxSize < expectedTokenSequence.length) { 108 maxSize = expectedTokenSequence.length; 109 } 110 for (int anExpectedTokenSequence : expectedTokenSequence) { 111 expected.append(tokenImage[anExpectedTokenSequence]).append(' '); 112 } 113 if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { 114 expected.append("..."); 115 } 116 expected.append(eol).append(" "); 117 } 118 String retval = "Encountered \""; 119 Token tok = currentToken.next; 120 for (int i = 0; i < maxSize; i++) { 121 if (i != 0) 122 retval += " "; 123 if (tok.kind == 0) { 124 retval += tokenImage[0]; 125 break; 126 } 127 retval += " " + tokenImage[tok.kind]; 128 retval += " \""; 129 retval += add_escapes(tok.image); 130 retval += " \""; 131 tok = tok.next; 132 } 133 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 134 retval += "." + eol; 135 if (expectedTokenSequences.length == 1) { 136 retval += "Was expecting:" + eol + " "; 137 } else { 138 retval += "Was expecting one of:" + eol + " "; 139 } 140 retval += expected.toString(); 141 return retval; 142 } 143 144 /** 145 * The end of line string for this machine. 146 */ 147 protected String eol = System.getProperty("line.separator", "\n"); 148 149 /** 150 * Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII string literal. 151 */ 152 static String add_escapes(String str) { 153 StringBuilder retval = new StringBuilder(); 154 char ch; 155 for (int i = 0; i < str.length(); i++) { 156 switch (str.charAt(i)) { 157 case 0: 158 continue; 159 case '\b': 160 retval.append("\\b"); 161 continue; 162 case '\t': 163 retval.append("\\t"); 164 continue; 165 case '\n': 166 retval.append("\\n"); 167 continue; 168 case '\f': 169 retval.append("\\f"); 170 continue; 171 case '\r': 172 retval.append("\\r"); 173 continue; 174 case '\"': 175 retval.append("\\\""); 176 continue; 177 case '\'': 178 retval.append("\\\'"); 179 continue; 180 case '\\': 181 retval.append("\\\\"); 182 continue; 183 default: 184 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 185 String s = "0000" + Integer.toString(ch, 16); 186 retval.append("\\u").append(s.substring(s.length() - 4, s.length())); 187 } else { 188 retval.append(ch); 189 } 190 } 191 } 192 return retval.toString(); 193 } 194 195} 196/* 197 * JavaCC - OriginalChecksum=c67b0f8ee6c642900399352b33f90efd (do not edit this 198 * line) 199 */