public static ScriptNode 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 ); ScriptNode tree = p.parse( source, "", 0 ); // IOException new NodeTransformer().transform( tree ); // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null); return tree; } }
public static ScriptNode 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 ); ScriptNode tree = p.parse( source, "", 0 ); // IOException new NodeTransformer().transform( tree ); // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null); return tree; } }
protected AstRoot parseEcmascript(final String sourceCode, final List<ParseProblem> parseProblems) throws ParseException { final CompilerEnvirons compilerEnvirons = new CompilerEnvirons(); compilerEnvirons.setRecordingComments(parserOptions.isRecordingComments()); compilerEnvirons.setRecordingLocalJsDocComments(parserOptions.isRecordingLocalJsDocComments()); compilerEnvirons.setLanguageVersion(parserOptions.getRhinoLanguageVersion().getVersion()); // Scope's don't appear to get set right without this compilerEnvirons.setIdeMode(true); compilerEnvirons.setWarnTrailingComma(true); // see bug #1150 "EmptyExpression" for valid statements! compilerEnvirons.setReservedKeywordAsIdentifier(true); // TODO We should do something with Rhino errors... final ErrorCollector errorCollector = new ErrorCollector(); final Parser parser = new Parser(compilerEnvirons, errorCollector); // TODO Fix hardcode final String sourceURI = "unknown"; final int beginLineno = 1; AstRoot astRoot = parser.parse(sourceCode, sourceURI, beginLineno); parseProblems.addAll(errorCollector.getErrors()); return astRoot; }
public ScriptOrFnNode parse(Reader sourceReader, String sourceURI, int lineno) throws IOException { this.sourceURI = sourceURI; this.ts = new TokenStream(this, sourceReader, null, lineno); return parse(); }
public ScriptOrFnNode parse(String sourceString, String sourceURI, int lineno) { this.sourceURI = sourceURI; this.ts = new TokenStream(this, null, sourceString, lineno); try { return parse(); } catch (IOException ex) { // Should never happen throw new IllegalStateException(); } }
public ScriptOrFnNode parse(Reader sourceReader, String sourceURI, int lineno) throws IOException { this.sourceURI = sourceURI; this.ts = new TokenStream(this, sourceReader, null, lineno); return parse(); }
public ScriptOrFnNode parse(Reader sourceReader, String sourceURI, int lineno) throws IOException { this.sourceURI = sourceURI; this.ts = new TokenStream(this, sourceReader, null, lineno); return parse(); }
public ScriptOrFnNode parse(String sourceString, String sourceURI, int lineno) { this.sourceURI = sourceURI; this.ts = new TokenStream(this, null, sourceString, lineno); try { return parse(); } catch (IOException ex) { // Should never happen throw new IllegalStateException(); } }
public ScriptOrFnNode parse(String sourceString, String sourceURI, int lineno) { this.sourceURI = sourceURI; this.ts = new TokenStream(this, null, sourceString, lineno); try { return parse(); } catch (IOException ex) { // Should never happen throw new IllegalStateException(); } }
/** * 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; } }
/** * 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; } }
/** * 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; } }
/** * 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; } }
/** * 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 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; } }
/** * 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(); } }
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 } } }
@Override public TreeContext generate(Reader r) throws IOException { CompilerEnvirons env = new CompilerEnvirons(); env.setRecordingLocalJsDocComments(true); env.setAllowSharpComments(true); env.setRecordingComments(true); Parser p = new Parser(env); AstRoot root = p.parse(r, null, 1); RhinoTreeVisitor visitor = new RhinoTreeVisitor(root); root.visitAll(visitor); return visitor.getTree(root); } }
/** * 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 text The node 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(); }