Search in sources :

Example 1 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestExternalGrammars method testPlsql.

@Test
public void testPlsql() {
    if (!toCheck("plsql"))
        return;
    Subject s = subjects.get("plsql");
    Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("PlSql" + "(Lexer|Parser).g4")).collect(Collectors.toSet());
    Assertions.assertTrue(mfiles.size() > 0);
    GenericParser mparser = null;
    try {
        mparser = new GenericParser(mfiles.toArray(new File[mfiles.size()]));
    } catch (FileNotFoundException e) {
        Assertions.assertTrue(false);
    }
    Assertions.assertNotNull(mparser);
    DefaultTreeListener mdt = new DefaultTreeListener();
    boolean compile;
    try {
        mparser.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    mparser.setStreamProvider(new CasedStreamProvider(GenericParser.CaseSensitiveType.UPPER));
    mparser.setListener(mdt);
    Assertions.assertTrue(compile);
    verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
Also used : java.util(java.util) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) Document(org.w3c.dom.Document) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) ParseTree(org.snt.inmemantlr.tree.ParseTree) CasedStreamProvider(org.snt.inmemantlr.stream.CasedStreamProvider) GenericParser(org.snt.inmemantlr.GenericParser) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.jupiter.api.Test) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Assertions(org.junit.jupiter.api.Assertions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) FilenameUtils(org.apache.commons.io.FilenameUtils) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) CasedStreamProvider(org.snt.inmemantlr.stream.CasedStreamProvider) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestArtifactSerialization method testBroken.

@Test
public void testBroken() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Simple.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    ToolCustomizer tc = new ToolCustomizer() {

        @Override
        public void customize(Tool t) {
            t.genPackage = "com.github.inmemantlr.parser";
        }
    };
    GenericParser gp = new GenericParser(tc, sgrammarcontent);
    DefaultTreeListener t = new DefaultTreeListener();
    gp.setListener(t);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    gp.writeAntlrAritfactsTo("/tmp/test/out");
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) Tool(org.antlr.v4.Tool) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestMemObjects method testStoreAndLoad.

@Test
public void testStoreAndLoad() throws CompilationException {
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
    assertNotNull(gp);
    File file = assertDoesNotThrow(() -> File.createTempFile("temp", Long.toString(System.nanoTime())));
    file.mkdir();
    gp.compile();
    String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
    assertTrue(s != null && !s.isEmpty());
    assertDoesNotThrow(() -> {
        gp.parse(s);
    });
    assertDoesNotThrow(() -> {
        gp.store(file.getAbsolutePath(), true);
    });
    GenericParser cgp = assertDoesNotThrow(() -> GenericParser.load(file.getAbsolutePath()));
    assertDoesNotThrow(() -> {
        cgp.parse(s);
    });
    DefaultTreeListener dlist = new DefaultTreeListener();
    cgp.setListener(dlist);
    assertDoesNotThrow(() -> {
        cgp.parse(s);
    });
    assertNotNull(dlist.getParseTree());
    LOGGER.debug(dlist.getParseTree().toDot());
}
Also used : File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener 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;
    }
    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);
            }
        };
        assertDoesNotThrow((Executable) processor::process);
        assertEquals(7, parseTree.getNodes().size());
        assertEquals("103", processor.getResult());
    } 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) 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 DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener 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);
    assertDoesNotThrow(gp::compile);
    ParserRuleContext ctx = assertDoesNotThrow(() -> gp.parse(toParse));
    LOGGER.info("ctx {}", ctx.getChild(0).getText());
    ParseTree a = t.getParseTree();
    assertTrue(a.getNodes().size() > 1);
    LOGGER.debug(a.toDot());
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) InputStream(java.io.InputStream) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)28 GenericParser (org.snt.inmemantlr.GenericParser)25 Test (org.junit.jupiter.api.Test)23 ParseTree (org.snt.inmemantlr.tree.ParseTree)18 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)16 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)16 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)15 File (java.io.File)14 FileNotFoundException (java.io.FileNotFoundException)11 InputStream (java.io.InputStream)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 IOException (java.io.IOException)8 RecognitionException (org.antlr.v4.runtime.RecognitionException)8 ToolCustomizer (org.snt.inmemantlr.tool.ToolCustomizer)8 Assertions (org.junit.jupiter.api.Assertions)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 FileUtils (org.snt.inmemantlr.utils.FileUtils)7 java.util (java.util)6 Collectors (java.util.stream.Collectors)6