/** * Gathers the syntax errors found by Rhino in-process when parsing the * document. * * @param errorHandler The errors found by Rhino. * @param root The root element of the document parsed. * @see #gatherParserErrorsJsHint(RSyntaxDocument) */ private void gatherParserErrorsRhino(ErrorCollector errorHandler, Element root) { List<ParseProblem> errors = errorHandler.getErrors(); if (errors != null && errors.size() > 0) { for (ParseProblem problem : errors) { int offs = problem.getFileOffset(); int len = problem.getLength(); int line = root.getElementIndex(offs); String desc = problem.getMessage(); DefaultParserNotice notice = new DefaultParserNotice(this, desc, line, offs, len); if (problem.getType() == ParseProblem.Type.Warning) { notice.setLevel(ParserNotice.Level.WARNING); } result.addNotice(notice); } } }
/** * Returns a {@code CompilerEnvirons} suitable for using Rhino * in an IDE environment. Most features are enabled by default. * The {@link ErrorReporter} is set to an {@link ErrorCollector}. */ public static CompilerEnvirons ideEnvirons() { CompilerEnvirons env = new CompilerEnvirons(); env.setRecoverFromErrors(true); env.setRecordingComments(true); env.setStrictMode(true); env.setWarnTrailingComma(true); env.setLanguageVersion(170); env.setReservedKeywordAsIdentifier(true); env.setIdeMode(true); env.setErrorReporter(new ErrorCollector()); return env; }
/** * Returns a {@code CompilerEnvirons} suitable for using Rhino * in an IDE environment. Most features are enabled by default. * The {@link org.mozilla.javascript.ErrorReporter} is set to an {@link org.mozilla.javascript.ast.ErrorCollector}. */ public static CompilerEnvirons ideEnvirons() { CompilerEnvirons env = new CompilerEnvirons(); env.setRecoverFromErrors(true); env.setRecordingComments(true); env.setStrictMode(true); env.setWarnTrailingComma(true); env.setLanguageVersion(170); env.setReservedKeywordAsIdentifier(true); env.setIdeMode(true); env.setErrorReporter(new ErrorCollector()); return env; }
/** * {@inheritDoc} */ @Override public ParseResult parse(RSyntaxDocument doc, String style) { astRoot = null; result.clearNotices(); // Always spell check all lines, for now. Element root = doc.getDefaultRootElement(); int lineCount = root.getElementCount(); result.setParsedLines(0, lineCount - 1); DocumentReader r = new DocumentReader(doc); ErrorCollector errorHandler = new ErrorCollector(); CompilerEnvirons env = createCompilerEnvironment(errorHandler, langSupport); long start = System.currentTimeMillis(); try { Parser parser = new Parser(env); astRoot = parser.parse(r, null, 0); long time = System.currentTimeMillis() - start; result.setParseTime(time); } catch (IOException ioe) { // Never happens result.setError(ioe); ioe.printStackTrace(); } catch (RhinoException re) { // Shouldn't happen since we're passing an ErrorCollector in int line = re.lineNumber(); // if (line>0) { Element elem = root.getElement(line); int offs = elem.getStartOffset(); int len = elem.getEndOffset() - offs - 1; String msg = re.details(); result.addNotice(new DefaultParserNotice(this, msg, line, offs, len)); // } } catch (Exception e) { result.setError(e); // catch all } r.close(); // Get any parser errors. switch (langSupport.getErrorParser()) { default: case RHINO: gatherParserErrorsRhino(errorHandler, root); break; case JSHINT: gatherParserErrorsJsHint(doc); break; } // addNotices(doc); support.firePropertyChange(PROPERTY_AST, null, astRoot); return result; }