Search in sources :

Example 16 with CompilationException

use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.

the class TestDoubleParse method testProcessor.

@Test
public void testProcessor() {
    GenericParser gp1 = new GenericParser(sgrammarcontent);
    boolean compile;
    try {
        gp1.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    GenericParser gp2 = new GenericParser(sgrammarcontent);
    try {
        gp2.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    DefaultTreeListener l1 = new DefaultTreeListener();
    DefaultTreeListener l2 = new DefaultTreeListener();
    Assertions.assertFalse(l1.toString() == null);
    Assertions.assertEquals(l1.toString(), "");
    gp1.setListener(l1);
    gp2.setListener(l2);
    try {
        gp1.parse(s1);
        gp2.parse(s2);
    } catch (IllegalWorkflowException | ParsingException e) {
        e.printStackTrace();
    }
    ParseTree out1 = l1.getParseTree();
    ParseTree out2 = l2.getParseTree();
    Assertions.assertFalse(out1.equals(out2));
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) 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 17 with CompilationException

use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.

the class TestExternalGrammars method testSubject.

private void testSubject(Subject s, boolean skip) {
    LOGGER.debug("test {}", s.name);
    if (specialCases.contains(s.name) && skip) {
        LOGGER.debug("skip {}", s.name);
        return;
    }
    LOGGER.info("test {}", s.name);
    GenericParser gp = getParserForSubject(s, null);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        LOGGER.debug("Error for {}:{}", s.name, e.getMessage());
        compile = false;
    }
    Assertions.assertTrue(compile);
    LOGGER.debug("successfully compiled grammar");
    DefaultTreeListener dt = new DefaultTreeListener();
    gp.setListener(dt);
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser)

Example 18 with CompilationException

use of org.snt.inmemantlr.exceptions.CompilationException 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 19 with CompilationException

use of org.snt.inmemantlr.exceptions.CompilationException 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[s.g4.size()]));
    } catch (FileNotFoundException e) {
        Assertions.assertTrue(false);
    }
    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) {
        Assertions.assertFalse(true);
    }
    Assertions.assertNotNull(gp);
    DefaultTreeListener mdt = new DefaultTreeListener();
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    gp.setListener(mdt);
    Assertions.assertTrue(compile);
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) 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 20 with CompilationException

use of org.snt.inmemantlr.exceptions.CompilationException in project inmemantlr by julianthome.

the class TestExternalGrammars method testHTML.

@Test
public void testHTML() {
    if (!toCheck("html"))
        return;
    Subject s = subjects.get("html");
    GenericParser gp = getParserForSubject(s, null);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    s.examples.removeIf(p -> !p.getName().contains("antlr"));
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Aggregations

GenericParser (org.snt.inmemantlr.GenericParser)31 CompilationException (org.snt.inmemantlr.exceptions.CompilationException)31 Test (org.junit.jupiter.api.Test)29 DefaultTreeListener (org.snt.inmemantlr.listener.DefaultTreeListener)23 IllegalWorkflowException (org.snt.inmemantlr.exceptions.IllegalWorkflowException)19 ParsingException (org.snt.inmemantlr.exceptions.ParsingException)19 FileNotFoundException (java.io.FileNotFoundException)17 File (java.io.File)16 ParseTree (org.snt.inmemantlr.tree.ParseTree)16 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 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 ToolCustomizer (org.snt.inmemantlr.tool.ToolCustomizer)7 FileUtils (org.snt.inmemantlr.utils.FileUtils)7 java.util (java.util)6 Collectors (java.util.stream.Collectors)6