author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 37924 | nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/AbstractParser.java@a78497edf9fb |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, 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 |
||
19089 | 28 |
import static jdk.nashorn.internal.parser.TokenType.COMMENT; |
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
29 |
import static jdk.nashorn.internal.parser.TokenType.DIRECTIVE_COMMENT; |
16147 | 30 |
import static jdk.nashorn.internal.parser.TokenType.EOF; |
31 |
import static jdk.nashorn.internal.parser.TokenType.EOL; |
|
32 |
import static jdk.nashorn.internal.parser.TokenType.IDENT; |
|
26243 | 33 |
import java.util.HashMap; |
34 |
import java.util.Map; |
|
16147 | 35 |
import jdk.nashorn.internal.ir.IdentNode; |
36 |
import jdk.nashorn.internal.ir.LiteralNode; |
|
37 |
import jdk.nashorn.internal.parser.Lexer.LexerToken; |
|
38 |
import jdk.nashorn.internal.parser.Lexer.RegexToken; |
|
39 |
import jdk.nashorn.internal.runtime.ECMAErrors; |
|
40 |
import jdk.nashorn.internal.runtime.ErrorManager; |
|
41 |
import jdk.nashorn.internal.runtime.JSErrorType; |
|
42 |
import jdk.nashorn.internal.runtime.ParserException; |
|
17257
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
43 |
import jdk.nashorn.internal.runtime.Source; |
16258 | 44 |
import jdk.nashorn.internal.runtime.regexp.RegExpFactory; |
16147 | 45 |
|
46 |
/** |
|
47 |
* Base class for parsers. |
|
48 |
*/ |
|
49 |
public abstract class AbstractParser { |
|
50 |
/** Source to parse. */ |
|
51 |
protected final Source source; |
|
52 |
||
53 |
/** Error manager to report errors. */ |
|
54 |
protected final ErrorManager errors; |
|
55 |
||
56 |
/** Stream of lex tokens to parse. */ |
|
57 |
protected TokenStream stream; |
|
58 |
||
59 |
/** Index of current token. */ |
|
60 |
protected int k; |
|
61 |
||
24719 | 62 |
/** Previous token - accessible to sub classes */ |
63 |
protected long previousToken; |
|
64 |
||
16147 | 65 |
/** Descriptor of current token. */ |
66 |
protected long token; |
|
67 |
||
68 |
/** Type of current token. */ |
|
69 |
protected TokenType type; |
|
70 |
||
71 |
/** Type of last token. */ |
|
72 |
protected TokenType last; |
|
73 |
||
74 |
/** Start position of current token. */ |
|
75 |
protected int start; |
|
76 |
||
77 |
/** Finish position of previous token. */ |
|
78 |
protected int finish; |
|
79 |
||
80 |
/** Current line number. */ |
|
81 |
protected int line; |
|
82 |
||
83 |
/** Position of last EOL + 1. */ |
|
84 |
protected int linePosition; |
|
85 |
||
86 |
/** Lexer used to scan source content. */ |
|
87 |
protected Lexer lexer; |
|
88 |
||
89 |
/** Is this parser running under strict mode? */ |
|
90 |
protected boolean isStrictMode; |
|
91 |
||
24719 | 92 |
/** What should line numbers be counted from? */ |
93 |
protected final int lineOffset; |
|
94 |
||
26243 | 95 |
private final Map<String, String> canonicalNames = new HashMap<>(); |
96 |
||
16147 | 97 |
/** |
98 |
* Construct a parser. |
|
99 |
* |
|
24719 | 100 |
* @param source Source to parse. |
101 |
* @param errors Error reporting manager. |
|
102 |
* @param strict True if we are in strict mode |
|
103 |
* @param lineOffset Offset from which lines should be counted |
|
16147 | 104 |
*/ |
24719 | 105 |
protected AbstractParser(final Source source, final ErrorManager errors, final boolean strict, final int lineOffset) { |
33890
2e8c1be40a52
8143304: Random failures when script size exceeds token limits
hannesw
parents:
31198
diff
changeset
|
106 |
if (source.getLength() > Token.LENGTH_MASK) { |
2e8c1be40a52
8143304: Random failures when script size exceeds token limits
hannesw
parents:
31198
diff
changeset
|
107 |
throw new RuntimeException("Source exceeds size limit of " + Token.LENGTH_MASK + " bytes"); |
2e8c1be40a52
8143304: Random failures when script size exceeds token limits
hannesw
parents:
31198
diff
changeset
|
108 |
} |
16147 | 109 |
this.source = source; |
110 |
this.errors = errors; |
|
111 |
this.k = -1; |
|
112 |
this.token = Token.toDesc(EOL, 0, 1); |
|
113 |
this.type = EOL; |
|
114 |
this.last = EOL; |
|
115 |
this.isStrictMode = strict; |
|
24719 | 116 |
this.lineOffset = lineOffset; |
16147 | 117 |
} |
118 |
||
119 |
/** |
|
120 |
* Get the ith token. |
|
121 |
* |
|
122 |
* @param i Index of token. |
|
123 |
* |
|
124 |
* @return the token |
|
125 |
*/ |
|
126 |
protected final long getToken(final int i) { |
|
127 |
// Make sure there are enough tokens available. |
|
128 |
while (i > stream.last()) { |
|
129 |
// If we need to buffer more for lookahead. |
|
130 |
if (stream.isFull()) { |
|
131 |
stream.grow(); |
|
132 |
} |
|
133 |
||
134 |
// Get more tokens. |
|
135 |
lexer.lexify(); |
|
136 |
} |
|
137 |
||
138 |
return stream.get(i); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Return the tokenType of the ith token. |
|
143 |
* |
|
144 |
* @param i Index of token |
|
145 |
* |
|
146 |
* @return the token type |
|
147 |
*/ |
|
148 |
protected final TokenType T(final int i) { |
|
149 |
// Get token descriptor and extract tokenType. |
|
150 |
return Token.descType(getToken(i)); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
19089 | 154 |
* Seek next token that is not an EOL or comment. |
16147 | 155 |
* |
156 |
* @return tokenType of next token. |
|
157 |
*/ |
|
158 |
protected final TokenType next() { |
|
159 |
do { |
|
160 |
nextOrEOL(); |
|
19089 | 161 |
} while (type == EOL || type == COMMENT); |
162 |
||
163 |
return type; |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Seek next token or EOL (skipping comments.) |
|
168 |
* |
|
169 |
* @return tokenType of next token. |
|
170 |
*/ |
|
171 |
protected final TokenType nextOrEOL() { |
|
172 |
do { |
|
173 |
nextToken(); |
|
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
174 |
if (type == DIRECTIVE_COMMENT) { |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
175 |
checkDirectiveComment(); |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
176 |
} |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
177 |
} while (type == COMMENT || type == DIRECTIVE_COMMENT); |
16147 | 178 |
|
179 |
return type; |
|
180 |
} |
|
181 |
||
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
182 |
// sourceURL= after directive comment |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
183 |
private static final String SOURCE_URL_PREFIX = "sourceURL="; |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
184 |
|
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
185 |
// currently only @sourceURL=foo supported |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
186 |
private void checkDirectiveComment() { |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
187 |
// if already set, ignore this one |
26055
fe8be844ba50
8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
24719
diff
changeset
|
188 |
if (source.getExplicitURL() != null) { |
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
189 |
return; |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
190 |
} |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
191 |
|
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
192 |
final String comment = (String) lexer.getValueOf(token, isStrictMode); |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
193 |
final int len = comment.length(); |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
194 |
// 4 characters for directive comment marker //@\s or //#\s |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
195 |
if (len > 4 && comment.substring(4).startsWith(SOURCE_URL_PREFIX)) { |
26055
fe8be844ba50
8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
24719
diff
changeset
|
196 |
source.setExplicitURL(comment.substring(4 + SOURCE_URL_PREFIX.length())); |
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
197 |
} |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
198 |
} |
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
199 |
|
16147 | 200 |
/** |
201 |
* Seek next token. |
|
202 |
* |
|
203 |
* @return tokenType of next token. |
|
204 |
*/ |
|
22390
1d2d88e478ea
8032068: implement @sourceURL and #sourceURL directives
sundar
parents:
20571
diff
changeset
|
205 |
private TokenType nextToken() { |
37924
a78497edf9fb
8156714: Parsing issue with automatic semicolon insertion
hannesw
parents:
33890
diff
changeset
|
206 |
// Capture last token type, but ignore comments (which are irrelevant for the purpose of newline detection). |
a78497edf9fb
8156714: Parsing issue with automatic semicolon insertion
hannesw
parents:
33890
diff
changeset
|
207 |
if (type != COMMENT) { |
a78497edf9fb
8156714: Parsing issue with automatic semicolon insertion
hannesw
parents:
33890
diff
changeset
|
208 |
last = type; |
a78497edf9fb
8156714: Parsing issue with automatic semicolon insertion
hannesw
parents:
33890
diff
changeset
|
209 |
} |
16147 | 210 |
if (type != EOF) { |
211 |
||
212 |
// Set up next token. |
|
213 |
k++; |
|
214 |
final long lastToken = token; |
|
24719 | 215 |
previousToken = token; |
16147 | 216 |
token = getToken(k); |
217 |
type = Token.descType(token); |
|
218 |
||
219 |
// do this before the start is changed below |
|
220 |
if (last != EOL) { |
|
221 |
finish = start + Token.descLength(lastToken); |
|
222 |
} |
|
223 |
||
224 |
if (type == EOL) { |
|
24719 | 225 |
line = Token.descLength(token); |
16147 | 226 |
linePosition = Token.descPosition(token); |
227 |
} else { |
|
228 |
start = Token.descPosition(token); |
|
229 |
} |
|
230 |
||
231 |
} |
|
232 |
||
233 |
return type; |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Get the message string for a message ID and arguments |
|
238 |
* |
|
239 |
* @param msgId The Message ID |
|
240 |
* @param args The arguments |
|
241 |
* |
|
242 |
* @return The message string |
|
243 |
*/ |
|
244 |
protected static String message(final String msgId, final String... args) { |
|
245 |
return ECMAErrors.getMessage("parser.error." + msgId, args); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Report an error. |
|
250 |
* |
|
251 |
* @param message Error message. |
|
252 |
* @param errorToken Offending token. |
|
17233 | 253 |
* @return ParserException upon failure. Caller should throw and not ignore |
16147 | 254 |
*/ |
17233 | 255 |
protected final ParserException error(final String message, final long errorToken) { |
256 |
return error(JSErrorType.SYNTAX_ERROR, message, errorToken); |
|
16147 | 257 |
} |
258 |
||
259 |
/** |
|
260 |
* Report an error. |
|
261 |
* |
|
262 |
* @param errorType The error type |
|
263 |
* @param message Error message. |
|
264 |
* @param errorToken Offending token. |
|
17233 | 265 |
* @return ParserException upon failure. Caller should throw and not ignore |
16147 | 266 |
*/ |
17233 | 267 |
protected final ParserException error(final JSErrorType errorType, final String message, final long errorToken) { |
16147 | 268 |
final int position = Token.descPosition(errorToken); |
269 |
final int lineNum = source.getLine(position); |
|
270 |
final int columnNum = source.getColumn(position); |
|
271 |
final String formatted = ErrorManager.format(message, source, lineNum, columnNum, errorToken); |
|
17233 | 272 |
return new ParserException(errorType, formatted, source, lineNum, columnNum, errorToken); |
16147 | 273 |
} |
274 |
||
275 |
/** |
|
276 |
* Report an error. |
|
277 |
* |
|
278 |
* @param message Error message. |
|
17233 | 279 |
* @return ParserException upon failure. Caller should throw and not ignore |
16147 | 280 |
*/ |
17233 | 281 |
protected final ParserException error(final String message) { |
282 |
return error(JSErrorType.SYNTAX_ERROR, message); |
|
16147 | 283 |
} |
284 |
||
285 |
/** |
|
286 |
* Report an error. |
|
287 |
* |
|
288 |
* @param errorType The error type |
|
289 |
* @param message Error message. |
|
17233 | 290 |
* @return ParserException upon failure. Caller should throw and not ignore |
16147 | 291 |
*/ |
17233 | 292 |
protected final ParserException error(final JSErrorType errorType, final String message) { |
16147 | 293 |
// TODO - column needs to account for tabs. |
294 |
final int position = Token.descPosition(token); |
|
295 |
final int column = position - linePosition; |
|
296 |
final String formatted = ErrorManager.format(message, source, line, column, token); |
|
17233 | 297 |
return new ParserException(errorType, formatted, source, line, column, token); |
16147 | 298 |
} |
299 |
||
300 |
/** |
|
17257
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
301 |
* Report a warning to the error manager. |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
302 |
* |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
303 |
* @param errorType The error type of the warning |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
304 |
* @param message Warning message. |
17518
2225a4f929c0
8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents:
17257
diff
changeset
|
305 |
* @param errorToken error token |
17257
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
306 |
*/ |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
307 |
protected final void warning(final JSErrorType errorType, final String message, final long errorToken) { |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
308 |
errors.warning(error(errorType, message, errorToken)); |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
309 |
} |
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
310 |
|
a2232050cd8f
8008814: Configurable ignore/warning/error behavior for function declaration as statement
attila
parents:
17233
diff
changeset
|
311 |
/** |
16147 | 312 |
* Generate 'expected' message. |
313 |
* |
|
314 |
* @param expected Expected tokenType. |
|
315 |
* |
|
316 |
* @return the message string |
|
317 |
*/ |
|
318 |
protected final String expectMessage(final TokenType expected) { |
|
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16226
diff
changeset
|
319 |
final String tokenString = Token.toString(source, token); |
16147 | 320 |
String msg; |
321 |
||
322 |
if (expected == null) { |
|
323 |
msg = AbstractParser.message("expected.stmt", tokenString); |
|
324 |
} else { |
|
325 |
final String expectedName = expected.getNameOrType(); |
|
326 |
msg = AbstractParser.message("expected", expectedName, tokenString); |
|
327 |
} |
|
328 |
||
329 |
return msg; |
|
330 |
} |
|
331 |
||
332 |
/** |
|
26505
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
333 |
* Check current token and advance to the next token. |
16147 | 334 |
* |
335 |
* @param expected Expected tokenType. |
|
336 |
* |
|
337 |
* @throws ParserException on unexpected token type |
|
338 |
*/ |
|
339 |
protected final void expect(final TokenType expected) throws ParserException { |
|
26505
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
340 |
expectDontAdvance(expected); |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
341 |
next(); |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
342 |
} |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
343 |
|
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
344 |
/** |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
345 |
* Check current token, but don't advance to the next token. |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
346 |
* |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
347 |
* @param expected Expected tokenType. |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
348 |
* |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
349 |
* @throws ParserException on unexpected token type |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
350 |
*/ |
d29bf8787b43
8057931: Instead of not skipping small functions in parser, make lexer avoid them instead
attila
parents:
26243
diff
changeset
|
351 |
protected final void expectDontAdvance(final TokenType expected) throws ParserException { |
16147 | 352 |
if (type != expected) { |
17233 | 353 |
throw error(expectMessage(expected)); |
16147 | 354 |
} |
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Check next token, get its value and advance. |
|
359 |
* |
|
360 |
* @param expected Expected tokenType. |
|
361 |
* @return The JavaScript value of the token |
|
362 |
* @throws ParserException on unexpected token type |
|
363 |
*/ |
|
364 |
protected final Object expectValue(final TokenType expected) throws ParserException { |
|
365 |
if (type != expected) { |
|
17233 | 366 |
throw error(expectMessage(expected)); |
16147 | 367 |
} |
368 |
||
369 |
final Object value = getValue(); |
|
370 |
||
371 |
next(); |
|
372 |
||
373 |
return value; |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Get the value of the current token. |
|
378 |
* |
|
379 |
* @return JavaScript value of the token. |
|
380 |
*/ |
|
381 |
protected final Object getValue() { |
|
382 |
return getValue(token); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Get the value of a specific token |
|
387 |
* |
|
388 |
* @param valueToken the token |
|
389 |
* |
|
390 |
* @return JavaScript value of the token |
|
391 |
*/ |
|
392 |
protected final Object getValue(final long valueToken) { |
|
393 |
try { |
|
394 |
return lexer.getValueOf(valueToken, isStrictMode); |
|
395 |
} catch (final ParserException e) { |
|
396 |
errors.error(e); |
|
397 |
} |
|
398 |
||
399 |
return null; |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Certain future reserved words can be used as identifiers in |
|
404 |
* non-strict mode. Check if the current token is one such. |
|
405 |
* |
|
406 |
* @return true if non strict mode identifier |
|
407 |
*/ |
|
408 |
protected final boolean isNonStrictModeIdent() { |
|
409 |
return !isStrictMode && type.getKind() == TokenKind.FUTURESTRICT; |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Get ident. |
|
414 |
* |
|
415 |
* @return Ident node. |
|
416 |
*/ |
|
417 |
protected final IdentNode getIdent() { |
|
418 |
// Capture IDENT token. |
|
419 |
long identToken = token; |
|
420 |
||
421 |
if (isNonStrictModeIdent()) { |
|
422 |
// Fake out identifier. |
|
423 |
identToken = Token.recast(token, IDENT); |
|
424 |
// Get IDENT. |
|
425 |
final String ident = (String)getValue(identToken); |
|
426 |
||
427 |
next(); |
|
428 |
||
429 |
// Create IDENT node. |
|
26243 | 430 |
return createIdentNode(identToken, finish, ident).setIsFutureStrictName(); |
16147 | 431 |
} |
432 |
||
433 |
// Get IDENT. |
|
434 |
final String ident = (String)expectValue(IDENT); |
|
435 |
if (ident == null) { |
|
436 |
return null; |
|
437 |
} |
|
438 |
// Create IDENT node. |
|
26243 | 439 |
return createIdentNode(identToken, finish, ident); |
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* Creates a new {@link IdentNode} as if invoked with a {@link IdentNode#IdentNode(long, int, String) |
|
444 |
* constructor} but making sure that the {@code name} is deduplicated within this parse job. |
|
445 |
* @param identToken the token for the new {@code IdentNode} |
|
446 |
* @param identFinish the finish for the new {@code IdentNode} |
|
447 |
* @param name the name for the new {@code IdentNode}. It will be de-duplicated. |
|
448 |
* @return a newly constructed {@code IdentNode} with the specified token, finish, and name; the name will |
|
449 |
* be deduplicated. |
|
450 |
*/ |
|
451 |
protected IdentNode createIdentNode(final long identToken, final int identFinish, final String name) { |
|
452 |
final String existingName = canonicalNames.putIfAbsent(name, name); |
|
453 |
final String canonicalName = existingName != null ? existingName : name; |
|
454 |
return new IdentNode(identToken, identFinish, canonicalName); |
|
16147 | 455 |
} |
456 |
||
457 |
/** |
|
458 |
* Check if current token is in identifier name |
|
459 |
* |
|
460 |
* @return true if current token is an identifier name |
|
461 |
*/ |
|
462 |
protected final boolean isIdentifierName() { |
|
463 |
final TokenKind kind = type.getKind(); |
|
464 |
if (kind == TokenKind.KEYWORD || kind == TokenKind.FUTURE || kind == TokenKind.FUTURESTRICT) { |
|
465 |
return true; |
|
466 |
} |
|
31198
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
467 |
|
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
468 |
// only literals allowed are null, false and true |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
469 |
if (kind == TokenKind.LITERAL) { |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
470 |
switch (type) { |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
471 |
case FALSE: |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
472 |
case NULL: |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
473 |
case TRUE: |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
474 |
return true; |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
475 |
default: |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
476 |
return false; |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
477 |
} |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
478 |
} |
f807ce35a661
8098847: obj."prop" and obj.'prop' should result in SyntaxError
sundar
parents:
26887
diff
changeset
|
479 |
|
16147 | 480 |
// Fake out identifier. |
481 |
final long identToken = Token.recast(token, IDENT); |
|
482 |
// Get IDENT. |
|
483 |
final String ident = (String)getValue(identToken); |
|
484 |
return !ident.isEmpty() && Character.isJavaIdentifierStart(ident.charAt(0)); |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Create an IdentNode from the current token |
|
489 |
* |
|
490 |
* @return an IdentNode representing the current token |
|
491 |
*/ |
|
492 |
protected final IdentNode getIdentifierName() { |
|
493 |
if (type == IDENT) { |
|
494 |
return getIdent(); |
|
495 |
} else if (isIdentifierName()) { |
|
496 |
// Fake out identifier. |
|
497 |
final long identToken = Token.recast(token, IDENT); |
|
498 |
// Get IDENT. |
|
499 |
final String ident = (String)getValue(identToken); |
|
500 |
next(); |
|
501 |
// Create IDENT node. |
|
26243 | 502 |
return createIdentNode(identToken, finish, ident); |
16147 | 503 |
} else { |
504 |
expect(IDENT); |
|
505 |
return null; |
|
506 |
} |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Create a LiteralNode from the current token |
|
511 |
* |
|
512 |
* @return LiteralNode representing the current token |
|
513 |
* @throws ParserException if any literals fails to parse |
|
514 |
*/ |
|
515 |
protected final LiteralNode<?> getLiteral() throws ParserException { |
|
516 |
// Capture LITERAL token. |
|
517 |
final long literalToken = token; |
|
518 |
||
519 |
// Create literal node. |
|
520 |
final Object value = getValue(); |
|
16153 | 521 |
// Advance to have a correct finish |
522 |
next(); |
|
16147 | 523 |
|
524 |
LiteralNode<?> node = null; |
|
525 |
||
526 |
if (value == null) { |
|
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17518
diff
changeset
|
527 |
node = LiteralNode.newInstance(literalToken, finish); |
16147 | 528 |
} else if (value instanceof Number) { |
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17518
diff
changeset
|
529 |
node = LiteralNode.newInstance(literalToken, finish, (Number)value); |
16147 | 530 |
} else if (value instanceof String) { |
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17518
diff
changeset
|
531 |
node = LiteralNode.newInstance(literalToken, finish, (String)value); |
16147 | 532 |
} else if (value instanceof LexerToken) { |
533 |
if (value instanceof RegexToken) { |
|
534 |
final RegexToken regex = (RegexToken)value; |
|
535 |
try { |
|
16258 | 536 |
RegExpFactory.validate(regex.getExpression(), regex.getOptions()); |
16147 | 537 |
} catch (final ParserException e) { |
17233 | 538 |
throw error(e.getMessage()); |
16147 | 539 |
} |
540 |
} |
|
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17518
diff
changeset
|
541 |
node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value); |
16147 | 542 |
} else { |
543 |
assert false : "unknown type for LiteralNode: " + value.getClass(); |
|
544 |
} |
|
545 |
||
546 |
return node; |
|
547 |
} |
|
548 |
} |