use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestDoubleParse method testProcessor.
@Test
public void testProcessor() {
GenericParser gp1 = new GenericParser(sgrammarcontent);
boolean compile;
try {
gp1.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
assertTrue(compile);
GenericParser gp2 = new GenericParser(sgrammarcontent);
try {
gp2.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
assertTrue(compile);
DefaultTreeListener l1 = new DefaultTreeListener();
DefaultTreeListener l2 = new DefaultTreeListener();
assertNotNull(l1.toString());
assertEquals("", l1.toString());
gp1.setListener(l1);
gp2.setListener(l2);
try {
gp1.parse(s1);
gp2.parse(s2);
} catch (IllegalWorkflowException | ParsingException e) {
e.printStackTrace();
}
ParseTree out1 = l1.getParseTree();
ParseTree out2 = l2.getParseTree();
assertNotEquals(out1, out2);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener 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;
}
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());
assertEquals(6, parseTree.getNodes().size());
gp.parse("PRINT \"test\"");
parseTree = t.getParseTree();
LOGGER.debug(parseTree.toDot());
assertEquals(4, parseTree.getNodes().size());
} catch (IllegalWorkflowException | ParsingException e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestSimple method testLogic.
@Test
public void testLogic() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Logic.g4")) {
sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
}
String toParse = "hello";
GenericParser gp = new GenericParser(sgrammarcontent);
DefaultTreeListener t = new DefaultTreeListener();
gp.setListener(t);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
LOGGER.debug(e.getMessage());
}
assertTrue(compile);
try {
gp.parse(toParse);
} catch (IllegalWorkflowException | ParsingException e) {
assert false;
}
ParseTree a = t.getParseTree();
assertEquals(4, a.getNodes().size());
LOGGER.debug(ParseTreeSerializer.INSTANCE.toDot(a));
}
Aggregations