use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestTreeGeneration method testGeneration.
@Test
public void testGeneration() {
assertNotNull(grammar);
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
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());
DefaultTreeListener dlist = new DefaultTreeListener();
gp.setListener(dlist);
assertDoesNotThrow(() -> {
gp.parse(s);
});
ParseTree parseTree = dlist.getParseTree();
LOGGER.debug(parseTree.toDot());
// create copy
ParseTree cast = new ParseTree(parseTree);
assertNotNull(parseTree);
assertEquals(parseTree.getNodes().size(), cast.getNodes().size());
Set<ParseTree> parseTrees = parseTree.getSubtrees(n -> "expression".equals(n.getRule()));
assertEquals(5, parseTrees.size());
for (ParseTree a : parseTrees) {
assertTrue(parseTree.hasSubtree(a));
assertNotNull(parseTree.getSubtree(a));
}
int sizeBefore = parseTree.getNodes().size();
ParseTree first = parseTrees.iterator().next();
parseTree.removeSubtree(first);
assertEquals(parseTree.getNodes().size() + first.getNodes().size(), sizeBefore);
ParseTree repl = new ParseTree("replacement", "replacement");
cast.replaceSubtree(first, repl);
assertEquals(cast.getNodes().size(), parseTree.getNodes().size() + 1);
assertEquals(1, cast.getDominatingSubtrees(n -> "classBody".equals(n.getRule())).size());
assertTrue(cast.toDot() != null && !cast.toDot().isEmpty());
ParseTreeNode root = cast.getRoot();
assertTrue(root.hasChildren());
assertFalse(root.hasParent());
for (ParseTreeNode n : cast.getNodes()) {
assertNotNull(n.getLabel());
assertNotNull(n.getRule());
for (int i = 0; i < n.getChildren().size(); i++) {
if (i == 0)
assertEquals(n.getChild(i), n.getFirstChild());
if (i == n.getChildren().size() - 1)
assertEquals(n.getChild(i), n.getLastChild());
}
}
for (ParseTreeNode c : cast.getLeafs()) {
assertTrue(c.hasParent());
assertFalse(c.hasChildren());
assertTrue(c.isLeaf());
assertNull(c.getLastChild());
assertNull(c.getFirstChild());
}
ParseTreeNode croot = cast.getRoot();
croot.setParent(parseTree.getRoot());
assertEquals(croot.getParent(), parseTree.getRoot());
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestBroken method testBroken.
@Test
public void testBroken() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Broken.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.assertFalse(compile);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testCSharp.
@Test
public void testCSharp() {
// being -- csharp grammar is runtime dependent
if (System.getProperty("os.name").toLowerCase().startsWith("win"))
return;
if (!toCheck("csharp"))
return;
Subject s = subjects.get("csharp");
Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("CSharp" + "(Lexer|PreprocessorParser|Parser).g4")).collect(Collectors.toSet());
assertFalse(mfiles.isEmpty());
GenericParser mparser = null;
try {
mparser = new GenericParser(mfiles.toArray(new File[0]));
} catch (FileNotFoundException e) {
fail();
}
assertNotNull(mparser);
DefaultTreeListener mdt = new DefaultTreeListener();
assertDoesNotThrow(mparser::compile);
mparser.setListener(mdt);
mparser.setParserName("CSharpParser");
verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testMySQL.
@Test
public void testMySQL() {
if (!toCheck("mysql"))
return;
Subject s = subjects.get("mysql");
Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("MySql" + "(Lexer|Parser).g4")).collect(Collectors.toSet());
assertFalse(mfiles.isEmpty());
GenericParser mparser = null;
try {
mparser = new GenericParser(mfiles.toArray(new File[0]));
} catch (FileNotFoundException e) {
fail();
}
assertNotNull(mparser);
DefaultTreeListener mdt = new DefaultTreeListener();
assertDoesNotThrow(mparser::compile);
mparser.setStreamProvider(new CasedStreamProvider(CaseSensitiveType.UPPER));
mparser.setListener(mdt);
verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testTSql.
@Test
public void testTSql() {
if (!toCheck("tsql"))
return;
Subject s = subjects.get("tsql");
Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("TSql(Lexer|Parser).g4")).collect(Collectors.toSet());
assertFalse(mfiles.isEmpty());
GenericParser mparser = null;
try {
mparser = new GenericParser(mfiles.toArray(new File[0]));
} catch (FileNotFoundException e) {
fail();
}
assertNotNull(mparser);
DefaultTreeListener mdt = new DefaultTreeListener();
assertDoesNotThrow(mparser::compile);
mparser.setStreamProvider(new CasedStreamProvider(CaseSensitiveType.UPPER));
mparser.setListener(mdt);
// seems to cause issues when running on windows
s.examples.removeIf(f -> f.getName().equals("full_width_chars.sql"));
verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
Aggregations