use of org.antlr.v4.runtime.ANTLRInputStream in project pinot by linkedin.
the class Pql2Compiler method compileToExpressionTree.
@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
CharStream charStream = new ANTLRInputStream(expression);
PQL2Lexer lexer = new PQL2Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
PQL2Parser parser = new PQL2Parser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
// Parse
ParseTree parseTree = parser.expression();
ParseTreeWalker walker = new ParseTreeWalker();
Pql2AstListener listener = new Pql2AstListener(expression);
walker.walk(listener, parseTree);
final AstNode rootNode = listener.getRootNode();
return TransformExpressionTree.buildTree(rootNode);
}
use of org.antlr.v4.runtime.ANTLRInputStream in project rest.li by linkedin.
the class PdlSchemaParser method parse.
/**
* Parse a JSON representation of a schema from a {{java.io.Reader}}.
*
* The top level {{DataSchema}}'s parsed are in {{#topLevelDataSchemas}}.
* These are the types that are not defined within other types.
* Parse errors are in {{#errorMessageBuilder}} and indicated
* by {{#hasError()}}.
*
* @param reader with the JSON representation of the schema.
*/
public void parse(Reader reader) {
try {
ErrorRecorder errorRecorder = new ErrorRecorder();
PdlLexer lexer;
try {
lexer = new PdlLexer(new ANTLRInputStream(reader));
} catch (IOException e) {
ParseError error = new ParseError(new ParseErrorLocation(0, 0), e.getMessage());
startErrorMessage(error).append(error.message).append(NEWLINE);
return;
}
lexer.removeErrorListeners();
lexer.addErrorListener(errorRecorder);
PdlParser parser = new PdlParser(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(errorRecorder);
DocumentContext antlrDocument = parser.document();
parse(antlrDocument);
if (errorRecorder.errors.size() > 0) {
for (ParseError error : errorRecorder.errors) {
startErrorMessage(error).append(error.message).append(NEWLINE);
}
}
} catch (ParseException e) {
startErrorMessage(e.error).append(e.getMessage()).append(NEWLINE);
} catch (Throwable t) {
ParseError parseError = new ParseError(new ParseErrorLocation(0, 0), null);
startErrorMessage(parseError).append("Unexpected parser error: ").append(ExceptionUtils.getStackTrace(t)).append(NEWLINE);
}
}
use of org.antlr.v4.runtime.ANTLRInputStream in project druid by druid-io.
the class Parser method parse.
public static Expr parse(String in, boolean withFlatten) {
ExprLexer lexer = new ExprLexer(new ANTLRInputStream(in));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
parser.setBuildParseTree(true);
ParseTree parseTree = parser.expr();
ParseTreeWalker walker = new ParseTreeWalker();
ExprListenerImpl listener = new ExprListenerImpl(parseTree);
walker.walk(listener, parseTree);
return withFlatten ? flatten(listener.getAST()) : listener.getAST();
}
use of org.antlr.v4.runtime.ANTLRInputStream in project checkstyle by checkstyle.
the class JavadocParseTreeTest method parseJavadoc.
private ParseTree parseJavadoc(String aBlockComment) throws IOException {
final Charset utf8Charset = Charset.forName("UTF-8");
final InputStream in = new ByteArrayInputStream(aBlockComment.getBytes(utf8Charset));
final ANTLRInputStream input = new ANTLRInputStream(in);
final JavadocLexer lexer = new JavadocLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new JavadocParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
return parser.javadoc();
}
use of org.antlr.v4.runtime.ANTLRInputStream in project checkstyle by checkstyle.
the class JavadocDetailNodeParser method parseJavadocAsParseTree.
/**
* Parses block comment content as javadoc comment.
* @param blockComment
* block comment content.
* @return parse tree
*/
private ParseTree parseJavadocAsParseTree(String blockComment) {
final ANTLRInputStream input = new ANTLRInputStream(blockComment);
final JavadocLexer lexer = new JavadocLexer(input);
// remove default error listeners
lexer.removeErrorListeners();
// add custom error listener that logs parsing errors
lexer.addErrorListener(errorListener);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final JavadocParser parser = new JavadocParser(tokens);
// remove default error listeners
parser.removeErrorListeners();
// add custom error listener that logs syntax errors
parser.addErrorListener(errorListener);
// This strategy stops parsing when parser error occurs.
// By default it uses Error Recover Strategy which is slow and useless.
parser.setErrorHandler(new BailErrorStrategy());
return parser.javadoc();
}
Aggregations