Search in sources :

Example 76 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class FortranIncludeStatementsFinder method run.

/**
 * @return true if at least one include statement found
 * @throws Exception
 */
public List<FortranStatementBasicPosition> run(InputStream input) throws Exception {
    lexer.reset();
    parser.reset();
    lexerErrorListener.reset();
    parserErrorListener.reset();
    CharStream chrStrm = CharStreams.fromStream(input, StandardCharsets.US_ASCII);
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    parser.setInputStream(tokStrm);
    parser.setBuildParseTree(true);
    ParseTree tree = null;
    try {
        tree = parser.root();
    } catch (CancellationException e) {
    }
    if (lexerErrorListener.error() != null) {
        throw lexerErrorListener.error();
    }
    if (parserErrorListener.error() != null) {
        throw parserErrorListener.error();
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener();
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.error() != null) {
        throw listener.error();
    }
    return listener.includes;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FortranIncludesResolverBaseListener(clawfc.depscan.parser.FortranIncludesResolverBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) Utils.toCharStream(clawfc.Utils.toCharStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 77 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class FortranLineBreaksFilterTest method toTokenStream.

private CommonTokenStream toTokenStream(String str) throws IOException {
    CharStream chrStrm = toCharStream(str);
    lexer.reset();
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    return tokStrm;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream)

Example 78 with CharStream

use of org.antlr.v4.runtime.CharStream in project Alpha by alpha-asp.

the class ParserTest method stringWithEscapedQuotes.

@Test
public void stringWithEscapedQuotes() throws IOException {
    CharStream stream = CharStreams.fromStream(ParserTest.class.getResourceAsStream("/escaped_quotes.asp"));
    ASPCore2Program prog = parser.parse(stream);
    assertEquals(1, prog.getFacts().size());
    Atom stringAtom = prog.getFacts().get(0);
    String stringWithQuotes = stringAtom.getTerms().get(0).toString();
    assertEquals("\"a string with \"quotes\"\"", stringWithQuotes);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) CharStream(org.antlr.v4.runtime.CharStream) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 79 with CharStream

use of org.antlr.v4.runtime.CharStream 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);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 80 with CharStream

use of org.antlr.v4.runtime.CharStream in project Alpha by alpha-asp.

the class RacksTest method test.

private void test(RegressionTestConfig cfg) throws IOException {
    CharStream programInputStream = CharStreams.fromPath(Paths.get("benchmarks", "siemens", "racks", "racks.lp"));
    Solver solver = buildSolverForRegressionTest(new ProgramParserImpl().parse(programInputStream), cfg);
    @SuppressWarnings("unused") Optional<AnswerSet> answerSet = solver.stream().findFirst();
// System.out.println(answerSet);
// TODO: check correctness of answer set
}
Also used : Solver(at.ac.tuwien.kr.alpha.api.Solver) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) CharStream(org.antlr.v4.runtime.CharStream)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14