use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestExternalGrammars method testObjc.
@Test
public void testObjc() {
if (!toCheck("objc"))
return;
Subject s = subjects.get("objc");
s.g4.add(new File("src/test/resources/grammars-v4/objc/one-step" + "-processing/ObjectiveCLexer.g4"));
s.g4.add(new File("src/test/resources/grammars-v4/objc/one-step" + "-processing/ObjectiveCPreprocessorParser.g4"));
GenericParser gp = null;
try {
gp = new GenericParser(s.g4.toArray(new File[0]));
} catch (FileNotFoundException e) {
fail();
}
DefaultTreeListener mdt = new DefaultTreeListener();
assertDoesNotThrow(gp::compile);
gp.setListener(mdt);
s.examples.removeIf(p -> p.getName().contains("AllInOne.m"));
verify(gp, s.examples, s.nexamples, s.entrypoint);
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestMalformed method testGeneration.
@Test
public void testGeneration() {
GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
Assertions.assertNotNull(gp);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
Assertions.assertTrue(compile);
String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
Assertions.assertTrue(s != null && !s.isEmpty());
DefaultTreeListener dlist = new DefaultTreeListener();
gp.setListener(dlist);
assertThrows(ParsingException.class, () -> gp.parse(s));
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class TestSimpleMixed method testInterpreter.
@Test
public void testInterpreter() throws IOException {
try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/SimpleMixed.g4")) {
sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
}
GenericParser gp = new GenericParser(sgrammarcontent);
DefaultTreeListener t = new DefaultTreeListener();
List<String> cp = new ArrayList<>();
File f = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
LOGGER.debug(f.toString());
// gp.setClassPath(cp);
gp.setListener(t);
boolean compile;
try {
gp.compile();
compile = true;
} catch (CompilationException e) {
compile = false;
}
Assertions.assertTrue(compile);
// parsing
try {
ParseTree parseTree;
gp.parse("jan 1999 12");
parseTree = t.getParseTree();
Assertions.assertEquals(6, parseTree.getNodes().size());
} catch (IllegalWorkflowException e) {
LOGGER.error(e.getMessage(), e);
} catch (ParsingException e) {
LOGGER.error(e.getMessage());
}
}
use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.
the class Inmemantlr method main.
public static void main(String[] args) {
LOGGER.info("org.snt.inmemantlr.tool.Inmemantlr tool");
HelpFormatter hformatter = new HelpFormatter();
Options options = new Options();
// Binary arguments
options.addOption("h", "print this message");
Option grmr = Option.builder().longOpt("grmrfiles").hasArgs().desc("comma-separated list of ANTLR files").required(true).argName("grmrfiles").type(String.class).valueSeparator(',').build();
Option infiles = Option.builder().longOpt("infiles").hasArgs().desc("comma-separated list of files to parseFile").required(true).argName("infiles").type(String.class).valueSeparator(',').build();
Option utilfiles = Option.builder().longOpt("utilfiles").hasArgs().desc("comma-separated list of utility files to be added for " + "compilation").required(false).argName("utilfiles").type(String.class).valueSeparator(',').build();
Option odir = Option.builder().longOpt("outdir").desc("output directory in which the dot files will be " + "created").required(false).hasArg(true).argName("outdir").type(String.class).build();
options.addOption(infiles);
options.addOption(grmr);
options.addOption(utilfiles);
options.addOption(odir);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (cmd.hasOption('h')) {
hformatter.printHelp("java -jar inmemantlr.jar", options);
System.exit(0);
}
} catch (ParseException e) {
hformatter.printHelp("java -jar inmemantlr.jar", options);
LOGGER.error(e.getMessage());
System.exit(-1);
}
// input files
Set<File> ins = getFilesForOption(cmd, "infiles");
// grammar files
Set<File> gs = getFilesForOption(cmd, "grmrfiles");
// utility files
Set<File> uf = getFilesForOption(cmd, "utilfiles");
// output dir
Set<File> od = getFilesForOption(cmd, "outdir");
if (od.size() > 1) {
LOGGER.error("output directories must be less than or equal to 1");
System.exit(-1);
}
if (ins.size() <= 0) {
LOGGER.error("no input files were specified");
System.exit(-1);
}
if (gs.size() <= 0) {
LOGGER.error("no grammar files were specified");
System.exit(-1);
}
LOGGER.info("create generic parser");
GenericParser gp = null;
try {
gp = new GenericParser(gs.toArray(new File[gs.size()]));
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage());
System.exit(-1);
}
if (!uf.isEmpty()) {
try {
gp.addUtilityJavaFiles(uf.toArray(new String[uf.size()]));
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage());
System.exit(-1);
}
}
LOGGER.info("create and add parseFile tree listener");
DefaultTreeListener dt = new DefaultTreeListener();
gp.setListener(dt);
LOGGER.info("compile generic parser");
try {
gp.compile();
} catch (CompilationException e) {
LOGGER.error("cannot compile generic parser: {}", e.getMessage());
System.exit(-1);
}
String fpfx = "";
for (File of : od) {
if (!of.exists() || !of.isDirectory()) {
LOGGER.error("output directory does not exist or is not a " + "directory");
System.exit(-1);
}
fpfx = of.getAbsolutePath();
}
ParseTree parseTree;
for (File f : ins) {
try {
gp.parse(f);
} catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
LOGGER.error(e.getMessage());
System.exit(-1);
}
parseTree = dt.getParseTree();
if (!fpfx.isEmpty()) {
String of = fpfx + "/" + FilenameUtils.removeExtension(f.getName()) + ".dot";
LOGGER.info("write file {}", of);
try {
FileUtils.writeStringToFile(new File(of), parseTree.toDot(), "UTF-8");
} catch (IOException e) {
LOGGER.error(e.getMessage());
System.exit(-1);
}
} else {
LOGGER.info("Tree for {} \n {}", f.getName(), parseTree.toDot());
}
}
System.exit(0);
}
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, GenericParser.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, GenericParser.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;
}
Assertions.assertEquals(shouldParse, parses);
if (shouldParse) {
ParseTree parseTree = dt.getParseTree();
Assertions.assertNotNull(parseTree);
LOGGER.debug(parseTree.toDot());
Assertions.assertTrue(parseTree.getNodes().size() > 1);
}
}
}
Aggregations