use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.
the class TestSimple method testInterpreter.
@Test
public void testInterpreter() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Simple.g4")) {
sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
}
GenericParser gp = new GenericParser(sgrammarcontent);
DefaultTreeListener t = new DefaultTreeListener();
gp.setListener(t);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
Assertions.assertTrue(compile);
// this example shows you how one could use inmemantlr for incremental parsing
try {
ParseTree parseTree;
gp.parse("PRINT a+b");
parseTree = t.getParseTree();
LOGGER.debug(parseTree.toDot());
Assertions.assertEquals(parseTree.getNodes().size(), 6);
gp.parse("PRINT \"test\"");
parseTree = t.getParseTree();
LOGGER.debug(parseTree.toDot());
Assertions.assertEquals(parseTree.getNodes().size(), 4);
} catch (IllegalWorkflowException | ParsingException e) {
LOGGER.error(e.getMessage(), e);
}
}
Aggregations