Search in sources :

Example 1 with InjectionPointDetector

use of org.snt.inmemantlr.utils.InjectionPointDetector 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)

Aggregations

File (java.io.File)1 Test (org.junit.jupiter.api.Test)1 GenericParserToGo (org.snt.inmemantlr.GenericParserToGo)1 InjectionException (org.snt.inmemantlr.exceptions.InjectionException)1 ParseTree (org.snt.inmemantlr.tree.ParseTree)1 InjectionPointDetector (org.snt.inmemantlr.utils.InjectionPointDetector)1