use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.
the class TestMemObjects method testAntlrObjectAccess.
@Test
public void testAntlrObjectAccess() {
GenericParser gp = null;
try {
gp = new GenericParser(grammar);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assertNotNull(gp);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
assertTrue(compile);
String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
assertTrue(s != null && !s.isEmpty());
MemoryTupleSet set = gp.getAllCompiledObjects();
assertTrue(set != null && set.size() == 4);
for (MemoryTuple tup : set) {
LOGGER.debug("tuple name {}", tup.getClassName());
// for printing the source code
LOGGER.debug("source {}", tup.getSource().getClassName());
// for printing the byte code
for (MemoryByteCode mc : tup.getByteCodeObjects()) {
Objects.requireNonNull(mc, "MemoryByteCode must not be null");
LOGGER.debug("bc name: {}", mc.getClassName());
if (mc.isInnerClass()) {
assertTrue(mc.getClassName().startsWith(tup.getSource().getClassName()));
} else {
assertEquals(tup.getSource().getClassName(), mc.getClassName());
}
}
}
}
use of org.snt.inmemantlr.GenericParser 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.GenericParser 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));
}
use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.
the class TestExternalGrammars method getParserForSubject.
private GenericParser getParserForSubject(Subject s, ToolCustomizer tc) {
File[] gs = s.g4.toArray(new File[0]);
LOGGER.debug("gs {}", gs.length);
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(tc, gs));
assertNotNull(gp);
return gp;
}
use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.
the class TestExternalGrammars method testSwift2.
@Test
public void testSwift2() {
if (!toCheck("swift2"))
return;
Subject s = subjects.get("swift2");
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(s.g4.toArray(new File[0])));
assertDoesNotThrow(() -> {
File util = new File("src/test/resources/grammars-v4/swift2/src/main/java" + "/SwiftSupport.java");
gp.addUtilityJavaFiles(util);
});
assertDoesNotThrow(gp::compile);
verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Aggregations