Search in sources :

Example 26 with GenericParser

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

the class TestExternalGrammars method testZ.

@Test
public void testZ() {
    if (!toCheck("z"))
        return;
    Subject s = subjects.get("z");
    // Assertions.assertTrue(mfiles.size() > 0);
    GenericParser gp = null;
    try {
        gp = new GenericParser(s.g4.toArray(new File[0]));
    } catch (FileNotFoundException e) {
        fail();
    }
    try {
        File util1 = new File("src/test/resources/grammars-v4/z/src/main/java" + "/ZOperatorListener.java");
        File util2 = new File("src/test/resources/grammars-v4/z/src/main/java" + "/ZSupport.java");
        gp.addUtilityJavaFiles(util1, util2);
    } catch (FileNotFoundException e) {
        fail();
    }
    assertNotNull(gp);
    DefaultTreeListener mdt = new DefaultTreeListener();
    assertDoesNotThrow(gp::compile);
    gp.setListener(mdt);
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : 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 27 with GenericParser

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

the class TestHello method testInterpreter.

@Test
public void testInterpreter() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Hello.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);
}
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 28 with GenericParser

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

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

Example 30 with GenericParser

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

the class TestArtifactSerialization method testArtifactSerialization.

@Test
public void testArtifactSerialization() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/Simple.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    ToolCustomizer tc = 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;
    }
    Assertions.assertTrue(compile);
    gp.writeAntlrAritfactsTo("/tmp/test/out");
    Assertions.assertTrue(Files.exists(Paths.get("/tmp/test/out")));
}
Also used : Logger(org.slf4j.Logger) Files(java.nio.file.Files) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) InputStream(java.io.InputStream) CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) 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