author | mhaupt |
Wed, 28 Oct 2015 10:54:05 +0100 | |
changeset 33414 | 2e284c36d51f |
parent 25865 | d38d876f1654 |
child 33890 | 2e8c1be40a52 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
33414 | 2 |
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
/** |
|
24994
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
64 |
* Normally returns the token itself, except in case of string tokens |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
65 |
* which report their position past their opening delimiter and thus |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
66 |
* need to have position and length adjusted. |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
67 |
* |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
68 |
* @param token Token descriptor. |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
69 |
* @return same or adjusted token. |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
70 |
*/ |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
71 |
public static long withDelimiter(final long token) { |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
72 |
final TokenType tokenType = Token.descType(token); |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
73 |
switch(tokenType) { |
33414 | 74 |
case STRING: |
75 |
case ESCSTRING: |
|
76 |
case EXECSTRING: |
|
77 |
case TEMPLATE: |
|
78 |
case TEMPLATE_TAIL: { |
|
24994
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
79 |
final int start = Token.descPosition(token) - 1; |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
80 |
final int len = Token.descLength(token) + 2; |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
81 |
return toDesc(tokenType, start, len); |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
82 |
} |
33414 | 83 |
case TEMPLATE_HEAD: |
84 |
case TEMPLATE_MIDDLE: { |
|
85 |
final int start = Token.descPosition(token) - 1; |
|
86 |
final int len = Token.descLength(token) + 3; |
|
87 |
return toDesc(tokenType, start, len); |
|
88 |
} |
|
24994
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
89 |
default: { |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
90 |
return token; |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
91 |
} |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
92 |
} |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
93 |
} |
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
94 |
|
92a19723a5ac
8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents:
16151
diff
changeset
|
95 |
/** |
16147 | 96 |
* Extract token length from a token descriptor. |
97 |
* @param token Token descriptor. |
|
98 |
* @return Length of the token. |
|
99 |
*/ |
|
100 |
public static int descLength(final long token) { |
|
101 |
return (int)token >>> 8; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Extract token type from a token descriptor. |
|
106 |
* @param token Token descriptor. |
|
107 |
* @return Type of token. |
|
108 |
*/ |
|
109 |
public static TokenType descType(final long token) { |
|
110 |
return TokenType.getValues()[(int)token & 0xff]; |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Change the token to use a new type. |
|
115 |
* |
|
116 |
* @param token The original token. |
|
117 |
* @param newType The new token type. |
|
118 |
* @return The recast token. |
|
119 |
*/ |
|
120 |
public static long recast(final long token, final TokenType newType) { |
|
121 |
return token & ~0xFFL | newType.ordinal(); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Return a string representation of a token. |
|
126 |
* @param source Token source. |
|
127 |
* @param token Token descriptor. |
|
128 |
* @param verbose True to include details. |
|
129 |
* @return String representation. |
|
130 |
*/ |
|
131 |
public static String toString(final Source source, final long token, final boolean verbose) { |
|
132 |
final TokenType type = Token.descType(token); |
|
133 |
String result; |
|
134 |
||
135 |
if (source != null && type.getKind() == LITERAL) { |
|
136 |
result = source.getString(token); |
|
137 |
} else { |
|
138 |
result = type.getNameOrType(); |
|
139 |
} |
|
140 |
||
141 |
if (verbose) { |
|
142 |
final int position = Token.descPosition(token); |
|
143 |
final int length = Token.descLength(token); |
|
144 |
result += " (" + position + ", " + length + ")"; |
|
145 |
} |
|
146 |
||
147 |
return result; |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* String conversion of token |
|
152 |
* |
|
153 |
* @param source the source |
|
154 |
* @param token the token |
|
155 |
* |
|
156 |
* @return token as string |
|
157 |
*/ |
|
158 |
public static String toString(final Source source, final long token) { |
|
159 |
return Token.toString(source, token, false); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* String conversion of token - version without source given |
|
164 |
* |
|
165 |
* @param token the token |
|
166 |
* |
|
167 |
* @return token as string |
|
168 |
*/ |
|
169 |
public static String toString(final long token) { |
|
170 |
return Token.toString(null, token, false); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Static hash code computation function token |
|
175 |
* |
|
176 |
* @param token a token |
|
177 |
* |
|
178 |
* @return hash code for token |
|
179 |
*/ |
|
180 |
public static int hashCode(final long token) { |
|
181 |
return (int)(token ^ token >>> 32); |
|
182 |
} |
|
183 |
||
184 |
} |