Search in sources :

Example 6 with GenericParser

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

Example 7 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestTreeGeneration method testGeneration.

@Test
public void testGeneration() {
    assertNotNull(grammar);
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
    assertNotNull(gp);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    assertTrue(compile);
    String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
    assertTrue(s != null && !s.isEmpty());
    DefaultTreeListener dlist = new DefaultTreeListener();
    gp.setListener(dlist);
    assertDoesNotThrow(() -> {
        gp.parse(s);
    });
    ParseTree parseTree = dlist.getParseTree();
    LOGGER.debug(parseTree.toDot());
    // create copy
    ParseTree cast = new ParseTree(parseTree);
    assertNotNull(parseTree);
    assertEquals(parseTree.getNodes().size(), cast.getNodes().size());
    Set<ParseTree> parseTrees = parseTree.getSubtrees(n -> "expression".equals(n.getRule()));
    assertEquals(5, parseTrees.size());
    for (ParseTree a : parseTrees) {
        assertTrue(parseTree.hasSubtree(a));
        assertNotNull(parseTree.getSubtree(a));
    }
    int sizeBefore = parseTree.getNodes().size();
    ParseTree first = parseTrees.iterator().next();
    parseTree.removeSubtree(first);
    assertEquals(parseTree.getNodes().size() + first.getNodes().size(), sizeBefore);
    ParseTree repl = new ParseTree("replacement", "replacement");
    cast.replaceSubtree(first, repl);
    assertEquals(cast.getNodes().size(), parseTree.getNodes().size() + 1);
    assertEquals(1, cast.getDominatingSubtrees(n -> "classBody".equals(n.getRule())).size());
    assertTrue(cast.toDot() != null && !cast.toDot().isEmpty());
    ParseTreeNode root = cast.getRoot();
    assertTrue(root.hasChildren());
    assertFalse(root.hasParent());
    for (ParseTreeNode n : cast.getNodes()) {
        assertNotNull(n.getLabel());
        assertNotNull(n.getRule());
        for (int i = 0; i < n.getChildren().size(); i++) {
            if (i == 0)
                assertEquals(n.getChild(i), n.getFirstChild());
            if (i == n.getChildren().size() - 1)
                assertEquals(n.getChild(i), n.getLastChild());
        }
    }
    for (ParseTreeNode c : cast.getLeafs()) {
        assertTrue(c.hasParent());
        assertFalse(c.hasChildren());
        assertTrue(c.isLeaf());
        assertNull(c.getLastChild());
        assertNull(c.getFirstChild());
    }
    ParseTreeNode croot = cast.getRoot();
    croot.setParent(parseTree.getRoot());
    assertEquals(croot.getParent(), parseTree.getRoot());
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) ParseTreeNode(org.snt.inmemantlr.tree.ParseTreeNode) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 8 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestBroken method testBroken.

@Test
public void testBroken() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Broken.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.assertFalse(compile);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 9 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestExternalGrammars method testCSharp.

@Test
public void testCSharp() {
    // being -- csharp grammar is runtime dependent
    if (System.getProperty("os.name").toLowerCase().startsWith("win"))
        return;
    if (!toCheck("csharp"))
        return;
    Subject s = subjects.get("csharp");
    Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("CSharp" + "(Lexer|PreprocessorParser|Parser).g4")).collect(Collectors.toSet());
    assertFalse(mfiles.isEmpty());
    GenericParser mparser = null;
    try {
        mparser = new GenericParser(mfiles.toArray(new File[0]));
    } catch (FileNotFoundException e) {
        fail();
    }
    assertNotNull(mparser);
    DefaultTreeListener mdt = new DefaultTreeListener();
    assertDoesNotThrow(mparser::compile);
    mparser.setListener(mdt);
    mparser.setParserName("CSharpParser");
    verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
Also used : java.util(java.util) CaseSensitiveType(org.snt.inmemantlr.GenericParser.CaseSensitiveType) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) 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) 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 10 with GenericParser

use of org.snt.inmemantlr.GenericParser in project inmemantlr by julianthome.

the class TestExternalGrammars method testMySQL.

@Test
public void testMySQL() {
    if (!toCheck("mysql"))
        return;
    Subject s = subjects.get("mysql");
    Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("MySql" + "(Lexer|Parser).g4")).collect(Collectors.toSet());
    assertFalse(mfiles.isEmpty());
    GenericParser mparser = null;
    try {
        mparser = new GenericParser(mfiles.toArray(new File[0]));
    } catch (FileNotFoundException e) {
        fail();
    }
    assertNotNull(mparser);
    DefaultTreeListener mdt = new DefaultTreeListener();
    assertDoesNotThrow(mparser::compile);
    mparser.setStreamProvider(new CasedStreamProvider(CaseSensitiveType.UPPER));
    mparser.setListener(mdt);
    verify(mparser, s.examples, s.nexamples, s.entrypoint);
}
Also used : java.util(java.util) CaseSensitiveType(org.snt.inmemantlr.GenericParser.CaseSensitiveType) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) 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) 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)

Aggregations

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