Search in sources :

Example 11 with GenericParser

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

the class TestExternalGrammars method testTSql.

@Test
public void testTSql() {
    if (!toCheck("tsql"))
        return;
    Subject s = subjects.get("tsql");
    Set<File> mfiles = s.g4.stream().filter(v -> v.getName().matches("TSql(Lexer|Parser).g4")).collect(Collectors.toSet());
    assertFalse(mfiles.isEmpty());
    GenericParser mparser = null;
    try {
        mparser = new GenericParser(mfiles.toArray(new File[0]));
    } catch (FileNotFoundException e) {
        fail();
    }
    assertNotNull(mparser);
    DefaultTreeListener mdt = new DefaultTreeListener();
    assertDoesNotThrow(mparser::compile);
    mparser.setStreamProvider(new CasedStreamProvider(CaseSensitiveType.UPPER));
    mparser.setListener(mdt);
    // seems to cause issues when running on windows
    s.examples.removeIf(f -> f.getName().equals("full_width_chars.sql"));
    verify(mparser, 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) CasedStreamProvider(org.snt.inmemantlr.stream.CasedStreamProvider) 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 12 with GenericParser

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

the class TestExternalGrammars method testObjc.

@Test
public void testObjc() {
    if (!toCheck("objc"))
        return;
    Subject s = subjects.get("objc");
    s.g4.add(new File("src/test/resources/grammars-v4/objc/one-step" + "-processing/ObjectiveCLexer.g4"));
    s.g4.add(new File("src/test/resources/grammars-v4/objc/one-step" + "-processing/ObjectiveCPreprocessorParser.g4"));
    GenericParser gp = null;
    try {
        gp = new GenericParser(s.g4.toArray(new File[0]));
    } catch (FileNotFoundException e) {
        fail();
    }
    DefaultTreeListener mdt = new DefaultTreeListener();
    assertDoesNotThrow(gp::compile);
    gp.setListener(mdt);
    s.examples.removeIf(p -> p.getName().contains("AllInOne.m"));
    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 13 with GenericParser

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

the class TestMalformed method testGeneration.

@Test
public void testGeneration() {
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(grammar));
    Assertions.assertNotNull(gp);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    String s = FileUtils.loadFileContent(sfile.getAbsolutePath());
    Assertions.assertTrue(s != null && !s.isEmpty());
    DefaultTreeListener dlist = new DefaultTreeListener();
    gp.setListener(dlist);
    assertThrows(ParsingException.class, () -> gp.parse(s));
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 14 with GenericParser

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

the class TestGenericParser method testParser.

@Test
public void testParser() {
    GenericParser gp = null;
    try {
        gp = new GenericParser(grammar);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Assertions.assertNotNull(gp);
    // Incorrect workflows
    boolean thrown = false;
    try {
        gp.parse(sfile);
    } catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
        thrown = true;
    }
    Assertions.assertTrue(thrown);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    Assertions.assertTrue(compile);
    // Incorrect workflows
    thrown = false;
    try {
        gp.parse(sfile);
    } catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
        thrown = true;
    }
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        if (e instanceof RedundantCompilationException)
            compile = true;
        else
            compile = false;
    }
    Assertions.assertTrue(compile);
    thrown = false;
    try {
        gp.parse(sfile);
    } catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
        thrown = true;
    }
    Assertions.assertFalse(thrown);
    thrown = false;
    Assertions.assertNotSame(null, gp.getListener());
    // Correct workflow
    gp.setListener(new DefaultListener());
    Assertions.assertNotNull(gp.getListener());
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        if (e instanceof RedundantCompilationException)
            compile = true;
        else
            compile = false;
    }
    Assertions.assertTrue(compile);
    try {
        gp.parse(sfile);
    } catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
        thrown = true;
    }
    Assertions.assertFalse(thrown);
}
Also used : DefaultListener(org.snt.inmemantlr.listener.DefaultListener) FileNotFoundException(java.io.FileNotFoundException) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 15 with GenericParser

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

the class TestGenericParser method testCompilationError.

@Test
public void testCompilationError() {
    GenericParser gp = null;
    try {
        gp = new GenericParser(fgrammar);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Assertions.assertNotNull(gp);
    // Incorrect workflows
    boolean thrown = false;
    try {
        gp.parse(sfile);
    } catch (IllegalWorkflowException | FileNotFoundException | ParsingException e) {
        thrown = true;
    }
    Assertions.assertTrue(thrown);
    boolean compile = true;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        if (e instanceof CompilationErrorException) {
            compile = false;
        }
    }
    Assertions.assertFalse(compile);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) 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