use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.
the class ProgramParserImpl method parse.
public ASPCore2Program parse(CharStream stream, Map<String, PredicateInterpretation> externals) {
// @formatter:off
/*
* // In order to require less memory: use unbuffered streams and avoid constructing a full parse tree.
* ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is));
* lexer.setTokenFactory(new CommonTokenFactory(true));
* final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer));
* parser.setBuildParseTree(false);
*/
// @formatter:on
CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(stream));
final ASPCore2Parser parser = new ASPCore2Parser(tokens);
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
final CustomErrorListener errorListener = new CustomErrorListener(stream.getSourceName());
ASPCore2Parser.ProgramContext programContext;
try {
// Parse program
programContext = parser.program();
} catch (ParseCancellationException e) {
// retry with LL parser and DefaultErrorStrategy printing errors to console.
if (e.getCause() instanceof RecognitionException) {
tokens.seek(0);
parser.addErrorListener(errorListener);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
// Re-run parse.
programContext = parser.program();
} else {
throw e;
}
}
// is attempted) and the user will only see the first error encountered.
if (errorListener.getRecognitionException() != null) {
throw errorListener.getRecognitionException();
}
// Abort parsing if there were some (recoverable) syntax errors.
if (parser.getNumberOfSyntaxErrors() != 0) {
throw new ParseCancellationException();
}
// The union of this parser's preloaded externals and the (program-specific) externals passed to the parse method
Map<String, PredicateInterpretation> knownExternals;
if (externals != null && !externals.isEmpty()) {
knownExternals = new HashMap<>(preloadedExternals);
knownExternals.putAll(externals);
} else {
knownExternals = preloadedExternals;
}
// Construct internal program representation.
ParseTreeVisitor visitor = new ParseTreeVisitor(knownExternals);
return visitor.translate(programContext);
}
use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.
the class AnswerSetsParser method parse.
public static Set<AnswerSet> parse(CharStream stream) {
final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
return VISITOR.translate(parser.answer_sets());
}
use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.
the class AnswerSetsParser method parse.
public static Set<AnswerSet> parse(CharStream stream) {
final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
return VISITOR.translate(parser.answer_sets());
}
Aggregations