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);
}
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();
}
}
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;
}
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);
}
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" : "");
}
}
Aggregations