jdk.nashorn.internal.parser.Parser Java Examples

The following examples show how to use jdk.nashorn.internal.parser.Parser. 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: NativeFunction.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionParameters(final String params) {
    final Source src = new Source("<function>", params);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFormalParameterList();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #2
Source File: NativeFunction.java    From jdk8u60 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 #3
Source File: JSONWriter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #4
Source File: NativeFunction.java    From openjdk-jdk8u-backup 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 #5
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 #6
Source File: JSONWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #7
Source File: RecompilableScriptFunctionData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
FunctionNode reparse() {
    if (isSerialized()) {
        return deserialize();
    }

    final int descPosition = Token.descPosition(token);
    final Context context = Context.getContextTrusted();
    final Parser parser = new Parser(
        context.getEnv(),
        source,
        new Context.ThrowErrorManager(),
        isStrict(),
        // source starts at line 0, so even though lineNumber is the correct declaration line, back off
        // one to make it exclusive
        lineNumber - 1,
        context.getLogger(Parser.class));

    if (getFunctionFlag(FunctionNode.IS_ANONYMOUS)) {
        parser.setFunctionName(functionName);
    }
    parser.setReparsedFunction(this);

    final FunctionNode program = parser.parse(CompilerConstants.PROGRAM.symbolName(), descPosition,
            Token.descLength(token), isPropertyAccessor());
    // Parser generates a program AST even if we're recompiling a single function, so when we are only
    // recompiling a single function, extract it from the program.
    return (isProgram() ? program : extractFunctionFromScript(program)).setName(null, functionName);
}
 
Example #8
Source File: JSONWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #9
Source File: NativeFunction.java    From openjdk-jdk9 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 #10
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 #11
Source File: JSONWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #12
Source File: Shell.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse potentially partial code and keep track of the start of last expression.
 * This 'partial' parsing support is meant to be used for code-completion.
 *
 * @param context the nashorn context
 * @param code code that is to be parsed
 * @return the start index of the last expression parsed in the (incomplete) code.
 */
@Override
public final int getLastExpressionStart(final Context context, final String code) {
    final int[] exprStart = { -1 };

    final Parser p = new Parser(context.getEnv(), sourceFor("<partial_code>", code),new Context.ThrowErrorManager()) {
        @Override
        protected Expression expression() {
            exprStart[0] = this.start;
            return super.expression();
        }

        @Override
        protected Expression assignmentExpression(final boolean noIn) {
            exprStart[0] = this.start;
            return super.assignmentExpression(noIn);
        }
    };

    try {
        p.parse();
    } catch (final Exception ignored) {
        // throw any parser exception, but we are partial parsing anyway
    }

    return exprStart[0];
}
 
Example #13
Source File: NativeFunction.java    From hottub 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 #14
Source File: NativeFunction.java    From hottub 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 #15
Source File: JSONWriter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #16
Source File: NativeFunction.java    From TencentKona-8 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 #17
Source File: NativeFunction.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Source src = new Source("<function>", funcBody);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #18
Source File: JSONWriter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param env  script environment to use
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.symbolName());
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #19
Source File: Shell.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the given script files in the command line
 *
 * @param context the nashorn context
 * @param global the global scope
 * @param files the list of script files to compile
 *
 * @return error code
 * @throws IOException when any script file read results in I/O error
 */
private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();
    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }
        final ErrorManager errors = context.getErrorManager();

        // For each file on the command line.
        for (final String fileName : files) {
            final FunctionNode functionNode = new Parser(env, new Source(fileName, new File(fileName)), errors).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            if (env._print_ast) {
                context.getErr().println(new ASTWriter(functionNode));
            }

            if (env._print_parse) {
                context.getErr().println(new PrintVisitor(functionNode));
            }

            //null - pass no code installer - this is compile only
            new Compiler(env).compile(functionNode);
        }
    } finally {
        env.getOut().flush();
        env.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}
 
Example #20
Source File: NativeFunction.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionParameters(final String params) {
    final Source src = new Source("<function>", params);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFormalParameterList();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #21
Source File: NativeFunction.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Source src = new Source("<function>", funcBody);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #22
Source File: JSONWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param env  script environment to use
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.symbolName());
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #23
Source File: Shell.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the given script files in the command line
 *
 * @param context the nashorn context
 * @param global the global scope
 * @param files the list of script files to compile
 *
 * @return error code
 * @throws IOException when any script file read results in I/O error
 */
private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();
    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }
        final ErrorManager errors = context.getErrorManager();

        // For each file on the command line.
        for (final String fileName : files) {
            final FunctionNode functionNode = new Parser(env, new Source(fileName, new File(fileName)), errors).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            if (env._print_ast) {
                context.getErr().println(new ASTWriter(functionNode));
            }

            if (env._print_parse) {
                context.getErr().println(new PrintVisitor(functionNode));
            }

            //null - pass no code installer - this is compile only
            new Compiler(env).compile(functionNode);
        }
    } finally {
        env.getOut().flush();
        env.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}
 
Example #24
Source File: NativeFunction.java    From jdk8u_nashorn 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 #25
Source File: NativeFunction.java    From jdk8u_nashorn 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 #26
Source File: JSONWriter.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param context context
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class));
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #27
Source File: NativeFunction.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionParameters(final String params) {
    final Source src = new Source("<function>", params);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFormalParameterList();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #28
Source File: NativeFunction.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFunctionBody(final String funcBody) {
    final Source src = new Source("<function>", funcBody);
    final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
    try {
        parser.parseFunctionBody();
    } catch (final ParserException pe) {
        pe.throwAsEcmaException();
    }
}
 
Example #29
Source File: JSONWriter.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param env  script environment to use
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.symbolName());
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #30
Source File: Shell.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the given script files in the command line
 *
 * @param context the nashorn context
 * @param global the global scope
 * @param files the list of script files to compile
 *
 * @return error code
 * @throws IOException when any script file read results in I/O error
 */
private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();
    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }
        final ErrorManager errors = context.getErrorManager();

        // For each file on the command line.
        for (final String fileName : files) {
            final FunctionNode functionNode = new Parser(env, new Source(fileName, new File(fileName)), errors).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            if (env._print_ast) {
                context.getErr().println(new ASTWriter(functionNode));
            }

            if (env._print_parse) {
                context.getErr().println(new PrintVisitor(functionNode));
            }

            //null - pass no code installer - this is compile only
            new Compiler(env).compile(functionNode);
        }
    } finally {
        env.getOut().flush();
        env.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}