Search in sources :

Example 16 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestExternalGrammars method verify.

private void verify(GenericParser p, Set<File> ok, Set<File> error, String ep) {
    DefaultTreeListener dt = new DefaultTreeListener();
    verify(p, ok, ep, CaseSensitiveType.NONE, true);
    verify(p, error, ep, CaseSensitiveType.NONE, false);
}
Also used : DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener)

Example 17 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestExternalGrammars method testStringTemplate.

@Test
public void testStringTemplate() {
    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);
    assertDoesNotThrow(() -> {
        File util = new File("src/test/resources/grammars-v4/stringtemplate/" + "src/main/java/org/antlr/parser/st4/LexerAdaptor.java");
        gp.addUtilityJavaFiles(util);
    });
    assertDoesNotThrow(gp::compile);
    LOGGER.debug("name {}", gp.getParserName());
    gp.setParserName("org.antlr.parser.st4.STParser");
    gp.setLexerName("org.antlr.parser.st4.STLexer");
    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) 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) 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 18 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener in project inmemantlr by julianthome.

the class TestExternalGrammars method verify.

private void verify(GenericParser p, Set<File> toParse, String ep, CaseSensitiveType t, boolean shouldParse) {
    DefaultTreeListener dt = new DefaultTreeListener();
    p.setListener(dt);
    boolean parses;
    for (File e : toParse) {
        try {
            LOGGER.info("parseFile {} with {} and ept {}", e.getName(), p.getParserName(), ep);
            ParserRuleContext ctx = (ep != null && !ep.isEmpty()) ? p.parse(e, ep, CaseSensitiveType.NONE) : p.parse(e, t);
            // Assert.Assertions.assertNotNull(ctx);
            if (ctx == null)
                parses = false;
            else
                parses = true;
        } catch (IllegalWorkflowException | FileNotFoundException | RecognitionException | ParsingException e1) {
            LOGGER.error("error: {}", e1.getMessage());
            parses = false;
        }
        assertEquals(shouldParse, parses);
        if (shouldParse) {
            ParseTree parseTree = dt.getParseTree();
            assertNotNull(parseTree);
            LOGGER.debug(parseTree.toDot());
            assertTrue(parseTree.getNodes().size() > 1);
        }
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) FileNotFoundException(java.io.FileNotFoundException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) File(java.io.File) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParseTree(org.snt.inmemantlr.tree.ParseTree)

Example 19 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener 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());
    assertTrue(!files.isEmpty());
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(tc, files.toArray(new File[0])));
    DefaultTreeListener dt = new DefaultTreeListener();
    gp.setListener(dt);
    assertDoesNotThrow(() -> gp.addUtilityJavaFiles(new File("src/test/resources/grammars-v4/antlr4/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java")));
    assertDoesNotThrow(gp::compile);
    s.examples.removeIf(f -> f.getName().contains("three.g4"));
    verify(gp, 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) 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 20 with DefaultTreeListener

use of org.snt.inmemantlr.listener.DefaultTreeListener 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);
    assertDoesNotThrow(gp::compile);
    LOGGER.debug("successfully compiled grammar");
    DefaultTreeListener dt = new DefaultTreeListener();
    gp.setListener(dt);
    verify(gp, s.examples, s.nexamples, s.entrypoint);
}
Also used : DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser)

Aggregations

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