Search in sources :

Example 1 with Parser

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

the class CommentCollectingParserFactory method setInCompiler.

public static void setInCompiler(JavaCompiler compiler, Context context) {
    context.put(CommentCollectingParserFactory.key(), (Parser.Factory) null);
    Field field;
    try {
        field = JavaCompiler.class.getDeclaredField("parserFactory");
        field.setAccessible(true);
        field.set(compiler, new CommentCollectingParserFactory(context));
    } catch (Exception e) {
        throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e);
    }
}
Also used : Field(java.lang.reflect.Field) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) Parser(com.sun.tools.javac.parser.Parser)

Example 2 with Parser

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

the class CommentCollectingParserFactory method newParser.

public Parser 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);
    return (Parser) 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 javac7's EndPosParser which implements Parser.
//Either way this will work out.
}
Also used : Lexer(com.sun.tools.javac.parser.Lexer) ScannerFactory(com.sun.tools.javac.parser.ScannerFactory) Parser(com.sun.tools.javac.parser.Parser)

Example 3 with Parser

use of com.sun.tools.javac.parser.Parser 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 4 with Parser

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

Aggregations

Parser (com.sun.tools.javac.parser.Parser)4 ParserFactory (com.sun.tools.javac.parser.ParserFactory)2 JCTree (com.sun.tools.javac.tree.JCTree)2 Symbol (com.sun.tools.javac.code.Symbol)1 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)1 Lexer (com.sun.tools.javac.parser.Lexer)1 ScannerFactory (com.sun.tools.javac.parser.ScannerFactory)1 TreeScanner (com.sun.tools.javac.tree.TreeScanner)1 Context (com.sun.tools.javac.util.Context)1 Log (com.sun.tools.javac.util.Log)1 Options (com.sun.tools.javac.util.Options)1 Field (java.lang.reflect.Field)1 CharBuffer (java.nio.CharBuffer)1 JavaFileObject (javax.tools.JavaFileObject)1 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)1