jdk.nashorn.internal.runtime.ParserException Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ParserException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 7 votes vote down vote up
private void handleParseException(final Exception e) {
    // Extract message from exception.  The message will be in error
    // message format.
    String message = e.getMessage();

    // If empty message.
    if (message == null) {
        message = e.toString();
    }

    // Issue message.
    if (e instanceof ParserException) {
        errors.error((ParserException)e);
    } else {
        errors.error(message);
    }

    if (env._dump_on_error) {
        e.printStackTrace(env.getErr());
    }
}
 
Example #2
Source File: Parser.java    From TencentKona-8 with GNU General Public License v2.0 7 votes vote down vote up
private void handleParseException(final Exception e) {
    // Extract message from exception.  The message will be in error
    // message format.
    String message = e.getMessage();

    // If empty message.
    if (message == null) {
        message = e.toString();
    }

    // Issue message.
    if (e instanceof ParserException) {
        errors.error((ParserException)e);
    } else {
        errors.error(message);
    }

    if (env._dump_on_error) {
        e.printStackTrace(env.getErr());
    }
}
 
Example #3
Source File: AssignSymbols.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void throwParserException(final String message, final Node origin) {
    if (origin == null) {
        throw new ParserException(message);
    }
    final Source source = compiler.getSource();
    final long token = origin.getToken();
    final int line = source.getLine(origin.getStart());
    final int column = source.getColumn(origin.getStart());
    final String formatted = ErrorManager.format(message, source, line, column, token);
    throw new ParserException(JSErrorType.SYNTAX_ERROR, formatted, source, line, column, token);
}
 
Example #4
Source File: NativeFunction.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionParameters(final String params) {
    final Parser parser = getParser(params);
    try {
        parser.parseFormalParameterList();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #5
Source File: JdkRegExp.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code source} and {@code flags} strings.
 *
 * @param source RegExp source string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or source string has syntax error.
 */
public JdkRegExp(final String source, final String flags) throws ParserException {
    super(source, flags);

    int intFlags = 0;

    if (isIgnoreCase()) {
        intFlags |= CASE_INSENSITIVE | UNICODE_CASE;
    }
    if (isMultiline()) {
        intFlags |= MULTILINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(source);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(source, intFlags);
            throw e;
        }

        if (parsed != null) {
            this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException e2) {
        throwParserException("syntax", e2.getMessage());
    } catch (StackOverflowError e3) {
        throw new RuntimeException(e3);
    }
}
 
Example #6
Source File: AntlrParserDriver.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void parseAndWalkTokenStream(TokenSubStream tokens)
    throws ParserException {
  filename = "";
  stream = tokens;
  ParseTree tree = parseTokenStream(tokens);
  walkTree(tree);
}
 
Example #7
Source File: AssignSymbols.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void throwParserException(final String message, final Node origin) {
    if (origin == null) {
        throw new ParserException(message);
    }
    final Source source = compiler.getSource();
    final long token = origin.getToken();
    final int line = source.getLine(origin.getStart());
    final int column = source.getColumn(origin.getStart());
    final String formatted = ErrorManager.format(message, source, line, column, token);
    throw new ParserException(JSErrorType.SYNTAX_ERROR, formatted, source, line, column, token);
}
 
Example #8
Source File: AbstractParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #9
Source File: AbstractParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the value of a specific token
 *
 * @param valueToken the token
 *
 * @return JavaScript value of the token
 */
protected final Object getValue(final long valueToken) {
    try {
        return lexer.getValueOf(valueToken, isStrictMode);
    } catch (final ParserException e) {
        errors.error(e);
    }

    return null;
}
 
Example #10
Source File: AbstractParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #11
Source File: AbstractParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Report an error.
 *
 * @param errorType  The error type
 * @param message    Error 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 String formatted = ErrorManager.format(message, source, line, column, token);
    return new ParserException(errorType, formatted, source, line, column, token);
}
 
