Search in sources :

Example 6 with ParsingException

use of org.snt.inmemantlr.exceptions.ParsingException in project inmemantlr by julianthome.

the class TestTreeGeneration method testGeneration.

@Test
public void testGeneration() {
    Assertions.assertNotNull(grammar);
    GenericParser gp = null;
    try {
        gp = new GenericParser(grammar);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    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);
    try {
        gp.parse(s);
    } catch (IllegalWorkflowException e) {
        Assertions.assertTrue(false);
    } catch (ParsingException e) {
        Assertions.assertTrue(false);
    }
    ParseTree parseTree = dlist.getParseTree();
    LOGGER.debug(parseTree.toDot());
    // create copy
    ParseTree cast = new ParseTree(parseTree);
    Assertions.assertTrue(parseTree != null);
    Assertions.assertEquals(parseTree.getNodes().size(), cast.getNodes().size());
    Set<ParseTree> parseTrees = parseTree.getSubtrees(n -> "expression".equals(n.getRule()));
    Assertions.assertTrue(parseTrees.size() == 5);
    for (ParseTree a : parseTrees) {
        Assertions.assertTrue(parseTree.hasSubtree(a));
        Assertions.assertFalse(parseTree.getSubtree(a) == null);
    }
    int sizeBefore = parseTree.getNodes().size();
    ParseTree first = parseTrees.iterator().next();
    parseTree.removeSubtree(first);
    Assertions.assertTrue(parseTree.getNodes().size() + first.getNodes().size() == sizeBefore);
    ParseTree repl = new ParseTree("replacement", "replacement");
    cast.replaceSubtree(first, repl);
    Assertions.assertTrue(cast.getNodes().size() == parseTree.getNodes().size() + 1);
    Assertions.assertTrue(cast.getDominatingSubtrees(n -> "classBody".equals(n.getRule())).size() == 1);
    Assertions.assertTrue(cast.toDot() != null && !cast.toDot().isEmpty());
    ParseTreeNode root = cast.getRoot();
    Assertions.assertTrue(root.hasChildren());
    Assertions.assertFalse(root.hasParent());
    for (ParseTreeNode n : cast.getNodes()) {
        Assertions.assertTrue(n.getLabel() != null);
        Assertions.assertTrue(n.getRule() != null);
        for (int i = 0; i < n.getChildren().size(); i++) {
            if (i == 0)
                Assertions.assertTrue(n.getChild(i).equals(n.getFirstChild()));
            if (i == n.getChildren().size() - 1)
                Assertions.assertTrue(n.getChild(i).equals(n.getLastChild()));
        }
    }
    for (ParseTreeNode c : cast.getLeafs()) {
        Assertions.assertTrue(c.hasParent());
        Assertions.assertFalse(c.hasChildren());
        Assertions.assertTrue(c.isLeaf());
        Assertions.assertFalse(c.equals(null));
        Assertions.assertFalse(c.equals(null));
        Assertions.assertNull(c.getLastChild());
        Assertions.assertNull(c.getFirstChild());
    }
    ParseTreeNode croot = cast.getRoot();
    croot.setParent(parseTree.getRoot());
    Assertions.assertTrue(croot.getParent().equals(parseTree.getRoot()));
}
Also used : Logger(org.slf4j.Logger) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) Set(java.util.Set) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.jupiter.api.Test) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) ParseTreeNode(org.snt.inmemantlr.tree.ParseTreeNode) Assertions(org.junit.jupiter.api.Assertions) ParseTree(org.snt.inmemantlr.tree.ParseTree) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) FileNotFoundException(java.io.FileNotFoundException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) GenericParser(org.snt.inmemantlr.GenericParser) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) ParseTreeNode(org.snt.inmemantlr.tree.ParseTreeNode) Test(org.junit.jupiter.api.Test)

Example 7 with ParsingException

use of org.snt.inmemantlr.exceptions.ParsingException 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 8 with ParsingException

use of org.snt.inmemantlr.exceptions.ParsingException in project inmemantlr by julianthome.

the class TestDoubleParse method testProcessor.

@Test
public void testProcessor() {
    GenericParser gp1 = new GenericParser(sgrammarcontent);
    boolean compile;
    try {
        gp1.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    GenericParser gp2 = new GenericParser(sgrammarcontent);
    try {
        gp2.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    DefaultTreeListener l1 = new DefaultTreeListener();
    DefaultTreeListener l2 = new DefaultTreeListener();
    Assertions.assertFalse(l1.toString() == null);
    Assertions.assertEquals(l1.toString(), "");
    gp1.setListener(l1);
    gp2.setListener(l2);
    try {
        gp1.parse(s1);
        gp2.parse(s2);
    } catch (IllegalWorkflowException | ParsingException e) {
        e.printStackTrace();
    }
    ParseTree out1 = l1.getParseTree();
    ParseTree out2 = l2.getParseTree();
    Assertions.assertFalse(out1.equals(out2));
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 9 with ParsingException

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

Example 10 with ParsingException

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

Aggregations

IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)14 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)14 GenericParser (org.snt.inmemantlr.GenericParser)13 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)13 Test (org.junit.jupiter.api.Test)12 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)12 ParseTree (org.snt.inmemantlr.tree.ParseTree)11 File (java.io.File)7 FileNotFoundException (java.io.FileNotFoundException)6 InputStream (java.io.InputStream)5 ParseTreeNode (org.snt.inmemantlr.tree.ParseTreeNode)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 ParseTreeProcessorException (org.snt.inmemantlr.exceptions.ParseTreeProcessorException)2 ParseTreeProcessor (org.snt.inmemantlr.tree.ParseTreeProcessor)2 IOException (java.io.IOException)1 List (java.util.List)1 Set (java.util.Set)1 Vector (java.util.Vector)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 Assertions (org.junit.jupiter.api.Assertions)1