use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.
the class JQLChecker method prepareParser.
protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final JQLContext jqlContext, final String jql) {
JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
CommonTokenStream tokens = new CommonTokenStream(lexer);
JqlParser parser = new JqlParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new JQLBaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
AssertKripton.assertTrue(false, jqlContext.getContextDescription() + ": unespected char at pos %s of SQL '%s'", charPositionInLine, jql);
}
});
ParserRuleContext context = parser.parse();
return new Pair<>(context, tokens);
}
use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.
the class JQLChecker method prepareVariableStatement.
/**
* <p>
* Parse the variable parts of a SQL:
* </p>
*
* <ul>
* <li>where_stmt</li>
* <li>group_stmt</li>
* <li>having_stmt</li>
* <li>order_stmt</li>
* <li>limit_stmt</li>
* <li>offset_stmt</li>
* </ul>
*
* @param jql
* @return
*/
protected Pair<ParserRuleContext, CommonTokenStream> prepareVariableStatement(final JQLContext jqlContext, final String jql) {
JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
CommonTokenStream tokens = new CommonTokenStream(lexer);
JqlParser parser = new JqlParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new JQLBaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
AssertKripton.assertTrue(false, jqlContext.getContextDescription() + ": unespected char at pos %s of JQL '%s'", charPositionInLine, jql);
}
});
ParserRuleContext context = parser.parse_variable();
return new Pair<>(context, tokens);
}
use of org.antlr.v4.runtime.Recognizer in project beast2 by CompEvol.
the class TreeParser method parseNewick.
/**
* Parse a newick-ish string and generate the BEAST tree it describes.
*
* @param newick string to parse
* @return root node of tree
*/
public Node parseNewick(String newick) {
CharStream charStream = CharStreams.fromString(newick);
// Custom parse/lexer error listener
BaseErrorListener errorListener = new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
throw new TreeParsingException(msg, charPositionInLine, line);
}
};
// Use lexer to produce token stream
NewickLexer lexer = new NewickLexer(charStream);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Parse token stream to produce parse tree
NewickParser parser = new NewickParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
ParseTree parseTree = parser.tree();
// Traverse parse tree, constructing BEAST tree along the way
NewickASTVisitor visitor = new NewickASTVisitor();
return visitor.visit(parseTree);
}
use of org.antlr.v4.runtime.Recognizer in project kalang by kasonyang.
the class KalangCompiler method createParser.
@Override
public KalangParser createParser(CompilationUnit compilationUnit, CommonTokenStream tokenStream) {
KalangParser parser = new KalangParser(tokenStream);
parser.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Diagnosis diagnosis = new Diagnosis(compilationUnit.getCompileContext(), Diagnosis.Kind.ERROR, OffsetRangeHelper.getOffsetRange(e.getOffendingToken()), msg, compilationUnit.getSource());
diagnosisHandler.handleDiagnosis(diagnosis);
}
});
return parser;
}
use of org.antlr.v4.runtime.Recognizer in project kalang by kasonyang.
the class AstBuilderFactory method createAstBuilder.
public static AstBuilder createAstBuilder(CompilationUnit source, TokenStream tokens) {
KalangParser p = new KalangParser(tokens);
AstBuilder sp = new AstBuilder(source, p);
p.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Token end = e.getOffendingToken();
Token start;
RuleContext ctx = e.getCtx();
if (ctx instanceof ParserRuleContext) {
start = ((ParserRuleContext) ctx).getStart();
} else {
start = end;
}
sp.getDiagnosisReporter().report(Diagnosis.Kind.ERROR, msg, start, end);
}
});
return sp;
}
Aggregations