Search in sources :

Example 1 with ParseTree

use of org.snt.inmemantlr.tree.ParseTree in project inmemantlr by julianthome.

the class TestGenericParserToGo method testParser.

@Test
public void testParser() {
    File gfile = new File(TestGenericParserToGo.class.getClassLoader().getResource("inmemantlr/Java.g4").getFile());
    File cfile = new File(TestGenericParserToGo.class.getClassLoader().getResource("inmemantlr/HelloWorld.java").getFile());
    ParseTree pt = new GenericParserToGo(gfile).parse(cfile, "compilationUnit");
    Assertions.assertEquals(pt.getNodes().size(), 60);
}
Also used : GenericParserToGo(org.snt.inmemantlr.GenericParserToGo) File(java.io.File) ParseTree(org.snt.inmemantlr.tree.ParseTree) Test(org.junit.jupiter.api.Test)

Example 2 with ParseTree

use of org.snt.inmemantlr.tree.ParseTree in project inmemantlr by julianthome.

the class TestParseTreeInjection method testInjection.

@Test
public void testInjection() {
    File gfile = new File(TestGenericParserToGo.class.getClassLoader().getResource("inmemantlr/Java.g4").getFile());
    File cfile = new File(TestGenericParserToGo.class.getClassLoader().getResource("inmemantlr/HelloWorld.java").getFile());
    GenericParserToGo g4go = new GenericParserToGo(gfile);
    ParseTree pt = g4go.parse(cfile, "compilationUnit");
    LOGGER.debug(pt.toDot());
    InjectionPointDetector ipdb = new InjectionPointDetector() {

        @Override
        public ParseTreeNode detect(ParseTree t) {
            return t.getNodes().stream().filter(n -> n.getRule().equals("block")).findFirst().orElse(null);
        }

        @Override
        public Position getPosition() {
            return Position.BEFORE;
        }
    };
    InjectionPointDetector ipda = new InjectionPointDetector() {

        @Override
        public ParseTreeNode detect(ParseTree t) {
            return t.getNodes().stream().filter(n -> n.getRule().equals("block")).findFirst().orElse(null);
        }

        @Override
        public Position getPosition() {
            return Position.AFTER;
        }
    };
    ParseTree inj = g4go.parse("int x = 0;", "blockStatement");
    try {
        ParseTreeManipulator.INTANCE.inject(pt, ipdb, inj);
        ParseTreeManipulator.INTANCE.inject(pt, ipda, inj);
    } catch (InjectionException e) {
        Assertions.assertTrue(false);
        LOGGER.error(e.getMessage());
    }
    Assertions.assertEquals(pt.getNodes().size(), 94);
}
Also used : InjectionException(org.snt.inmemantlr.exceptions.InjectionException) InjectionPointDetector(org.snt.inmemantlr.utils.InjectionPointDetector) GenericParserToGo(org.snt.inmemantlr.GenericParserToGo) File(java.io.File) ParseTree(org.snt.inmemantlr.tree.ParseTree) Test(org.junit.jupiter.api.Test)

Example 3 with ParseTree

use of org.snt.inmemantlr.tree.ParseTree in project inmemantlr by julianthome.

the class TestParseTreeProcessorEvaluation method testInterpreter.

@Test
public void testInterpreter() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Ops.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.assertTrue(compile);
    // this example shows you how one could use inmemantlr for incremental parsing
    try {
        ParseTree parseTree;
        gp.parse("3+100");
        parseTree = t.getParseTree();
        // Process the tree bottom up
        ParseTreeProcessor<String, String> processor = new ParseTreeProcessor<String, String>(parseTree) {

            @Override
            public String getResult() {
                return smap.get(this.parseTree.getRoot());
            }

            @Override
            protected void initialize() {
                this.parseTree.getNodes().forEach(n -> smap.put(n, n.getLabel()));
            }

            @Override
            protected void process(ParseTreeNode n) {
                LOGGER.debug("id " + n.getId());
                if (n.getRule().equals("expression")) {
                    int n0 = Integer.parseInt(smap.get(n.getChild(0)));
                    int n1 = Integer.parseInt(smap.get(n.getChild(2)));
                    int result = 0;
                    switch(smap.get(n.getChild(1))) {
                        case "+":
                            result = n0 + n1;
                            break;
                        case "-":
                            result = n0 - n1;
                            break;
                    }
                    smap.put(n, String.valueOf(result));
                } else
                    simpleProp(n);
            }
        };
        try {
            processor.process();
        } catch (ParseTreeProcessorException e) {
            Assertions.assertFalse(true);
        }
        Assertions.assertEquals(parseTree.getNodes().size(), 7);
        Assertions.assertEquals(processor.getResult(), "103");
    } catch (IllegalWorkflowException | ParsingException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) ParseTreeProcessorException(org.snt.inmemantlr.exceptions.ParseTreeProcessorException) GenericParser(org.snt.inmemantlr.GenericParser) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) ParseTreeProcessor(org.snt.inmemantlr.tree.ParseTreeProcessor) 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 4 with ParseTree

use of org.snt.inmemantlr.tree.ParseTree in project inmemantlr by julianthome.

the class TestSimple method testOclStringParsing.

@Test
public void testOclStringParsing() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/DeepOcl.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    String toParse = "context Dependent inv inv54: (self.birthyear " + ">=2012 and self.allowances->size()=1) or (self.birthyear < " + "2012 and self.birthyear >= 1996)";
    GenericParser gp = new GenericParser(sgrammarcontent);
    DefaultTreeListener t = new DefaultTreeListener();
    gp.setListener(t);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
        LOGGER.debug(e.getMessage());
    }
    Assertions.assertTrue(compile);
    try {
        ParserRuleContext ctx = gp.parse(toParse);
        LOGGER.info("ctx {}", ctx.getChild(0).getText());
    } catch (IllegalWorkflowException | ParsingException e) {
        Assertions.assertTrue(false);
    }
    ParseTree a = t.getParseTree();
    Assertions.assertTrue(a.getNodes().size() > 1);
    LOGGER.debug(a.toDot());
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) InputStream(java.io.InputStream) 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 5 with ParseTree

use of org.snt.inmemantlr.tree.ParseTree 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 cp = new Vector<String>();
    final 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(parseTree.getNodes().size(), 6);
    } 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) List(java.util.List) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) Vector(java.util.Vector) 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)

Aggregations

ParseTree (org.snt.inmemantlr.tree.ParseTree)14 Test (org.junit.jupiter.api.Test)11 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)11 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)11 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)11 GenericParser (org.snt.inmemantlr.GenericParser)10 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)10 File (java.io.File)7 InputStream (java.io.InputStream)5 FileNotFoundException (java.io.FileNotFoundException)3 ParseTreeNode (org.snt.inmemantlr.tree.ParseTreeNode)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 GenericParserToGo (org.snt.inmemantlr.GenericParserToGo)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