Example #12
Source File: NativeFunction.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Parser parser = getParser(funcBody);
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #13
Source File: AbstractParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #14
Source File: JdkRegExp.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code source} and {@code flags} strings.
 *
 * @param source RegExp source string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or source string has syntax error.
 */
public JdkRegExp(final String source, final String flags) throws ParserException {
    super(source, flags);

    int intFlags = 0;

    if (isIgnoreCase()) {
        intFlags |= CASE_INSENSITIVE | UNICODE_CASE;
    }
    if (isMultiline()) {
        intFlags |= MULTILINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(source);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(source, intFlags);
            throw e;
        }

        if (parsed != null) {
            this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException e2) {
        throwParserException("syntax", e2.getMessage());
    } catch (StackOverflowError e3) {
        throw new RuntimeException(e3);
    }
}
 
Example #15
Source File: AbstractParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #16
Source File: AbstractParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Report an error.
 *
 * @param errorType  The error type
 * @param message    Error 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 String formatted = ErrorManager.format(message, source, line, column, token);
    return new ParserException(errorType, formatted, source, line, column, token);
}
 
Example #17
Source File: JdkRegExp.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code source} and {@code flags} strings.
 *
 * @param source RegExp source string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or source string has syntax error.
 */
public JdkRegExp(final String source, final String flags) throws ParserException {
    super(source, flags);

    int intFlags = 0;

    if (isIgnoreCase()) {
        intFlags |= CASE_INSENSITIVE | UNICODE_CASE;
    }
    if (isMultiline()) {
        intFlags |= MULTILINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(source);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(source, intFlags);
            throw e;
        }

        if (parsed != null) {
            this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException e2) {
        throwParserException("syntax", e2.getMessage());
    }
}
 
Example #18
Source File: JoniRegExp.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code pattern} and {@code flags} strings.
 *
 * @param pattern RegExp pattern string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or pattern string has syntax error.
 */
public JoniRegExp(final String pattern, final String flags) throws ParserException {
    super(pattern, flags);

    int option = Option.SINGLELINE;

    if (this.isIgnoreCase()) {
        option |= Option.IGNORECASE;
    }
    if (this.isMultiline()) {
        option &= ~Option.SINGLELINE;
        option |= Option.NEGATE_SINGLELINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(pattern);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(pattern, 0);
            throw e;
        }

        if (parsed != null) {
            final char[] javaPattern = parsed.getJavaPattern().toCharArray();
            this.regex = new Regex(javaPattern, 0, javaPattern.length, option, Syntax.JAVASCRIPT);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException | JOniException e2) {
        throwParserException("syntax", e2.getMessage());
    } catch (StackOverflowError e3) {
        throw new RuntimeException(e3);
    }
}
 
Example #19
Source File: JoniRegExp.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code pattern} and {@code flags} strings.
 *
 * @param pattern RegExp pattern string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or pattern string has syntax error.
 */
public JoniRegExp(final String pattern, final String flags) throws ParserException {
    super(pattern, flags);

    int option = Option.SINGLELINE;

    if (this.isIgnoreCase()) {
        option |= Option.IGNORECASE;
    }
    if (this.isMultiline()) {
        option &= ~Option.SINGLELINE;
        option |= Option.NEGATE_SINGLELINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(pattern);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(pattern, 0);
            throw e;
        }

        if (parsed != null) {
            final char[] javaPattern = parsed.getJavaPattern().toCharArray();
            this.regex = new Regex(javaPattern, 0, javaPattern.length, option, Syntax.JAVASCRIPT);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException | JOniException e2) {
        throwParserException("syntax", e2.getMessage());
    }
}
 
Example #20
Source File: NativeRegExp.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
NativeRegExp(final String input, final String flagString, final Global global, final ScriptObject proto) {
    super(proto, $nasgenmap$);
    try {
        this.regexp = RegExpFactory.create(input, flagString);
    } catch (final ParserException e) {
        // translate it as SyntaxError object and throw it
        e.throwAsEcmaException();
        throw new AssertionError(); //guard against null warnings below
    }
    this.globalObject = global;
    this.setLastIndex(0);
}
 
