Search in sources :

Example 16 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestGrammarImport method testLogic.

@Test
public void testLogic() {
    File[] fls = { new File("src/test/resources/inmemantlr/Logic.g4"), new File("src/test/resources/inmemantlr/Extlogic.g4") };
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(fls));
    assertDoesNotThrow(gp::compile);
    String toParse = "( XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2  and  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX3 ) " + "or ( XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX4  and  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX5  " + "and  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX6 )";
    assertDoesNotThrow(() -> {
        gp.parse(toParse);
    });
}
Also used : File(java.io.File) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 17 with GenericParser

use of org.snt.inmemantlr.GenericParser 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());
    }
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) ArrayList(java.util.ArrayList) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 18 with GenericParser

use of org.snt.inmemantlr.GenericParser 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);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) FileNotFoundException(java.io.FileNotFoundException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) IOException(java.io.IOException) GenericParser(org.snt.inmemantlr.GenericParser) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree)

Example 19 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestExternalGrammars method testHTML.

@Test
public void testHTML() {
    if (!toCheck("html"))
        return;
    Subject s = subjects.get("html");
    GenericParser gp = getParserForSubject(s, null);
    assertDoesNotThrow(gp::compile);
    s.examples.removeIf(p -> !p.getName().contains("antlr"));
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 20 with GenericParser

use of org.snt.inmemantlr.GenericParser 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);
}
Also used : java.util(java.util) CaseSensitiveType(org.snt.inmemantlr.GenericParser.CaseSensitiveType) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) Document(org.w3c.dom.Document) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) ParseTree(org.snt.inmemantlr.tree.ParseTree) CasedStreamProvider(org.snt.inmemantlr.stream.CasedStreamProvider) GenericParser(org.snt.inmemantlr.GenericParser) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.jupiter.api.Test) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Assertions(org.junit.jupiter.api.Assertions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) FilenameUtils(org.apache.commons.io.FilenameUtils) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Aggregations

GenericParser (org.snt.inmemantlr.GenericParser)40 Test (org.junit.jupiter.api.Test)37 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)25 File (java.io.File)18 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)18 ParseTree (org.snt.inmemantlr.tree.ParseTree)16 FileNotFoundException (java.io.FileNotFoundException)15 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)14 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)14 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 ToolCustomizer (org.snt.inmemantlr.tool.ToolCustomizer)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)7 Assertions (org.junit.jupiter.api.Assertions)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 CasedStreamProvider (org.snt.inmemantlr.stream.CasedStreamProvider)7 FileUtils (org.snt.inmemantlr.utils.FileUtils)7 java.util (java.util)6 Collectors (java.util.stream.Collectors)6