Java Code Examples for jdk.nashorn.internal.runtime.Context#ThrowErrorManager

The following examples show how to use jdk.nashorn.internal.runtime.Context#ThrowErrorManager . 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: 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 2
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 3
Source File: NashornScriptEngine.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) {
    this.factory = factory;
    final Options options = new Options("nashorn");
    options.process(args);

    // throw ParseException on first error from script
    final ErrorManager errMgr = new Context.ThrowErrorManager();
    // create new Nashorn Context
    this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
        @Override
        public Context run() {
            try {
                return new Context(options, errMgr, appLoader);
            } catch (final RuntimeException e) {
                if (Context.DEBUG) {
                    e.printStackTrace();
                }
                throw e;
            }
        }
    }, CREATE_CONTEXT_ACC_CTXT);

    // cache this option that is used often
    this._global_per_engine = nashornContext.getEnv()._global_per_engine;

    // create new global object
    this.global = createNashornGlobal(context);
    // set the default ENGINE_SCOPE object for the default context
    context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
}
 
Example 4
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 5
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 6
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 7
Source File: NashornScriptEngine.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) {
    this.factory = factory;
    final Options options = new Options("nashorn");
    options.process(args);

    // throw ParseException on first error from script
    final ErrorManager errMgr = new Context.ThrowErrorManager();
    // create new Nashorn Context
    this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
        @Override
        public Context run() {
            try {
                return new Context(options, errMgr, appLoader);
            } catch (final RuntimeException e) {
                if (Context.DEBUG) {
                    e.printStackTrace();
                }
                throw e;
            }
        }
    }, CREATE_CONTEXT_ACC_CTXT);

    // cache this option that is used often
    this._global_per_engine = nashornContext.getEnv()._global_per_engine;

    // create new global object
    this.global = createNashornGlobal(context);
    // set the default ENGINE_SCOPE object for the default context
    context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
}
 
Example 8
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 9
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 10
Source File: NashornScriptEngine.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) {
    this.factory = factory;
    final Options options = new Options("nashorn");
    options.process(args);

    // throw ParseException on first error from script
    final ErrorManager errMgr = new Context.ThrowErrorManager();
    // create new Nashorn Context
    this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
        @Override
        public Context run() {
            try {
                return new Context(options, errMgr, appLoader);
            } catch (final RuntimeException e) {
                if (Context.DEBUG) {
                    e.printStackTrace();
                }
                throw e;
            }
        }
    }, CREATE_CONTEXT_ACC_CTXT);

    // cache this option that is used often
    this._global_per_engine = nashornContext.getEnv()._global_per_engine;

    // create new global object
    this.global = createNashornGlobal(context);
    // set the default ENGINE_SCOPE object for the default context
    context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
}
 
Example 11
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 12
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 13
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 14
Source File: JSONWriter.java    From TencentKona-8 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 15
Source File: NativeFunction.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static Parser getParser(final String sourceText) {
    final ScriptEnvironment env = Global.getEnv();
    return new Parser(env, sourceFor("<function>", sourceText), new Context.ThrowErrorManager(), env._strict, null);
}
 
Example 16
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private jdk.nashorn.internal.parser.Parser makeParser(final Source source, final DiagnosticListener listener) {
    final ErrorManager errMgr = listener != null ? new ListenerErrorManager(listener) : new Context.ThrowErrorManager();
    return new jdk.nashorn.internal.parser.Parser(env, source, errMgr);
}
 
Example 17
Source File: NativeFunction.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static Parser getParser(final String sourceText) {
    final ScriptEnvironment env = Global.getEnv();
    return new Parser(env, sourceFor("<function>", sourceText), new Context.ThrowErrorManager(), env._strict, null);
}
 
Example 18
Source File: NativeFunction.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static Parser getParser(final String sourceText) {
    final ScriptEnvironment env = Global.getEnv();
    return new Parser(env, sourceFor("<function>", sourceText), new Context.ThrowErrorManager(), env._strict, null);
}
 
Example 19
Source File: NativeFunction.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static Parser getParser(final String sourceText) {
    final ScriptEnvironment env = Global.getEnv();
    return new Parser(env, sourceFor("<function>", sourceText), new Context.ThrowErrorManager(), env._strict, null);
}
 
Example 20
Source File: NativeFunction.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static Parser getParser(final String sourceText) {
    final ScriptEnvironment env = Global.getEnv();
    return new Parser(env, sourceFor("<function>", sourceText), new Context.ThrowErrorManager(), env._strict, null);
}