public static FunctionNode parseJs(String aJsContent) { Source source = Source.sourceFor("", aJsContent); // NOI18N Options options = new Options(null); ScriptEnvironment env = new ScriptEnvironment(options, null, null); ErrorManager errors = new ErrorManager(); Parser p = new Parser(env, source, errors); return p.parse(); }
@Override public boolean parse(File file, ParserOptions options) throws IOException { final PrintWriter err = options.isPrintError() ? options.getProgressWriter() : new PrintWriter(new OutputStreamWriter(new ByteArrayOutputStream())); final ScriptEnvironment env = new ScriptEnvironment( new jdk.nashorn.internal.runtime.options.Options(file.getAbsolutePath()), err, err); final Source src = Source.sourceFor(file.getName(), file); final ErrorManager em = new ErrorManager(err); final Parser p = new Parser(env, src, em); final FunctionNode node = p.parse(); return !em.hasErrors(); }
/** * Extracts the comments tokens from a JavaScript source. * * @param aSource a source * @return a list of comment tokens */ public static List<Long> getCommentsTokens(String aSource) { TokenStream tokens = new TokenStream(); Lexer lexer = new Lexer(Source.sourceFor("", aSource), tokens); // NOI18N long t; TokenType tt = TokenType.EOL; int i = 0; List<Long> commentsTokens = new ArrayList<>(); while (tt != TokenType.EOF) { // Get next token in nashorn's parser way while (i > tokens.last()) { if (tokens.isFull()) { tokens.grow(); } lexer.lexify(); } t = tokens.get(i++); tt = Token.descType(t); if (tt == TokenType.COMMENT) { commentsTokens.add(t); } } return commentsTokens; }