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.parser; 27 28 import static jdk.nashorn.internal.parser.TokenKind.LITERAL; 29 30 import jdk.nashorn.internal.runtime.Source; 31 32 /** 33 * Basic parse/lex unit. 34 * 35 */ 36 public class Token { 37 38 private Token() { 39 } 40 41 /** 42 * Create a compact form of token information. 43 * @param type Type of token. 44 * @param position Start position of the token in the source. 45 * @param length Length of the token. 46 * @return Token descriptor. 47 */ 48 public static long toDesc(final TokenType type, final int position, final int length) { 49 return (long)position << 32 | 50 (long)length << 8 | 51 type.ordinal(); 52 } 53 54 /** 55 * Extract token position from a token descriptor. 56 * @param token Token descriptor. 57 * @return Start position of the token in the source. 58 */ 59 public static int descPosition(final long token) { 60 return (int)(token >>> 32); 61 } 62 63 /** 64 * Extract token length from a token descriptor. 65 * @param token Token descriptor. 66 * @return Length of the token. 67 */ 68 public static int descLength(final long token) { 69 return (int)token >>> 8; 70 } 71 72 /** 73 * Extract token type from a token descriptor. 74 * @param token Token descriptor. 75 * @return Type of token. 76 */ 77 public static TokenType descType(final long token) { 78 return TokenType.getValues()[(int)token & 0xff]; 79 } 80 81 /** 82 * Change the token to use a new type. 83 * 84 * @param token The original token. 85 * @param newType The new token type. 86 * @return The recast token. 87 */ 88 public static long recast(final long token, final TokenType newType) { 89 return token & ~0xFFL | newType.ordinal(); 90 } 91 92 /** 93 * Return a string representation of a token. 94 * @param source Token source. 95 * @param token Token descriptor. 96 * @param verbose True to include details. 97 * @return String representation. 98 */ 99 public static String toString(final Source source, final long token, final boolean verbose) { 100 final TokenType type = Token.descType(token); 101 String result; 102 103 if (source != null && type.getKind() == LITERAL) { 104 result = source.getString(token); 105 } else { 106 result = type.getNameOrType(); 107 } 108 109 if (verbose) { 110 final int position = Token.descPosition(token); 111 final int length = Token.descLength(token); 112 result += " (" + position + ", " + length + ")"; 113 } 114 115 return result; 116 } 117 118 /** 119 * String conversion of token 120 * 121 * @param source the source 122 * @param token the token 123 * 124 * @return token as string 125 */ 126 public static String toString(final Source source, final long token) { 127 return Token.toString(source, token, false); 128 } 129 130 /** 131 * String conversion of token - version without source given 132 * 133 * @param token the token 134 * 135 * @return token as string 136 */ 137 public static String toString(final long token) { 138 return Token.toString(null, token, false); 139 } 140 141 /** 142 * Static hash code computation function token 143 * 144 * @param token a token 145 * 146 * @return hash code for token 147 */ 148 public static int hashCode(final long token) { 149 return (int)(token ^ token >>> 32); 150 } 151 152 }