/** * Transforms the tree into a lower-level IR suitable for codegen. * Optionally generates the encoded source. */ public ScriptNode transformTree(AstRoot root) { currentScriptOrFn = root; this.inUseStrictDirective = root.isInStrictMode(); int sourceStartOffset = decompiler.getCurrentOffset(); if (Token.printTrees) { System.out.println("IRFactory.transformTree"); System.out.println(root.debugPrint()); } ScriptNode script = (ScriptNode)transform(root); int sourceEndOffset = decompiler.getCurrentOffset(); script.setEncodedSourceBounds(sourceStartOffset, sourceEndOffset); if (compilerEnv.isGeneratingSource()) { script.setEncodedSource(decompiler.getEncodedSource()); } decompiler = null; return script; }
/** * Builds a parse tree from the given source string. * * @return an {@link AstRoot} object representing the parsed program. If * the parse fails, {@code null} will be returned. (The parse failure will * result in a call to the {@link ErrorReporter} from * {@link CompilerEnvirons}.) */ public AstRoot parse(String sourceString, String sourceURI, int lineno) { if (parseFinished) throw new IllegalStateException("parser reused"); this.sourceURI = sourceURI; if (compilerEnv.isIdeMode()) { this.sourceChars = sourceString.toCharArray(); } this.ts = new TokenStream(this, null, sourceString, lineno); try { return parse(); } catch (IOException iox) { // Should never happen throw new IllegalStateException(); } finally { parseFinished = true; } }
/** * Builds a parse tree from the given sourcereader. * @see #parse(String,String,int) * @throws IOException if the {@link Reader} encounters an error */ public AstRoot parse(Reader sourceReader, String sourceURI, int lineno) throws IOException { if (parseFinished) throw new IllegalStateException("parser reused"); if (compilerEnv.isIdeMode()) { return parse(readFully(sourceReader), sourceURI, lineno); } try { this.sourceURI = sourceURI; ts = new TokenStream(this, sourceReader, null, lineno); return parse(); } finally { parseFinished = true; } }
private void transformCompilationUnit(ScriptNode tree) { loops = new ObjArray(); loopEnds = new ObjArray(); // to save against upchecks if no finally blocks are used. hasFinally = false; // Flatten all only if we are not using scope objects for block scope boolean createScopeObjects = tree.getType() != Token.FUNCTION || ((FunctionNode)tree).requiresActivation(); tree.flattenSymbolTable(!createScopeObjects); //uncomment to print tree before transformation if (Token.printTrees) System.out.println(tree.toStringTree(tree)); boolean inStrictMode = tree instanceof AstRoot && ((AstRoot)tree).isInStrictMode(); transformCompilationUnit_r(tree, tree, tree, createScopeObjects, inStrictMode); }
@Test public void testNodeReplacementInWhileLoopWithBrackets() throws IOException { String script = "var o = {\n" + " _x: 123, \n" + " get x() {\n" + " return this._x;\n" + " }\n" + ", \n" + " set x(value) {\n" + " this._x = value;\n" + " }\n" + "};\n"; Parser parser = new Parser(environment); AstRoot astRoot = parser.parse(new StringReader(script), null, 1); assertEquals(script, astRoot.toSource()); }
/** * 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); Parser p = new Parser(compilerEnv); 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; }
/** * 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; }
private static String explainErrors(JSEnvironment env, String sourceCode) { AstRoot root = new Parser().parse(sourceCode, "", 1); SatSolver sat = new Sat4J(); SJSTypeTheory theory = new SJSTypeTheory(env, null, root); List<Integer> hard = new ArrayList<>(); List<Integer> soft = new ArrayList<>(); List<ITypeConstraint> constraints = theory.getConstraints(); for (int i = 0; i < constraints.size(); ++i) { (theory.hackyGenerator().hasExplanation(constraints.get(i)) ? soft : hard).add(i); } Pair<TypeAssignment, Collection<Integer>> result = TheorySolver.solve( theory, new SatFixingSetFinder<>(sat), hard, soft); ConstraintGenerator g = theory.hackyGenerator(); StringBuilder buf = new StringBuilder(); for (int broken : result.getRight()) { ITypeConstraint c = theory.hackyConstraintAccess().get(broken); ByteArrayOutputStream stream = new ByteArrayOutputStream(); g.explainFailure(c, result.getLeft()).prettyprint(new PrintStream(stream)); buf.append(stream.toString()); } return buf.toString(); }
/** * 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(); } }
@Override public Bundle parse(InputStream inStream) throws IOException { LinkedHashMap<String, String> resultMap = null; Bundle result = new Bundle(); try (InputStreamReader reader = new InputStreamReader(new BomInputStream(inStream), "UTF-8")) { AstRoot root = new Parser().parse(reader, null, 1); KeyValueVisitor visitor = new KeyValueVisitor(); root.visitAll(visitor); resultMap = visitor.elements; int sequenceNum = 1; for (Entry<String, String> entry : resultMap.entrySet()) { result.addResourceString(entry.getKey(), entry.getValue(), sequenceNum++); } } catch (Exception e) { throw new IllegalResourceFormatException(e); } return result; }
/** * * @param fileName * @param staticMethods * @param staticFields * @return * @throws FileNotFoundException * @throws IOException */ Collection<JsFile> parseFile(final String fileName, final List<JsMethod> staticMethods, final Map<String, JsElement> staticFields) throws FileNotFoundException, IOException { try (final Reader reader = new FileReader(fileName)) { final CompilerEnvirons env = new CompilerEnvirons(); env.setRecordingLocalJsDocComments(true); env.setAllowSharpComments(true); env.setRecordingComments(true); final AstRoot node = new Parser(env).parse(reader, fileName, 1); final JavaScriptFileParser parser = new JavaScriptFileParser(fileName); node.visitAll(parser); staticMethods.addAll(parser.getStaticMethods()); staticFields.putAll(parser.getConsts()); return parser.getFiles(); } }
/** * * @param expr * @param objectName * @return */ private static String getReferencedScriptObject( String expr, String objectName ) { if ( expr == null ) return null; try { Context cx = Context.enter( ); CompilerEnvirons ce = new CompilerEnvirons( ); Parser p = new Parser( ce, cx.getErrorReporter( ) ); AstRoot tree = p.parse( expr, null, 0 ); IRFactory ir = new IRFactory( ce ); ScriptNode script = ir.transformTree( tree ); return getScriptObjectName( script, objectName ); } finally { Context.exit( ); } }
/** * * @param expr * @param objectName * @return */ public static String getReferencedScriptObject( String expr, String objectName ) { if ( expr == null ) return null; try { Context cx = Context.enter( ); CompilerEnvirons ce = new CompilerEnvirons( ); Parser p = new Parser( ce, cx.getErrorReporter( ) ); AstRoot tree = p.parse( expr, null, 0 ); Node root = new IRFactory( ce).transformTree(tree); return getScriptObjectName( root, objectName ); } catch( Exception ex ) { return null; } finally { Context.exit( ); } }
public void parseScript(String scriptText, TypeDeclarationOptions options) { if(scriptText != null && scriptText.length() > 0) { CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport()); Parser parser = new Parser(env); StringReader r = new StringReader(scriptText); try { AstRoot root = parser.parse(r, null, 0); CodeBlock block = provider.iterateAstRoot(root, preProcessingCompletions, "", Integer.MAX_VALUE, options); provider.recursivelyAddLocalVars(preProcessingCompletions, block, 0, null, false, true); } catch(IOException io) { //ignore this } } }
/** * Called whenever the text area's syntax style changes, as well as * when it is re-parsed. */ @Override public void propertyChange(PropertyChangeEvent e) { String name = e.getPropertyName(); // If the text area is changing the syntax style it is editing if (RSyntaxTextArea.SYNTAX_STYLE_PROPERTY.equals(name)) { checkForJavaScriptParsing(); } else if (JavaScriptParser.PROPERTY_AST.equals(name)) { AstRoot ast = (AstRoot)e.getNewValue(); update(ast); } }
/** * Compiles Text and resolves the type. * e.g * "Hello World".length; //resolve as a Number * * @param text to compile and resolve */ @Override public JavaScriptType compileText(String text) throws IOException { CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport()); String parseText = JavaScriptHelper.removeLastDotFromText(text); int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(parseText); env.setRecoverFromErrors(true); Parser parser = new Parser(env); StringReader r = new StringReader(parseText); AstRoot root = parser.parse(r, null, 0); CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0); root.visitAll(visitor); return lastJavaScriptType; }
/** * Resolve node type to TypeDeclaration. Called instead of #compileText(String text) when document is already parsed * @param node AstNode to resolve * @return TypeDeclaration for node or null if not found. */ @Override public TypeDeclaration resolveParamNode(String text) throws IOException { if(text != null) { CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport()); int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(text); env.setRecoverFromErrors(true); Parser parser = new Parser(env); StringReader r = new StringReader(text); AstRoot root = parser.parse(r, null, 0); CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0); root.visitAll(visitor); } return lastJavaScriptType != null ? lastJavaScriptType.getType() : provider.getTypesFactory().getDefaultTypeDeclaration(); }
public InterpreterData compile(CompilerEnvirons compilerEnv, ScriptNode tree, String encodedSource, boolean returnFunction) { this.compilerEnv = compilerEnv; if (Token.printTrees) { System.out.println("before transform:"); System.out.println(tree.toStringTree(tree)); } new NodeTransformer().transform(tree); if (Token.printTrees) { System.out.println("after transform:"); System.out.println(tree.toStringTree(tree)); } if (returnFunction) { scriptOrFn = tree.getFunctionNode(0); } else { scriptOrFn = tree; } itsData = new InterpreterData(compilerEnv.getLanguageVersion(), scriptOrFn.getSourceName(), encodedSource, ((AstRoot)tree).isInStrictMode()); itsData.topLevel = true; if (returnFunction) { generateFunctionICode(); } else { generateICodeFromTree(scriptOrFn); } return itsData; }
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); }
/** * Asserts that the value returned by {@link AstRoot#toSource()} after * the given input source was parsed equals the specified expected output source. * * @param source the JavaScript source to be parsed * @param expectedOutput the JavaScript source that is expected to be * returned by {@link AstRoot#toSource()} */ private void assertSource(String source, String expectedOutput) { CompilerEnvirons env = new CompilerEnvirons(); env.setLanguageVersion(Context.VERSION_1_7); Parser parser = new Parser(env); AstRoot root = parser.parse(source, null, 0); Assert.assertEquals(expectedOutput, root.toSource()); }
private void assertSource(String source, String expectedOutput) { CompilerEnvirons env = new CompilerEnvirons(); env.setLanguageVersion(Context.VERSION_ES6); Parser parser = new Parser(env); AstRoot root = parser.parse(source, null, 0); Assert.assertEquals(expectedOutput, root.toSource()); }