use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method verify.
private void verify(GenericParser p, Set<File> ok, Set<File> error, String ep) {
DefaultTreeListener dt = new DefaultTreeListener();
verify(p, ok, ep, CaseSensitiveType.NONE, true);
verify(p, error, ep, CaseSensitiveType.NONE, false);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testStringTemplate.
@Test
public void testStringTemplate() {
if (!toCheck("stringtemplate"))
return;
Subject s = subjects.get("stringtemplate");
// LOGGER.info("G4 {}", s.g4);
// Exam
ToolCustomizer tc = t -> t.genPackage = "org.antlr.parser.st4";
GenericParser gp = getParserForSubject(s, tc);
DefaultTreeListener dt = new DefaultTreeListener();
gp.setListener(dt);
assertDoesNotThrow(() -> {
File util = new File("src/test/resources/grammars-v4/stringtemplate/" + "src/main/java/org/antlr/parser/st4/LexerAdaptor.java");
gp.addUtilityJavaFiles(util);
});
assertDoesNotThrow(gp::compile);
LOGGER.debug("name {}", gp.getParserName());
gp.setParserName("org.antlr.parser.st4.STParser");
gp.setLexerName("org.antlr.parser.st4.STLexer");
s.examples = s.examples.stream().filter(f -> f.getName().contains("example1.st")).collect(Collectors.toSet());
verify(gp, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method verify.
private void verify(GenericParser p, Set<File> toParse, String ep, CaseSensitiveType t, boolean shouldParse) {
DefaultTreeListener dt = new DefaultTreeListener();
p.setListener(dt);
boolean parses;
for (File e : toParse) {
try {
LOGGER.info("parseFile {} with {} and ept {}", e.getName(), p.getParserName(), ep);
ParserRuleContext ctx = (ep != null && !ep.isEmpty()) ? p.parse(e, ep, CaseSensitiveType.NONE) : p.parse(e, t);
// Assert.Assertions.assertNotNull(ctx);
if (ctx == null)
parses = false;
else
parses = true;
} catch (IllegalWorkflowException | FileNotFoundException | RecognitionException | ParsingException e1) {
LOGGER.error("error: {}", e1.getMessage());
parses = false;
}
assertEquals(shouldParse, parses);
if (shouldParse) {
ParseTree parseTree = dt.getParseTree();
assertNotNull(parseTree);
LOGGER.debug(parseTree.toDot());
assertTrue(parseTree.getNodes().size() > 1);
}
}
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testAntlr4.
@Test
public void testAntlr4() {
if (!toCheck("antlr4"))
return;
Subject s = subjects.get("antlr4");
// Exam
ToolCustomizer tc = t -> t.genPackage = "org.antlr.parser.antlr4";
Set<File> files = s.g4.stream().filter(v -> v.getName().matches("(ANTLRv4(Lexer|Parser)|LexBasic).g4")).collect(Collectors.toSet());
assertTrue(!files.isEmpty());
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(tc, files.toArray(new File[0])));
DefaultTreeListener dt = new DefaultTreeListener();
gp.setListener(dt);
assertDoesNotThrow(() -> gp.addUtilityJavaFiles(new File("src/test/resources/grammars-v4/antlr4/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java")));
assertDoesNotThrow(gp::compile);
s.examples.removeIf(f -> f.getName().contains("three.g4"));
verify(gp, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testSubject.
private void testSubject(Subject s, boolean skip) {
LOGGER.debug("test {}", s.name);
if (specialCases.contains(s.name) && skip) {
LOGGER.debug("skip {}", s.name);
return;
}
LOGGER.info("test {}", s.name);
GenericParser gp = getParserForSubject(s, null);
assertDoesNotThrow(gp::compile);
LOGGER.debug("successfully compiled grammar");
DefaultTreeListener dt = new DefaultTreeListener();
gp.setListener(dt);
verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Aggregations