Search in sources :

Example 1 with JavacParser

use of com.sun.tools.javac.parser.JavacParser in project error-prone by google.

the class GuardedByUtils method parseString.

public static JCTree.JCExpression parseString(String guardedByString, Context context) {
    JavacParser parser = ParserFactory.instance(context).newParser(guardedByString, false, true, false);
    JCTree.JCExpression exp;
    try {
        exp = parser.parseExpression();
    } catch (Throwable e) {
        throw new IllegalGuardedBy(e.getMessage());
    }
    int len = (parser.getEndPos(exp) - exp.getStartPosition());
    if (len != guardedByString.length()) {
        throw new IllegalGuardedBy("Didn't parse entire string.");
    }
    return exp;
}
Also used : JavacParser(com.sun.tools.javac.parser.JavacParser) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with JavacParser

use of com.sun.tools.javac.parser.JavacParser in project bazel by bazelbuild.

the class TreePrunerTest method parseLines.

JCCompilationUnit parseLines(String... lines) {
    Context context = new Context();
    try (JavacFileManager fm = new JavacFileManager(context, true, UTF_8)) {
        ParserFactory parserFactory = ParserFactory.instance(context);
        String input = Joiner.on('\n').join(lines);
        JavacParser parser = parserFactory.newParser(input, /*keepDocComments=*/
        false, /*keepEndPos=*/
        false, /*keepLineMap=*/
        false);
        return parser.parseCompilationUnit();
    } catch (IOException e) {
        throw new IOError(e);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavacParser(com.sun.tools.javac.parser.JavacParser) IOError(java.io.IOError) ParserFactory(com.sun.tools.javac.parser.ParserFactory) IOException(java.io.IOException)

Example 3 with JavacParser

use of com.sun.tools.javac.parser.JavacParser in project lombok by rzwitserloot.

the class CommentCollectingParserFactory method newParser.

public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
    ScannerFactory scannerFactory = ScannerFactory.instance(context);
    Lexer lexer = scannerFactory.newScanner(input, true);
    Object x = new CommentCollectingParser(this, lexer, true, keepLineMap, keepEndPos);
    return (JavacParser) x;
// CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either
//javac6's EndPosParser which extends Parser, or javac8's JavacParser which implements Parser.
//Either way this will work out.
}
Also used : Lexer(com.sun.tools.javac.parser.Lexer) JavacParser(com.sun.tools.javac.parser.JavacParser) ScannerFactory(com.sun.tools.javac.parser.ScannerFactory)

Aggregations

JavacParser (com.sun.tools.javac.parser.JavacParser)3 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)1 Lexer (com.sun.tools.javac.parser.Lexer)1 ParserFactory (com.sun.tools.javac.parser.ParserFactory)1 ScannerFactory (com.sun.tools.javac.parser.ScannerFactory)1 JCTree (com.sun.tools.javac.tree.JCTree)1 Context (com.sun.tools.javac.util.Context)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1