Search in sources :

Example 1 with JavaLexer

use of org.antlr.v4.test.runtime.java.api.JavaLexer in project beakerx by twosigma.

the class JavaAutocomplete method find.

private AutocompleteResult find(String txt, int cur, ClassLoader l, Imports imports) {
    registry = AutocompleteRegistryFactory.createRegistry(cps);
    ClassUtils cu = createClassUtils(l);
    setup(cu, registry);
    AutocompleteRegistryFactory.addDefaultImports(cu, registry, imports.toListOfStrings(), cps);
    Lexer lexer = new JavaLexer(new ANTLRInputStream(txt));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that reads from the scanner
    JavaParser parser = new JavaParser(tokens);
    parser.removeErrorListeners();
    // start parsing at the compilationUnit rule
    ParserRuleContext t = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker();
    List<AutocompleteCandidate> q = new ArrayList<AutocompleteCandidate>();
    JavaImportDeclarationCompletion extractor = new JavaImportDeclarationCompletion(txt, cur, registry, cps, cu);
    JavaNameBuilder extractor2 = new JavaNameBuilder(registry, cu);
    JavaNodeCompletion extractor3 = new JavaNodeCompletion(txt, cur, registry, cu);
    walker.walk(extractor, t);
    if (extractor.getQuery() != null)
        q.addAll(extractor.getQuery());
    walker.walk(extractor2, t);
    walker.walk(extractor3, t);
    if (extractor3.getQuery() != null)
        q.addAll(extractor3.getQuery());
    List<String> ret = registry.searchCandidates(q);
    if (!ret.isEmpty()) {
        return new AutocompleteResult(ret, getStartIndex(extractor, extractor2, extractor3));
    }
    return findAutocompleteResult(txt, cur, cu);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ArrayList(java.util.ArrayList) ClassUtils(com.twosigma.beakerx.autocomplete.ClassUtils) Lexer(org.antlr.v4.runtime.Lexer) AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult)

Example 2 with JavaLexer

use of org.antlr.v4.test.runtime.java.api.JavaLexer in project antlr4 by tunnelvisionlabs.

the class TimeLexerSpeed method lex_legacy_java_utf8.

public void lex_legacy_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
    InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(Parser_java_file);
    try {
        InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
        try {
            BufferedReader br = new BufferedReader(isr);
            try {
                @SuppressWarnings("deprecation") CharStream input = new org.antlr.v4.runtime.ANTLRInputStream(br);
                JavaLexer lexer = new JavaLexer(input);
                double avg = tokenize(lexer, n, clearLexerDFACache);
                String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
                if (output)
                    System.out.printf("%27s average time %5dus over %4d runs of %5d symbols%s\n", currentMethodName, (int) avg, n, input.size(), clearLexerDFACache ? " DFA cleared" : "");
            } finally {
                br.close();
            }
        } finally {
            isr.close();
        }
    } finally {
        is.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException) JavaLexer(org.antlr.v4.test.runtime.java.api.JavaLexer) BufferedReader(java.io.BufferedReader)

Example 3 with JavaLexer

use of org.antlr.v4.test.runtime.java.api.JavaLexer in project flink by apache.

the class MemberFieldRewriter method prepareRewrite.

private JavaParser prepareRewrite() {
    CommonTokenStream tokenStream = new CommonTokenStream(new JavaLexer(CharStreams.fromString(code)));
    this.rewriter = new TokenStreamRewriter(tokenStream);
    JavaParser javaParser = new JavaParser(tokenStream);
    javaParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    return javaParser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenStreamRewriter(org.antlr.v4.runtime.TokenStreamRewriter)

Example 4 with JavaLexer

use of org.antlr.v4.test.runtime.java.api.JavaLexer in project flink by apache.

the class JavaParserTest method testConstructorCall.

@Test
public void testConstructorCall() {
    String code = "public class A extends B {\n" + "  private final int a, b;\n" + "  public A(int a) {\n" + "    this(a, 0);\n" + "  }\n" + "  public A(int a, int b) {\n" + "    super(a, b);\n" + "    this.a = a;\n" + "    this.b = b;\n" + "  }\n" + "}";
    CommonTokenStream tokenStream = new CommonTokenStream(new JavaLexer(CharStreams.fromString(code)));
    JavaParser parser = new JavaParser(tokenStream);
    TestConstructorCallVisitor visitor = new TestConstructorCallVisitor();
    visitor.visit(parser.compilationUnit());
    Assert.assertEquals(1, visitor.thisCount);
    Assert.assertEquals(1, visitor.superCount);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Test(org.junit.Test)

Example 5 with JavaLexer

use of org.antlr.v4.test.runtime.java.api.JavaLexer in project antlr4 by antlr.

the class TimeLexerSpeed method lex_legacy_java_utf8.

public void lex_legacy_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
    try (InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(Parser_java_file);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr)) {
        CharStream input = new ANTLRInputStream(br);
        JavaLexer lexer = new JavaLexer(input);
        double avg = tokenize(lexer, n, clearLexerDFACache);
        String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
        if (output)
            System.out.printf("%27s average time %5dus over %4d runs of %5d symbols%s\n", currentMethodName, (int) avg, n, input.size(), clearLexerDFACache ? " DFA cleared" : "");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JavaLexer(org.antlr.v4.test.runtime.java.api.JavaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 CharStream (org.antlr.v4.runtime.CharStream)4 JavaLexer (org.antlr.v4.test.runtime.java.api.JavaLexer)4 ArrayList (java.util.ArrayList)3 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FilenameFilter (java.io.FilenameFilter)2 InputStreamReader (java.io.InputStreamReader)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ATN (org.antlr.v4.runtime.atn.ATN)2 AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)1 AutocompleteResult (com.twosigma.beakerx.autocomplete.AutocompleteResult)1 ClassUtils (com.twosigma.beakerx.autocomplete.ClassUtils)1 Lexer (org.antlr.v4.runtime.Lexer)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1