use of org.snt.inmemantlr.exceptions.ParseTreeProcessorException 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;
}
Assertions.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);
}
};
try {
processor.process();
} catch (ParseTreeProcessorException e) {
Assertions.assertFalse(true);
}
Assertions.assertEquals(parseTree.getNodes().size(), 7);
Assertions.assertEquals(processor.getResult(), "103");
} catch (IllegalWorkflowException | ParsingException e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.snt.inmemantlr.exceptions.ParseTreeProcessorException in project inmemantlr by julianthome.
the class TestParseTreeProcessor method testProcessor.
@Test
public void testProcessor() {
GenericParser gp = new GenericParser(sgrammarcontent);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
Assertions.assertTrue(compile);
Assertions.assertTrue(s != null && !s.isEmpty());
DefaultTreeListener dlist = new DefaultTreeListener();
gp.setListener(dlist);
try {
gp.parse(s);
} catch (IllegalWorkflowException | ParsingException e) {
Assertions.assertTrue(false);
}
ParseTree parseTree = dlist.getParseTree();
// Process the tree bottom up
ParseTreeProcessor<String, String> processor = new ParseTreeProcessor<String, String>(parseTree) {
int cnt = 0;
@Override
public String getResult() {
return String.valueOf(cnt);
}
@Override
protected void initialize() {
for (ParseTreeNode n : this.parseTree.getNodes()) {
smap.put(n, "");
}
}
@Override
protected void process(ParseTreeNode n) {
cnt++;
simpleProp(n);
Assertions.assertTrue(getElement(n) != null);
}
};
try {
processor.process();
} catch (ParseTreeProcessorException e) {
Assertions.assertFalse(true);
}
Assertions.assertTrue(processor.debug() != null);
}
Aggregations