use of org.antlr.v4.runtime.ANTLRInputStream in project neo4j by neo4j.
the class ResultsParser method parseParameter.
Object parseParameter(String value, CypherParametersCreator listener) {
lexer.setInputStream(new ANTLRInputStream(value));
walker.walk(listener, parser.value());
return listener.parsed();
}
use of org.antlr.v4.runtime.ANTLRInputStream in project aic-expresso by aic-sri-international.
the class AntlrGrinderParserWrapper method parse.
@Override
public Expression parse(String string, Parser.ErrorListener parserEerrorListener) {
Expression result = null;
try {
AntlrErrorListener antlrErrorListener = new AntlrErrorListener(parserEerrorListener);
ANTLRInputStream input = new ANTLRInputStream(string);
AntlrGrinderLexer lexer = new AntlrGrinderLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
AntlrGrinderParser parser = new AntlrGrinderParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener(antlrErrorListener);
parser.addErrorListener(antlrErrorListener);
ParseTree tree = parser.expression();
boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;
if (!antlrErrorListener.errorsDetected) {
if (!eof) {
System.err.println("Unable to parse the complete input expression: " + input);
} else {
lexer.removeErrorListeners();
parser.removeErrorListeners();
ExpressionVisitor expressionVisitor = new ExpressionVisitor(getRandomPredicatesSignatures());
result = expressionVisitor.visit(tree);
}
}
} catch (RecognitionException re) {
re.printStackTrace();
} catch (RuntimeException re) {
re.printStackTrace();
}
return result;
}
use of org.antlr.v4.runtime.ANTLRInputStream in project aic-praise by aic-sri-international.
the class HOGMCodeArea method computeHighlighting.
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
int lastTokenEnd = 0;
ANTLRInputStream input = new ANTLRInputStream(text);
HOGMLexer lexer = new HOGMLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
for (int i = 0; i < tokens.size(); i++) {
Token t = tokens.get(i);
if (t.getType() == Token.EOF) {
break;
}
String styleClass;
if (t.getType() == HOGMLexer.COMMENT || t.getType() == HOGMLexer.LINE_COMMENT) {
styleClass = "hogmCodeComment";
} else if (HOGMTerminalSymbols.isTerminalSymbol(t.getText())) {
styleClass = "hogmCodeKeyword";
} else {
styleClass = "hogmCodeOther";
}
int spacing = t.getStartIndex() - lastTokenEnd;
if (spacing > 0) {
spansBuilder.add(Collections.emptyList(), spacing);
}
int stylesize = (t.getStopIndex() - t.getStartIndex()) + 1;
spansBuilder.add(Collections.singleton(styleClass), stylesize);
lastTokenEnd = t.getStopIndex() + 1;
}
return spansBuilder.create();
}
use of org.antlr.v4.runtime.ANTLRInputStream in project presto by prestodb.
the class SqlParser method invokeParser.
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.reset();
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder().visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.ANTLRInputStream in project presto by prestodb.
the class StatementSplitter method getLexer.
private static TokenSource getLexer(String sql, Set<String> terminators) {
requireNonNull(sql, "sql is null");
CharStream stream = new CaseInsensitiveStream(new ANTLRInputStream(sql));
return new DelimiterLexer(stream, terminators);
}
Aggregations