Search in sources :

Example 1 with ToolCustomizer

use of org.snt.inmemantlr.tool.ToolCustomizer 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 2 with ToolCustomizer

use of org.snt.inmemantlr.tool.ToolCustomizer in project inmemantlr by julianthome.

the class TestExternalGrammars method testStringTemplate.

@Test
public void testStringTemplate() {
    // skip for the time being -- grammar issue
    if (true)
        return;
    if (!toCheck("stringtemplate"))
        return;
    Subject s = subjects.get("stringtemplate");
    // LOGGER.info("G4 {}", s.g4);
    // Exam
    ToolCustomizer tc = t -> t.genPackage = "org.antlr.parser.st4";
    GenericParser gp = getParserForSubject(s, tc);
    DefaultTreeListener dt = new DefaultTreeListener();
    gp.setListener(dt);
    try {
        File util = new File("src/test/resources/grammars-v4/stringtemplate/" + "src/main/java/org/antlr/parser/st4/LexerAdaptor.java");
        gp.addUtilityJavaFiles(util);
    } catch (FileNotFoundException e) {
        Assertions.assertFalse(true);
    }
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    LOGGER.debug("name {}", gp.getParserName());
    gp.setParserName("org.antlr.parser.st4.STParser");
    gp.setLexerName("org.antlr.parser.st4.STLexer");
    Assertions.assertTrue(compile);
    s.examples = s.examples.stream().filter(f -> f.getName().contains("example1.st")).collect(Collectors.toSet());
    verify(gp, 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) FileNotFoundException(java.io.FileNotFoundException) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 3 with ToolCustomizer

use of org.snt.inmemantlr.tool.ToolCustomizer in project inmemantlr by julianthome.

the class TestExternalGrammars method testAntlr4.

@Test
public void testAntlr4() {
    if (!toCheck("antlr4"))
        return;
    Subject s = subjects.get("antlr4");
    // Exam
    ToolCustomizer tc = t -> t.genPackage = "org.antlr.parser.antlr4";
    Set<File> files = s.g4.stream().filter(v -> v.getName().matches("" + "(ANTLRv4" + "(Lexer|Parser)|LexBasic).g4")).collect(Collectors.toSet());
    Assertions.assertTrue(files.size() > 0);
    GenericParser gp = null;
    try {
        gp = new GenericParser(tc, files.toArray(new File[files.size()]));
    } catch (FileNotFoundException e) {
        Assertions.assertTrue(false);
    }
    DefaultTreeListener dt = new DefaultTreeListener();
    gp.setListener(dt);
    try {
        File util = new File("src/test/resources/grammars-v4/antlr4/src/main/java" + "/org" + "/antlr/parser/antlr4/LexerAdaptor.java");
        gp.addUtilityJavaFiles(util);
    } catch (FileNotFoundException e) {
        Assertions.assertFalse(true);
    }
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    s.examples.removeIf(f -> f.getName().contains("three.g4"));
    Assertions.assertTrue(compile);
    verify(gp, 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) FileNotFoundException(java.io.FileNotFoundException) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 GenericParser (org.snt.inmemantlr.GenericParser)3 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)3 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)3 ToolCustomizer (org.snt.inmemantlr.tool.ToolCustomizer)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 FilenameUtils (org.apache.commons.io.FilenameUtils)2 Assertions (org.junit.jupiter.api.Assertions)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)2 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)2