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;
}
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;
}
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);
}
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);
}
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
}
Aggregations