Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void error(final ParserException e)
// Reset state and fall back to non-strict script as our goal
working.getErrors().clear();
fParser = new CommentCollectingParser(env, src, errorManager);
result = fParser.parse(filename, startOffset, source.length() - startOffset, false);
result = fParser.parse(filename, startOffset, source.length() - startOffset, 0);
}

// If any errors found, will run in a simple recovery mode where will assume basic expected tokens({, },IDENT, (,)) are available and proceed with the parse without failing.
Expand All @@ -152,7 +152,7 @@ public void error(final ParserException e)
// considered in the recovery mode
inRecoveryMode[0] = true;
fParser = new CommentCollectingParser(env, src, errorManager, true);
result = fParser.parse(filename, startOffset, source.length() - startOffset, false);
result = fParser.parse(filename, startOffset, source.length() - startOffset, 0);

}
return result;
Expand Down Expand Up @@ -255,18 +255,6 @@ protected void expectDontAdvance(TokenType expected) throws ParserException
}
}

private void insertToken(TokenType expectedTokenType)
{
long expectedNewToken = Token.toDesc(expectedTokenType, linePosition,
expectedTokenType.getName() != null ? expectedTokenType.getLength() : 0);

// insert the token that is expected before the unexpected token
stream.insert(k, expectedNewToken);
token = expectedNewToken;
k--;

}

}

}
1 change: 1 addition & 0 deletions bundles/com.oracle.js.parser/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: com.oracle.js.parser,
com.oracle.js.parser.ir,
com.oracle.js.parser.ir.visitor
Require-Bundle: org.graalvm.collections;visibility:=reexport
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* The Universal Permissive License (UPL), Version 1.0
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
* (a) the Software, and
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.oracle.js.parser;
Expand All @@ -31,8 +47,8 @@
import static com.oracle.js.parser.TokenType.EOL;
import static com.oracle.js.parser.TokenType.IDENT;

import java.util.HashMap;
import java.util.Map;
import java.math.BigInteger;

import java.util.function.Function;

