Search in sources :

Example 1 with ParserFactory

use of com.sun.tools.javac.parser.ParserFactory in project ceylon-compiler by ceylon.

the class JavacTaskImpl method parseType.

/**
     * For internal use only.  This method will be
     * removed without warning.
     */
public Type parseType(String expr, TypeElement scope) {
    if (expr == null || expr.equals(""))
        throw new IllegalArgumentException();
    compiler = JavaCompiler.instance(context);
    JavaFileObject prev = compiler.log.useSource(null);
    ParserFactory parserFactory = ParserFactory.instance(context);
    Attr attr = Attr.instance(context);
    try {
        CharBuffer buf = CharBuffer.wrap((expr + "").toCharArray(), 0, expr.length());
        Parser parser = parserFactory.newParser(buf, false, false, false);
        JCTree tree = parser.parseType();
        return attr.attribType(tree, (Symbol.TypeSymbol) scope);
    } finally {
        compiler.log.useSource(prev);
    }
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) CharBuffer(java.nio.CharBuffer) JCTree(com.sun.tools.javac.tree.JCTree) ParserFactory(com.sun.tools.javac.parser.ParserFactory) Parser(com.sun.tools.javac.parser.Parser)

Example 2 with ParserFactory

use of com.sun.tools.javac.parser.ParserFactory in project ceylon-compiler by ceylon.

the class TestLog method test.

static void test(boolean genEndPos) throws IOException {
    Context context = new Context();
    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
    Log log = Log.instance(context);
    log.multipleErrors = true;
    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);
    final String text = "public class Foo {\n" + "  public static void main(String[] args) {\n" + "    if (args.length == 0)\n" + "      System.out.println(\"no args\");\n" + "    else\n" + "      System.out.println(args.length + \" args\");\n" + "  }\n" + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);
    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);
    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);
    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
Also used : Context(com.sun.tools.javac.util.Context) Options(com.sun.tools.javac.util.Options) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) ParserFactory(com.sun.tools.javac.parser.ParserFactory) Parser(com.sun.tools.javac.parser.Parser) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreeScanner(com.sun.tools.javac.tree.TreeScanner)

Example 3 with ParserFactory

use of com.sun.tools.javac.parser.ParserFactory 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)

Aggregations

ParserFactory (com.sun.tools.javac.parser.ParserFactory)3 Parser (com.sun.tools.javac.parser.Parser)2 JCTree (com.sun.tools.javac.tree.JCTree)2 Context (com.sun.tools.javac.util.Context)2 Symbol (com.sun.tools.javac.code.Symbol)1 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)1 JavacParser (com.sun.tools.javac.parser.JavacParser)1 TreeScanner (com.sun.tools.javac.tree.TreeScanner)1 Log (com.sun.tools.javac.util.Log)1 Options (com.sun.tools.javac.util.Options)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 CharBuffer (java.nio.CharBuffer)1 JavaFileObject (javax.tools.JavaFileObject)1 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)1