Search in sources :

Example 36 with GenericParser

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

the class TestMemObjects method testAntlrObjectAccess.

@Test
public void testAntlrObjectAccess() {
    GenericParser gp = null;
    try {
        gp = new GenericParser(grammar);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    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());
    MemoryTupleSet set = gp.getAllCompiledObjects();
    assertTrue(set != null && set.size() == 4);
    for (MemoryTuple tup : set) {
        LOGGER.debug("tuple name {}", tup.getClassName());
        // for printing the source code
        LOGGER.debug("source {}", tup.getSource().getClassName());
        // for printing the byte code
        for (MemoryByteCode mc : tup.getByteCodeObjects()) {
            Objects.requireNonNull(mc, "MemoryByteCode must not be null");
            LOGGER.debug("bc name: {}", mc.getClassName());
            if (mc.isInnerClass()) {
                assertTrue(mc.getClassName().startsWith(tup.getSource().getClassName()));
            } else {
                assertEquals(tup.getSource().getClassName(), mc.getClassName());
            }
        }
    }
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode) MemoryTupleSet(org.snt.inmemantlr.memobjects.MemoryTupleSet) MemoryTuple(org.snt.inmemantlr.memobjects.MemoryTuple) FileNotFoundException(java.io.FileNotFoundException) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 37 with GenericParser

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

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

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

the class TestExternalGrammars method getParserForSubject.

private GenericParser getParserForSubject(Subject s, ToolCustomizer tc) {
    File[] gs = s.g4.toArray(new File[0]);
    LOGGER.debug("gs {}", gs.length);
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(tc, gs));
    assertNotNull(gp);
    return gp;
}
Also used : File(java.io.File) GenericParser(org.snt.inmemantlr.GenericParser)

Example 40 with GenericParser

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

the class TestExternalGrammars method testSwift2.

@Test
public void testSwift2() {
    if (!toCheck("swift2"))
        return;
    Subject s = subjects.get("swift2");
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(s.g4.toArray(new File[0])));
    assertDoesNotThrow(() -> {
        File util = new File("src/test/resources/grammars-v4/swift2/src/main/java" + "/SwiftSupport.java");
        gp.addUtilityJavaFiles(util);
    });
    assertDoesNotThrow(gp::compile);
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : File(java.io.File) 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