Token.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.parser;
27
28import static jdk.nashorn.internal.parser.TokenKind.LITERAL;
29
30import jdk.nashorn.internal.runtime.Source;
31
32/**
33 * Basic parse/lex unit.
34 *
35 */
36public 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     * Normally returns the token itself, except in case of string tokens
65     * which report their position past their opening delimiter and thus
66     * need to have position and length adjusted.
67     *
68     * @param token Token descriptor.
69     * @return same or adjusted token.
70     */
71    public static long withDelimiter(final long token) {
72        final TokenType tokenType = Token.descType(token);
73        switch(tokenType) {
74            case STRING: case ESCSTRING: case EXECSTRING: {
75                final int start = Token.descPosition(token) - 1;
76                final int len = Token.descLength(token) + 2;
77                return toDesc(tokenType, start, len);
78            }
79            default: {
80                return token;
81            }
82        }
83    }
84
85    /**
86     * Extract token length from a token descriptor.
87     * @param token Token descriptor.
88     * @return Length of the token.
89     */
90    public static int descLength(final long token) {
91        return (int)token >>> 8;
92    }
93
94    /**
95     * Extract token type from a token descriptor.
96     * @param token Token descriptor.
97     * @return Type of token.
98     */
99    public static TokenType descType(final long token) {
100        return TokenType.getValues()[(int)token & 0xff];
101    }
102
103    /**
104     * Change the token to use a new type.
105     *
106     * @param token   The original token.
107     * @param newType The new token type.
108     * @return The recast token.
109     */
110    public static long recast(final long token, final TokenType newType) {
111        return token & ~0xFFL | newType.ordinal();
112    }
113
114    /**
115     * Return a string representation of a token.
116     * @param source  Token source.
117     * @param token   Token descriptor.
118     * @param verbose True to include details.
119     * @return String representation.
120     */
121    public static String toString(final Source source, final long token, final boolean verbose) {
122        final TokenType type = Token.descType(token);
123        String result;
124
125        if (source != null && type.getKind() == LITERAL) {
126            result = source.getString(token);
127        } else {
128            result = type.getNameOrType();
129        }
130
131        if (verbose) {
132            final int position = Token.descPosition(token);
133            final int length = Token.descLength(token);
134            result += " (" + position + ", " + length + ")";
135        }
136
137        return result;
138    }
139
140    /**
141     * String conversion of token
142     *
143     * @param source the source
144     * @param token  the token
145     *
146     * @return token as string
147     */
148    public static String toString(final Source source, final long token) {
149        return Token.toString(source, token, false);
150    }
151
152    /**
153     * String conversion of token - version without source given
154     *
155     * @param token  the token
156     *
157     * @return token as string
158     */
159    public static String toString(final long token) {
160        return Token.toString(null, token, false);
161    }
162
163    /**
164     * Static hash code computation function token
165     *
166     * @param token a token
167     *
168     * @return hash code for token
169     */
170    public static int hashCode(final long token) {
171        return (int)(token ^ token >>> 32);
172    }
173
174}
175