Search in sources :

Example 1 with ASPCore2Lexer

use of at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer in project Alpha by alpha-asp.

the class Main method parseVisit.

public static ParsedProgram parseVisit(ANTLRInputStream is) throws IOException {
    /*
		// 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);
		*/
    CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(is));
    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(is.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.reset();
            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();
    }
    // Construct internal program representation.
    ParsedTreeVisitor visitor = new ParsedTreeVisitor();
    return (ParsedProgram) visitor.visitProgram(programContext);
}
Also used : ASPCore2Lexer(at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) ParsedTreeVisitor(at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor) ASPCore2Parser(at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)

Aggregations

ASPCore2Lexer (at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)1 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)1 ParsedTreeVisitor (at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1