Search in sources :

Example 1 with ParseTreeNode

use of org.snt.inmemantlr.tree.ParseTreeNode 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 2 with ParseTreeNode

use of org.snt.inmemantlr.tree.ParseTreeNode 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 3 with ParseTreeNode

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

the class DefaultTreeListener method visitTerminal.

@Override
public void visitTerminal(TerminalNode terminalNode) {
    if (includeTerminals) {
        ParseTreeNode n = parseTree.newNode(nodeptr, "", terminalNode.toString(), terminalNode.getSymbol().getStartIndex(), terminalNode.getSymbol().getStopIndex());
        nodeptr.addChild(n);
    }
}
Also used : ParseTreeNode(org.snt.inmemantlr.tree.ParseTreeNode)

Example 4 with ParseTreeNode

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

the class TestParseTreeProcessor method testProcessor.

@Test
public void testProcessor() {
    GenericParser gp = new GenericParser(sgrammarcontent);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    Assertions.assertTrue(s != null && !s.isEmpty());
    DefaultTreeListener dlist = new DefaultTreeListener();
    gp.setListener(dlist);
    try {
        gp.parse(s);
    } catch (IllegalWorkflowException | ParsingException e) {
        Assertions.assertTrue(false);
    }
    ParseTree parseTree = dlist.getParseTree();
    // Process the tree bottom up
    ParseTreeProcessor<String, String> processor = new ParseTreeProcessor<String, String>(parseTree) {

        int cnt = 0;

        @Override
        public String getResult() {
            return String.valueOf(cnt);
        }

        @Override
        protected void initialize() {
            for (ParseTreeNode n : this.parseTree.getNodes()) {
                smap.put(n, "");
            }
        }

        @Override
        protected void process(ParseTreeNode n) {
            cnt++;
            simpleProp(n);
            Assertions.assertTrue(getElement(n) != null);
        }
    };
    try {
        processor.process();
    } catch (ParseTreeProcessorException e) {
        Assertions.assertFalse(true);
    }
    Assertions.assertTrue(processor.debug() != null);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) 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 5 with ParseTreeNode

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

the class DefaultTreeListener method enterEveryRule.

@Override
public void enterEveryRule(ParserRuleContext ctx) {
    String rule = getRuleByKey(ctx.getRuleIndex());
    if (filter.test(rule)) {
        Token s = ctx.getStart();
        Token e = ctx.getStop();
        ParseTreeNode n = parseTree.newNode(nodeptr, rule, ctx.getText(), s != null ? s.getStartIndex() : 0, e != null ? e.getStopIndex() : 0);
        nodeptr.addChild(n);
        nodeptr = n;
    }
}
Also used : Token(org.antlr.v4.runtime.Token) ParseTreeNode(org.snt.inmemantlr.tree.ParseTreeNode)

Aggregations

ParseTreeNode (org.snt.inmemantlr.tree.ParseTreeNode)5 Test (org.junit.jupiter.api.Test)3 GenericParser (org.snt.inmemantlr.GenericParser)3 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)3 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)3 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)3 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)3 ParseTree (org.snt.inmemantlr.tree.ParseTree)3 ParseTreeProcessorException (org.snt.inmemantlr.exceptions.ParseTreeProcessorException)2 ParseTreeProcessor (org.snt.inmemantlr.tree.ParseTreeProcessor)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Set (java.util.Set)1 Token (org.antlr.v4.runtime.Token)1 Assertions (org.junit.jupiter.api.Assertions)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 FileUtils (org.snt.inmemantlr.utils.FileUtils)1