nashorn/src/jdk/nashorn/internal/ir/Location.java
changeset 17529 7bd8457aa8d9
parent 17289 4dec41b3c5e3
parent 17528 0f5bedbf0e06
child 17530 7d6fffdd46a9
child 17743 ecc98238ef11
equal deleted inserted replaced
17289:4dec41b3c5e3 17529:7bd8457aa8d9
     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.ir;
       
    27 
       
    28 import jdk.nashorn.internal.parser.Token;
       
    29 import jdk.nashorn.internal.parser.TokenType;
       
    30 import jdk.nashorn.internal.runtime.Source;
       
    31 
       
    32 /**
       
    33  * Used to locate an entity back to it's source file.
       
    34  */
       
    35 public class Location implements Cloneable {
       
    36     /** Source of entity. */
       
    37     private final Source source;
       
    38 
       
    39     /** Token descriptor. */
       
    40     private final long token;
       
    41 
       
    42     /**
       
    43      * Constructor
       
    44      *
       
    45      * @param source the source
       
    46      * @param token  token
       
    47      */
       
    48     public Location(final Source source, final long token) {
       
    49         this.source = source;
       
    50         this.token = token;
       
    51     }
       
    52 
       
    53     /**
       
    54      * Copy constructor
       
    55      *
       
    56      * @param location source node
       
    57      */
       
    58     protected Location(final Location location) {
       
    59         this.source = location.source;
       
    60         this.token = location.token;
       
    61     }
       
    62 
       
    63     @Override
       
    64     protected Object clone() {
       
    65         try {
       
    66             return super.clone();
       
    67         } catch(CloneNotSupportedException e) {
       
    68             throw new AssertionError(e);
       
    69         }
       
    70     }
       
    71 
       
    72     @Override
       
    73     public final boolean equals(final Object other) {
       
    74         return super.equals(other);
       
    75     }
       
    76 
       
    77     @Override
       
    78     public final int hashCode() {
       
    79         return super.hashCode();
       
    80     }
       
    81 
       
    82     /**
       
    83      * Return token position from a token descriptor.
       
    84      *
       
    85      * @return Start position of the token in the source.
       
    86      */
       
    87     public int position() {
       
    88         return Token.descPosition(token);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Return token length from a token descriptor.
       
    93      *
       
    94      * @return Length of the token.
       
    95      */
       
    96     public int length() {
       
    97         return Token.descLength(token);
       
    98     }
       
    99 
       
   100     /**
       
   101      * Return token tokenType from a token descriptor.
       
   102      *
       
   103      * @return Type of token.
       
   104      */
       
   105     public TokenType tokenType() {
       
   106         return Token.descType(token);
       
   107     }
       
   108 
       
   109     /**
       
   110      * Test token tokenType.
       
   111      *
       
   112      * @param type a type to check this token against
       
   113      * @return true if token types match.
       
   114      */
       
   115     public boolean isTokenType(final TokenType type) {
       
   116         return Token.descType(token) == type;
       
   117     }
       
   118 
       
   119     /**
       
   120      * Get the source for this location
       
   121      * @return the source
       
   122      */
       
   123     public Source getSource() {
       
   124         return source;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Get the token for this location
       
   129      * @return the token
       
   130      */
       
   131     public long getToken() {
       
   132         return token;
       
   133     }
       
   134 }