Example #21
Source File: AbstractParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the value of a specific token
 *
 * @param valueToken the token
 *
 * @return JavaScript value of the token
 */
protected final Object getValue(final long valueToken) {
    try {
        return lexer.getValueOf(valueToken, isStrictMode);
    } catch (final ParserException e) {
        errors.error(e);
    }

    return null;
}
 
Example #22
Source File: AbstractParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Report an error.
 *
 * @param errorType  The error type
 * @param message    Error 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 String formatted = ErrorManager.format(message, source, line, column, token);
    return new ParserException(errorType, formatted, source, line, column, token);
}
 
Example #23
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void throwParserException(final String message, final Node origin) {
    if (origin == null) {
        throw new ParserException(message);
    }
    final Source source = compiler.getSource();
    final long token = origin.getToken();
    final int line = source.getLine(origin.getStart());
    final int column = source.getColumn(origin.getStart());
    final String formatted = ErrorManager.format(message, source, line, column, token);
    throw new ParserException(JSErrorType.SYNTAX_ERROR, formatted, source, line, column, token);
}
 
Example #24
Source File: JdkRegExp.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Regular expression from the given {@code source} and {@code flags} strings.
 *
 * @param source RegExp source string
 * @param flags RegExp flag string
 * @throws ParserException if flags is invalid or source string has syntax error.
 */
public JdkRegExp(final String source, final String flags) throws ParserException {
    super(source, flags);

    int intFlags = 0;

    if (isIgnoreCase()) {
        intFlags |= CASE_INSENSITIVE | UNICODE_CASE;
    }
    if (isMultiline()) {
        intFlags |= MULTILINE;
    }

    try {
        RegExpScanner parsed;

        try {
            parsed = RegExpScanner.scan(source);
        } catch (final PatternSyntaxException e) {
            // refine the exception with a better syntax error, if this
            // passes, just rethrow what we have
            Pattern.compile(source, intFlags);
            throw e;
        }

        if (parsed != null) {
            this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags);
            this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
        }
    } catch (final PatternSyntaxException e2) {
        throwParserException("syntax", e2.getMessage());
    }
}
 
Example #25
Source File: NativeRegExp.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
NativeRegExp(final String input, final String flagString, final Global global, final ScriptObject proto) {
    super(proto, $nasgenmap$);
    try {
        this.regexp = RegExpFactory.create(input, flagString);
    } catch (final ParserException e) {
        // translate it as SyntaxError object and throw it
        e.throwAsEcmaException();
        throw new AssertionError(); //guard against null warnings below
    }
    this.globalObject = global;
    this.setLastIndex(0);
}
 
Example #26
Source File: NativeFunction.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Parser parser = getParser(funcBody);
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #27
Source File: AbstractParser.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #28
Source File: NativeFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Parser parser = getParser(funcBody);
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #29
Source File: NativeRegExp.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
NativeRegExp(final String input, final String flagString, final Global global, final ScriptObject proto) {
    super(proto, $nasgenmap$);
    try {
        this.regexp = RegExpFactory.create(input, flagString);
    } catch (final ParserException e) {
        // translate it as SyntaxError object and throw it
        e.throwAsEcmaException();
        throw new AssertionError(); //guard against null warnings below
    }
    this.globalObject = global;
    this.setLastIndex(0);
}
 
Example #30
Source File: AbstractParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check next token, get its value and advance.
 *
 * @param  expected Expected tokenType.
 * @return The JavaScript value of the token
 * @throws ParserException on unexpected token type
 */
protected final Object expectValue(final TokenType expected) throws ParserException {
    if (type != expected) {
        throw error(expectMessage(expected));
    }

    final Object value = getValue();

    next();

    return value;
}