/** * Compiles {@code source} and returns the transformed and optimized * {@link ScriptNode} */ protected ScriptNode compile(CharSequence source) { final String mainMethodClassName = "Main"; final String scriptClassName = "Main"; CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.initFromContext(cx); ErrorReporter compilationErrorReporter = compilerEnv .getErrorReporter(); Parser p = new Parser(compilerEnv, compilationErrorReporter); AstRoot ast = p.parse(source.toString(), "<eval>", 1); IRFactory irf = new IRFactory(compilerEnv); ScriptNode tree = irf.transformTree(ast); Codegen codegen = new Codegen(); codegen.setMainMethodClass(mainMethodClassName); codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(), false); return tree; }
/** * @return a tree representation of the parsed JavaScript file * @throws IOException */ public ScriptOrFnNode parse() throws IOException { if (nodeTree == null) { Reader reader = new FileReader(jsFile); CompilerEnvirons compilerEnv = new CompilerEnvirons(); ErrorReporter errorReporter = compilerEnv.getErrorReporter(); Parser parser = new Parser(compilerEnv, errorReporter); String sourceURI; try { sourceURI = jsFile.getCanonicalPath(); } catch (IOException e) { sourceURI = jsFile.toString(); } nodeTree = parser.parse(reader, sourceURI, 1); } return nodeTree; }
/** * Scan the given file for class definitions and accumulate dependencies. */ private void scan(final File source) throws IOException { log.debug("Scanning: " + source); ErrorReporter errorReporter = new LogErrorReporter(log); CompilerEnvirons env = new CompilerEnvirons(); env.setErrorReporter(errorReporter); Parser parser = new Parser(env, errorReporter); Reader reader = new BufferedReader(new FileReader(source)); try { AstRoot root = parser.parse(reader, source.getAbsolutePath(), 0); DependencyAccumulator visitor = new DependencyAccumulator(source); root.visit(visitor); // complain if no def was found in this source if (visitor.current == null) { log.warn("No class definition was found while processing: " + source); } } finally { reader.close(); } }
/** * Creates options for Rhino based off of the user's preferences. * * @param errorHandler The container for errors found while parsing. * @return The properties for the JS compiler to use. */ public static CompilerEnvirons createCompilerEnvironment(ErrorReporter errorHandler, JavaScriptLanguageSupport langSupport) { CompilerEnvirons env = new CompilerEnvirons(); env.setErrorReporter(errorHandler); env.setIdeMode(true); env.setRecordingComments(true); env.setRecordingLocalJsDocComments(true); env.setRecoverFromErrors(true); if(langSupport != null) { env.setXmlAvailable(langSupport.isXmlAvailable()); env.setStrictMode(langSupport.isStrictMode()); int version = langSupport.getLanguageVersion(); if (version > 0) { Logger.log("[JavaScriptParser]: JS language version set to: " + version); env.setLanguageVersion(version); } } return env; }
private AstRoot parse(CharSequence cs) { CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.initFromContext(cx); ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter(); Parser p = new Parser(compilerEnv, compilationErrorReporter); return p.parse(cs.toString(), "<eval>", 1); }
protected static AstRoot parseIntoTree(String formula) { Context cx = Context.enter(); CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.initFromContext(cx); ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter(); Parser parser = new Parser(compilerEnv, compilationErrorReporter); AstRoot tree = parser.parse(formula, "(formula being checked by unit-checker)", 0); Context.exit(); return tree; }
public static ScriptOrFnNode parseVariables(Context cx, Scriptable scope, String source, String sourceName, int lineno, Object securityDomain){ // Interpreter compiler = new Interpreter(); CompilerEnvirons evn = new CompilerEnvirons(); //evn.setLanguageVersion(Context.VERSION_1_5); evn.setOptimizationLevel(-1); evn.setGeneratingSource(true); evn.setGenerateDebugInfo(true); ErrorReporter errorReporter = new ToolErrorReporter(false); Parser p = new Parser(evn, errorReporter); ScriptOrFnNode tree = p.parse(source, "",0); // IOException new NodeTransformer().transform(tree); //Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null); return tree; }
@Override public String process(final String filename, final String source, final Config conf) throws Exception { ErrorReporter reporter = reporter(log, filename, name()); JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(source), reporter); StringWriter out = new StringWriter(); compressor.compress(out, get("linebreakpos"), get("munge"), get("verbose"), get("preserve-semi"), get("disable-optimizations")); return out.toString(); }