/** * Checks the parsed file against the API Map. * * @param p * an instance of {@link JavaScriptParser} to get the tree representation of it * @param jsFileName * the JavaScript file name that is being checked * @throws IOException */ private void checkJS(JavaScriptParser p, String jsFileName) throws IOException { ScriptOrFnNode nodeTree = p.parse(); for (Node cursor = nodeTree.getFirstChild(); cursor != null; cursor = cursor.getNext()) { StringBuffer sb = new StringBuffer(); if (cursor.getType() == Token.FUNCTION) { int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP); FunctionNode fn = nodeTree.getFunctionNode(fnIndex); sb.append("FUNCTION: " + fn.getFunctionName()); } else if (cursor.getType() == Token.VAR) { Node vn = cursor.getFirstChild(); sb.append("VAR: " + vn.getString()); } apiMap.remove(jsFileName + sb); } }
/** * @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; }
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; }