Search in sources :

Example 31 with GenericParser

use of org.snt.inmemantlr.GenericParser 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;
    }
    assertTrue(compile);
    GenericParser gp2 = new GenericParser(sgrammarcontent);
    try {
        gp2.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    assertTrue(compile);
    DefaultTreeListener l1 = new DefaultTreeListener();
    DefaultTreeListener l2 = new DefaultTreeListener();
    assertNotNull(l1.toString());
    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();
    assertNotEquals(out1, 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 32 with GenericParser

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

the class TestGenericParser method testWrongParams1Exception.

@Test
public void testWrongParams1Exception() {
    boolean thrown = false;
    GenericParser genericParser0 = null;
    try {
        genericParser0 = new GenericParser((File) null);
        fail("Expecting exception: NullPointerException");
    } catch (NullPointerException | FileNotFoundException e) {
        thrown = true;
    }
    Assertions.assertTrue(thrown);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 33 with GenericParser

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

the class TestGenericParser method testWrongParams2Exception.

@Test
public void testWrongParams2Exception() {
    boolean thrown = false;
    GenericParser genericParser0 = null;
    try {
        genericParser0 = new GenericParser("xyz");
        fail("Expecting exception: Error");
    } catch (NullPointerException | Error e) {
        thrown = true;
    }
    Assertions.assertTrue(thrown);
}
Also used : GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 34 with GenericParser

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

the class TestGrammarImport method testOncrpc.

@Test
public void testOncrpc() {
    File[] fls = { new File("src/test/resources/inmemantlr/xdr.g4"), new File("src/test/resources/inmemantlr/oncrpcv2.g4") };
    GenericParser gp = assertDoesNotThrow(() -> new GenericParser(fls));
    assertDoesNotThrow(gp::compile);
    assertDoesNotThrow(() -> {
        gp.parse(new File("src/test/resources/inmemantlr/CalculatorService.x"), "oncrpcv2Specification", CaseSensitiveType.NONE);
    });
}
Also used : File(java.io.File) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Example 35 with GenericParser

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

the class TestLexerGrammar method testInterpreter.

@Test
public void testInterpreter() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/LexerGrammar.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    GenericParser gp = new GenericParser(sgrammarcontent);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
    }
    assertTrue(compile);
    List<Token> tokens = assertDoesNotThrow(() -> gp.lex("a09"));
    assertEquals(2, tokens.size());
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) InputStream(java.io.InputStream) Token(org.antlr.v4.runtime.Token) 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