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 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import jdk.nashorn.api.scripting.NashornException;

  29 import jdk.nashorn.internal.parser.Token;
  30 
  31 /**
  32  * ECMAScript parser exceptions.
  33  */
  34 @SuppressWarnings("serial")
  35 public final class ParserException extends NashornException {
  36     // Source from which this ParserException originated
  37     private final Source source;
  38     // token responsible for this exception
  39     private final long token;
  40     // if this is traslated as ECMA error, which type should be used?
  41     private final JSErrorType errorType;
  42 
  43     /**
  44      * Constructor
  45      *
  46      * @param msg exception message for this parser error.
  47      */
  48     public ParserException(final String msg) {
  49         this(JSErrorType.SYNTAX_ERROR, msg, null, -1, -1, -1);
  50     }
  51 
  52     /**
  53      * Constructor
  54      *
  55      * @param errorType error type
  56      * @param msg       exception message
  57      * @param source    source from which this exception originates
  58      * @param line      line number of exception
  59      * @param column    column number of exception
  60      * @param token     token from which this exception originates
  61      *
  62      */
  63     public ParserException(final JSErrorType errorType, final String msg, final Source source, final int line, final int column, final long token) {
  64         super(msg, source != null ? source.getName() : null, line, column);
  65         this.source = source;
  66         this.token = token;
  67         this.errorType = errorType;
  68     }
  69 
  70     /**
  71      * Get the {@code Source} of this {@code ParserException}
  72      * @return source
  73      */
  74     public Source getSource() {
  75         return source;
  76     }
  77 
  78     /**
  79      * Get the token responsible for this {@code ParserException}
  80      * @return token
  81      */
  82     public long getToken() {
  83         return token;
  84     }
  85 
  86     /**
  87      * Get token position within source where the error originated.
  88      * @return token position if available, else -1
  89      */
  90     public int getPosition() {
  91         return Token.descPosition(token);
  92     }
  93 
  94     /**
  95      * Get the {@code JSErrorType} of this {@code ParserException}
  96      * @return error type
  97      */
  98     public JSErrorType getErrorType() {
  99         return errorType;
 100     }
 101 
 102     /**
 103      * Throw this {@code ParserException} as one of the 7 native JavaScript errors
 104      */
 105     public void throwAsEcmaException() {
 106         throw ECMAErrors.asEcmaException(this);
 107     }
 108 
 109     /**
 110      * Throw this {@code ParserException} as one of the 7 native JavaScript errors
 111      * @param global global scope object
 112      */
 113     public void throwAsEcmaException(final ScriptObject global) {
 114         throw ECMAErrors.asEcmaException(global, this);
 115     }
 116 }
 117 
--- EOF ---