import com.oracle.js.parser.Lexer.LexerToken;
Expand Down Expand Up @@ -85,14 +101,9 @@ public abstract class AbstractParser {
/** Is this parser running under strict mode? */
protected boolean isStrictMode;

/** Is this parser running under strong mode? */
protected boolean isStrongMode;

/** What should line numbers be counted from? */
protected final int lineOffset;

private final Map<String, String> canonicalNames = new HashMap<>();

/**
* Construct a parser.
*
Expand Down Expand Up @@ -204,17 +215,16 @@ private void checkDirectiveComment() {
* @return tokenType of next token.
*/
protected TokenType nextToken() {
// Capture last token type, but ignore comments (which are irrelevant for the purpose of
// newline detection).
if (type != COMMENT) {
last = type;
}
if (type != EOF) {

// Set up next token.
k++;
final long lastToken = token;
previousToken = token;
// Capture last token type, but ignore comments (which are irrelevant for the purpose of
// newline detection).
if (type != COMMENT) {
last = type;
previousToken = token;
}
token = getToken(k);
type = Token.descType(token);

Expand Down Expand Up @@ -292,9 +302,8 @@ protected final ParserException error(final String message) {
* @return ParserException upon failure. Caller should throw and not ignore
*/
protected final ParserException error(final JSErrorType errorType, final String message) {
// TODO - column needs to account for tabs.
final int position = Token.descPosition(token);
final int column = position - linePosition;
final int column = source.getColumn(position);
final String formatted = ErrorManager.format(message, source, line, column, token);
return new ParserException(errorType, formatted, source, line, column, token);
}
Expand Down Expand Up @@ -331,6 +340,12 @@ protected final String expectMessage(final TokenType expected) {
return msg;
}

protected final String expectMessage(final TokenType expected, final long errorToken) {
final String expectedName = expected.getNameOrType();
final String tokenString = Token.toString(source, errorToken);
return AbstractParser.message("expected", expectedName, tokenString);
}

/**
* Check current token and advance to the next token.
*
Expand All @@ -357,22 +372,18 @@ protected void expectDontAdvance(final TokenType expected) throws ParserExceptio
}

/**
* Check next token, get its value and advance.
* Get the value of the current token. If the current token contains an escape sequence, the
* method does not attempt to convert it.
*
* @param expected Expected tokenType.
* @return The JavaScript value of the token
* @throws ParserException on unexpected token type
* @return JavaScript value of the token.
*/
protected final Object expectValue(final TokenType expected) throws ParserException {
if (type != expected) {
throw error(expectMessage(expected));
protected final Object getValueNoEscape() {
try {
return lexer.getValueOf(token, isStrictMode, false);
} catch (final ParserException e) {
errors.error(e);
}

final Object value = getValue();

next();

return value;
return null;
}

/**
Expand Down Expand Up @@ -420,25 +431,22 @@ protected final IdentNode getIdent() {
// Capture IDENT token.
long identToken = token;

if (isNonStrictModeIdent()) {
// Fake out identifier.
identToken = Token.recast(token, IDENT);
// Get IDENT.
if (type == IDENT) {
final String ident = (String) getValue(identToken);

next();

// Create IDENT node.
return createIdentNode(identToken, finish, ident).setIsFutureStrictName();
}
return createIdentNode(identToken, finish, ident);
} else if (type.isContextualKeyword() || isNonStrictModeIdent()) {
final String ident = type.getName();

// Get IDENT.
final String ident = (String) expectValue(IDENT);
if (ident == null) {
return null;
next();

return new IdentNode(identToken, finish, ident);
} else {
// Not an IDENT.
throw error(expectMessage(IDENT));
}
// Create IDENT node.
return createIdentNode(identToken, finish, ident);
}

/**
Expand All @@ -453,9 +461,16 @@ protected final IdentNode getIdent() {
* name will be deduplicated.
*/
protected IdentNode createIdentNode(final long identToken, final int identFinish, final String name) {
final String existingName = canonicalNames.putIfAbsent(name, name);
final String canonicalName = existingName != null ? existingName : name;
return new IdentNode(identToken, identFinish, canonicalName);
assert isInterned(name) : name;
return new IdentNode(identToken, identFinish, name);
}

private boolean isInterned(final String name) {
return isSame(lexer.stringIntern(name), name) || isSame(name.intern(), name);
}

private static boolean isSame(Object a, Object b) {
return a == b;
}

/**
Expand All @@ -464,14 +479,25 @@ protected IdentNode createIdentNode(final long identToken, final int identFinish
* @return true if current token is an identifier name
*/
protected final boolean isIdentifierName() {
final TokenKind kind = type.getKind();
if (kind == TokenKind.KEYWORD || kind == TokenKind.FUTURE || kind == TokenKind.FUTURESTRICT) {
return isIdentifierName(token);
}

/**
* Check if token is an identifier name
*
* @return true if token is an identifier name
*/
protected final boolean isIdentifierName(long currentToken) {
final TokenType currentType = Token.descType(currentToken);
assert currentType != IDENT; // handled before
final TokenKind kind = currentType.getKind();
if (kind == TokenKind.KEYWORD || kind == TokenKind.FUTURE || kind == TokenKind.FUTURESTRICT || kind == TokenKind.CONTEXTUAL) {
return true;
}

// only literals allowed are null, false and true
if (kind == TokenKind.LITERAL) {
switch (type) {
switch (currentType) {
case FALSE:
case NULL:
case TRUE:
Expand All @@ -482,7 +508,7 @@ protected final boolean isIdentifierName() {
}

// Fake out identifier.
final long identToken = Token.recast(token, IDENT);
final long identToken = Token.recast(currentToken, IDENT);
// Get IDENT.
final String ident = (String) getValue(identToken);
return !ident.isEmpty() && Character.isJavaIdentifierStart(ident.charAt(0));
Expand All @@ -505,11 +531,8 @@ protected final IdentNode getIdentifierName() {
// Create IDENT node.
return createIdentNode(identToken, finish, ident);
} else {
expectDontAdvance(IDENT);

// Fake out identifier.
final long identToken = Token.recast(token, IDENT);
return createIdentNode(identToken, finish, "");
expect(IDENT);
return null;
}
}

Expand All @@ -532,6 +555,8 @@ protected final LiteralNode<?> getLiteral() throws ParserException {

if (value == null) {
node = LiteralNode.newInstance(literalToken, finish);
} else if (value instanceof BigInteger) {
node = LiteralNode.newInstance(literalToken, finish, (BigInteger) value);
} else if (value instanceof Number) {
node = LiteralNode.newInstance(literalToken, finish, (Number) value, getNumberToStringConverter());
} else if (value instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* The Universal Permissive License (UPL), Version 1.0
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
* (a) the Software, and
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.oracle.js.parser;
Expand Down
Loading