/** * 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; } }
private String instrument(final int lineNumber, final ScriptData data) { final Parser parser = new Parser(); final String sourceUriAsString = data.getSourceUriAsString(); final AstRoot root = parser.parse(data.getSourceCode(), sourceUriAsString, lineNumber); root.visit(new InstrumentingNodeVisitor(data, lineNumber - 1)); final String treeSource = root.toSource(); final StringBuilder buf = new StringBuilder( INITIALIZING_CODE.length() + data.getNumberOfStatements() * ARRAY_INITIALIZER.length() + treeSource.length()); buf.append(COMPLETION_MONITOR); buf.append(INITIALIZING_CODE); buf.append(String.format("if(!%s['%s']) {%n", COVERAGE_VARIABLE_NAME, sourceUriAsString)); buf.append(String.format(" %s['%s'] = {};%n", COVERAGE_VARIABLE_NAME, sourceUriAsString)); for (final Integer i : data.getLineNumbersOfAllStatements()) { buf.append(String.format(ARRAY_INITIALIZER, sourceUriAsString, i)); } buf.append(String.format("}%n")); buf.append(treeSource); return buf.toString(); }
Parser p = new Parser(compilerEnv, DefaultErrorReporter.instance); try { p.parse(source, null, 1); } catch (EvaluatorException ee) { errorseen = true;
AstRoot ast = p.parse(source, sourceLocation, lineno); IRFactory irf = new IRFactory(compilerEnv); ScriptNode tree = irf.transformTree(ast);