use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testPlsql.
@Test
public void testPlsql() {
if (!toCheck("plsql"))
return;
Subject s = subjects.get("plsql");
Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("PlSql" + "(Lexer|Parser).g4")).collect(Collectors.toSet());
Assertions.assertTrue(mfiles.size() > 0);
GenericParser mparser = null;
try {
mparser = new GenericParser(mfiles.toArray(new File[mfiles.size()]));
} catch (FileNotFoundException e) {
Assertions.assertTrue(false);
}
Assertions.assertNotNull(mparser);
DefaultTreeListener mdt = new DefaultTreeListener();
boolean compile;
try {
mparser.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
mparser.setStreamProvider(new CasedStreamProvider(GenericParser.CaseSensitiveType.UPPER));
mparser.setListener(mdt);
Assertions.assertTrue(compile);
verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestArtifactSerialization method testBroken.
@Test
public void testBroken() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Simple.g4")) {
sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
}
ToolCustomizer tc = new ToolCustomizer() {
@Override
public void customize(Tool t) {
t.genPackage = "com.github.inmemantlr.parser";
}
};
GenericParser gp = new GenericParser(tc, sgrammarcontent);
DefaultTreeListener t = new DefaultTreeListener();
gp.setListener(t);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
gp.writeAntlrAritfactsTo("/tmp/test/out");
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestMemObjects method testStoreAndLoad.
@Test
public void testStoreAndLoad() throws CompilationException {
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
assertNotNull(gp);
File file = assertDoesNotThrow(() -> File.createTempFile("temp", Long.toString(System.nanoTime())));
file.mkdir();
gp.compile();
String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
assertTrue(s != null && !s.isEmpty());
assertDoesNotThrow(() -> {
gp.parse(s);
});
assertDoesNotThrow(() -> {
gp.store(file.getAbsolutePath(), true);
});
GenericParser cgp = assertDoesNotThrow(() -> GenericParser.load(file.getAbsolutePath()));
assertDoesNotThrow(() -> {
cgp.parse(s);
});
DefaultTreeListener dlist = new DefaultTreeListener();
cgp.setListener(dlist);
assertDoesNotThrow(() -> {
cgp.parse(s);
});
assertNotNull(dlist.getParseTree());
LOGGER.debug(dlist.getParseTree().toDot());
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestParseTreeProcessorEvaluation method testInterpreter.
@Test
public void testInterpreter() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Ops.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("3+100");
parseTree = t.getParseTree();
// Process the tree bottom up
ParseTreeProcessor<String, String> processor = new ParseTreeProcessor<String, String>(parseTree) {
@Override
public String getResult() {
return smap.get(this.parseTree.getRoot());
}
@Override
protected void initialize() {
this.parseTree.getNodes().forEach(n -> smap.put(n, n.getLabel()));
}
@Override
protected void process(ParseTreeNode n) {
LOGGER.debug("id " + n.getId());
if (n.getRule().equals("expression")) {
int n0 = Integer.parseInt(smap.get(n.getChild(0)));
int n1 = Integer.parseInt(smap.get(n.getChild(2)));
int result = 0;
switch(smap.get(n.getChild(1))) {
case "+":
result = n0 + n1;
break;
case "-":
result = n0 - n1;
break;
}
smap.put(n, String.valueOf(result));
} else
simpleProp(n);
}
};
assertDoesNotThrow((Executable) processor::process);
assertEquals(7, parseTree.getNodes().size());
assertEquals("103", processor.getResult());
} 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 testOclStringParsing.
@Test
public void testOclStringParsing() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/DeepOcl.g4")) {
sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
}
String toParse = "context Dependent inv inv54: (self.birthyear " + ">=2012 and self.allowances->size()=1) or (self.birthyear < " + "2012 and self.birthyear >= 1996)";
GenericParser gp = new GenericParser(sgrammarcontent);
DefaultTreeListener t = new DefaultTreeListener();
gp.setListener(t);
assertDoesNotThrow(gp::compile);
ParserRuleContext ctx = assertDoesNotThrow(() -> gp.parse(toParse));
LOGGER.info("ctx {}", ctx.getChild(0).getText());
ParseTree a = t.getParseTree();
assertTrue(a.getNodes().size() > 1);
LOGGER.debug(a.toDot());
}
Aggregations