ParserException.java revision 953:221a84ef44c0
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.runtime; 27 28import jdk.nashorn.api.scripting.NashornException; 29import jdk.nashorn.internal.objects.Global; 30import jdk.nashorn.internal.parser.Token; 31 32/** 33 * ECMAScript parser exceptions. 34 */ 35@SuppressWarnings("serial") 36public final class ParserException extends NashornException { 37 // Source from which this ParserException originated 38 private final Source source; 39 // token responsible for this exception 40 private final long token; 41 // if this is traslated as ECMA error, which type should be used? 42 private final JSErrorType errorType; 43 44 /** 45 * Constructor 46 * 47 * @param msg exception message for this parser error. 48 */ 49 public ParserException(final String msg) { 50 this(JSErrorType.SYNTAX_ERROR, msg, null, -1, -1, -1); 51 } 52 53 /** 54 * Constructor 55 * 56 * @param errorType error type 57 * @param msg exception message 58 * @param source source from which this exception originates 59 * @param line line number of exception 60 * @param column column number of exception 61 * @param token token from which this exception originates 62 * 63 */ 64 public ParserException(final JSErrorType errorType, final String msg, final Source source, final int line, final int column, final long token) { 65 super(msg, source != null ? source.getName() : null, line, column); 66 this.source = source; 67 this.token = token; 68 this.errorType = errorType; 69 } 70 71 /** 72 * Get the {@code Source} of this {@code ParserException} 73 * @return source 74 */ 75 public Source getSource() { 76 return source; 77 } 78 79 /** 80 * Get the token responsible for this {@code ParserException} 81 * @return token 82 */ 83 public long getToken() { 84 return token; 85 } 86 87 /** 88 * Get token position within source where the error originated. 89 * @return token position if available, else -1 90 */ 91 public int getPosition() { 92 return Token.descPosition(token); 93 } 94 95 /** 96 * Get the {@code JSErrorType} of this {@code ParserException} 97 * @return error type 98 */ 99 public JSErrorType getErrorType() { 100 return errorType; 101 } 102 103 /** 104 * Throw this {@code ParserException} as one of the 7 native JavaScript errors 105 */ 106 public void throwAsEcmaException() { 107 throw ECMAErrors.asEcmaException(this); 108 } 109 110 /** 111 * Throw this {@code ParserException} as one of the 7 native JavaScript errors 112 * @param global global scope object 113 */ 114 public void throwAsEcmaException(final Global global) { 115 throw ECMAErrors.asEcmaException(global, this); 116 } 117} 118 119