Search in sources :

Example 6 with IllegalWorkflowException

use of org.snt.inmemantlr.exceptions.IllegalWorkflowException 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;
    }
    assertTrue(compile);
    GenericParser gp2 = new GenericParser(sgrammarcontent);
    try {
        gp2.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    assertTrue(compile);
    DefaultTreeListener l1 = new DefaultTreeListener();
    DefaultTreeListener l2 = new DefaultTreeListener();
    assertNotNull(l1.toString());
    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();
    assertNotEquals(out1, 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 7 with IllegalWorkflowException

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

the class TestSimple method testInterpreter.

@Test
public void testInterpreter() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Simple.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;
    }
    assertTrue(compile);
    // this example shows you how one could use inmemantlr for incremental parsing
    try {
        ParseTree parseTree;
        gp.parse("PRINT a+b");
        parseTree = t.getParseTree();
        LOGGER.debug(parseTree.toDot());
        assertEquals(6, parseTree.getNodes().size());
        gp.parse("PRINT \"test\"");
        parseTree = t.getParseTree();
        LOGGER.debug(parseTree.toDot());
        assertEquals(4, parseTree.getNodes().size());
    } catch (IllegalWorkflowException | ParsingException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) 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 8 with IllegalWorkflowException

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

the class TestSimple method testLogic.

@Test
public void testLogic() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Logic.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    String toParse = "hello";
    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());
    }
    assertTrue(compile);
    try {
        gp.parse(toParse);
    } catch (IllegalWorkflowException | ParsingException e) {
        assert false;
    }
    ParseTree a = t.getParseTree();
    assertEquals(4, a.getNodes().size());
    LOGGER.debug(ParseTreeSerializer.INSTANCE.toDot(a));
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) 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 9 with IllegalWorkflowException

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

the class TestNonCombinedGrammar method testParserLexer.

@Test
public void testParserLexer() throws IOException {
    LOGGER.debug("Test multi file parsing");
    File[] files = { new File(getClass().getClassLoader().getResource("inmemantlr/MySQLLexer.g4").getFile()), new File(getClass().getClassLoader().getResource("inmemantlr/MySQLParser.g4").getFile()) };
    GenericParser gp = new GenericParser(files);
    DefaultTreeListener t = new DefaultTreeListener();
    gp.setListener(t);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    try {
        ParseTree parseTree;
        gp.parse("select a from b;");
        parseTree = t.getParseTree();
        Assertions.assertEquals(13, parseTree.getNodes().size());
        LOGGER.debug(parseTree.toDot());
    } catch (IllegalWorkflowException | ParsingException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) 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 10 with IllegalWorkflowException

use of org.snt.inmemantlr.exceptions.IllegalWorkflowException 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;
    }
    assertTrue(compile);
    assertTrue(s != null && !s.isEmpty());
    DefaultTreeListener dlist = new DefaultTreeListener();
    gp.setListener(dlist);
    try {
        gp.parse(s);
    } catch (IllegalWorkflowException | ParsingException e) {
        fail();
    }
    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);
            assertNotNull(getElement(n));
        }
    };
    try {
        processor.process();
    } catch (ParseTreeProcessorException e) {
        fail();
    }
    assertNotNull(processor.debug());
}
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)

Aggregations

IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)10 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)10 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)10 ParseTree (org.snt.inmemantlr.tree.ParseTree)10 GenericParser (org.snt.inmemantlr.GenericParser)8 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)8 Test (org.junit.jupiter.api.Test)7 File (java.io.File)5 InputStream (java.io.InputStream)4 FileNotFoundException (java.io.FileNotFoundException)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 ParseTreeNode (org.snt.inmemantlr.tree.ParseTreeNode)2 ParseTreeProcessor (org.snt.inmemantlr.tree.ParseTreeProcessor)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ParseTreeProcessorException (org.snt.inmemantlr.exceptions.ParseTreeProcessorException)1