use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.
the class TestGrammarImport method testOncrpc.
@Test
public void testOncrpc() {
boolean thrown = false;
File[] fls = new File[] { new File("src/test/resources/inmemantlr/xdr.g4"), new File("src/test/resources/inmemantlr/oncrpcv2.g4") };
GenericParser gp = null;
try {
gp = new GenericParser(fls);
} catch (FileNotFoundException e) {
thrown = true;
}
Assertions.assertFalse(thrown);
try {
gp.compile();
} catch (CompilationException e) {
thrown = true;
}
try {
gp.parse(new File("src/test/resources/inmemantlr/CalculatorService.x"), "oncrpcv2Specification", GenericParser.CaseSensitiveType.NONE);
} catch (IllegalWorkflowException e) {
thrown = true;
} catch (ParsingException e) {
thrown = true;
} catch (FileNotFoundException e) {
thrown = true;
}
Assertions.assertFalse(thrown);
}
use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.
the class TestHello method testInterpreter.
@Test
public void testInterpreter() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Hello.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);
}
use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.
the class TestNonCombinedGrammar method testParserLexer.
@Test
public void testParserLexer() throws IOException {
LOGGER.debug("Test multi file parsing");
File[] files = { new File(getClass().getClassLoader().getResource("inmemantlr/MySQLLexer.g4").getFile()), new File(getClass().getClassLoader().getResource("inmemantlr/MySQLParser.g4").getFile()) };
GenericParser gp = new GenericParser(files);
DefaultTreeListener t = new DefaultTreeListener();
gp.setListener(t);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
Assertions.assertTrue(compile);
try {
ParseTree parseTree;
gp.parse("select a from b;");
parseTree = t.getParseTree();
Assertions.assertEquals(parseTree.getNodes().size(), 13);
LOGGER.debug(parseTree.toDot());
} catch (IllegalWorkflowException | ParsingException e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.snt.inmemantlr.exceptions.CompilationException 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);
}
use of org.snt.inmemantlr.exceptions.CompilationException 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());
}
Assertions.assertTrue(compile);
try {
gp.parse(toParse);
} catch (IllegalWorkflowException | ParsingException e) {
assert false;
}
ParseTree a = t.getParseTree();
Assertions.assertEquals(a.getNodes().size(), 4);
LOGGER.debug(ParseTreeSerializer.INSTANCE.toDot(a, true));
}
Aggregations