001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package eu.cqse.check.framework.scanner; 018 019import eu.cqse.check.framework.scanner.ETokenType.ETokenClass; 020 021/** 022 * Generated scanners throw exceptions of this description to signal scanning 023 * problems. This class is immutable. 024 */ 025public class ScannerException extends Exception { 026 027 /** Version used for serialization. */ 028 private static final long serialVersionUID = 1; 029 030 /** Exception description. */ 031 private final ETokenType type; 032 033 /** A verbatim copy of the scanned text. */ 034 private final String yyText; 035 036 /** The line number where the problem occurred. */ 037 private final int position; 038 039 /** 040 * Create a new <code>ScannerException</code>. 041 * 042 * @param type 043 * the token type representing the scanner error. Expected to 044 * have token class {@link ETokenClass#ERROR}. 045 * @param yyText 046 * A verbatim copy of the scanned text. 047 * @param position 048 * The line number where the problem occurred. 049 */ 050 public ScannerException(ETokenType type, String yyText, int position) { 051 this.type = type; 052 this.yyText = yyText; 053 this.position = position; 054 } 055 056 /** 057 * Get an error message. 058 * 059 * @return An error message, including line number and scanned text. 060 */ 061 @Override 062 public String getMessage() { 063 return type.name().toLowerCase().replace('_', ' ') + " at line " + position + " [" + yyText + "]"; 064 } 065 066 /** Returns the line number where the proplem ocurred. */ 067 public int getLineNumber() { 068 return position; 069 } 070}