Search in sources :

Example 1 with RouteParser

use of loghub.RouteParser in project LogHub by fbacchella.

the class TestParser method testType.

@Test
public void testType() throws IOException {
    CharStream cs = CharStreams.fromStream(getClass().getClassLoader().getResourceAsStream("types.conf"), CharsetUtil.UTF_8);
    RouteLexer lexer = new RouteLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    RouteParser parser = new RouteParser(tokens);
    parser.removeErrorListeners();
    ConfigErrorListener errListener = new ConfigErrorListener();
    parser.addErrorListener(errListener);
    // begin parsing at init rule
    loghub.RouteParser.ConfigurationContext tree = parser.configuration();
    ConfigListener conf = new ConfigListener();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(conf, tree);
    Assert.assertEquals("stack not empty :" + conf.stack, 0, conf.stack.size());
    ConfigListener.PipenodesList main = conf.pipelines.get("main");
    ObjectDescription p = (ObjectDescription) main.processors.get(0);
    Assert.assertTrue(((ObjectWrapped) p.beans.get("string")).wrapped instanceof String);
    Assert.assertTrue(((ObjectWrapped) p.beans.get("boolean")).wrapped instanceof Boolean);
    Assert.assertTrue(((ObjectWrapped) p.beans.get("int")).wrapped instanceof Integer);
    Assert.assertTrue(((ObjectWrapped) p.beans.get("double")).wrapped instanceof Double);
    Assert.assertArrayEquals(new Object[] { "0", 1, 1.0, true }, (Object[]) ((ObjectWrapped) p.beans.get("array")).wrapped);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RouteLexer(loghub.RouteLexer) RouteParser(loghub.RouteParser) ObjectWrapped(loghub.configuration.ConfigListener.ObjectWrapped) CharStream(org.antlr.v4.runtime.CharStream) ObjectDescription(loghub.configuration.ConfigListener.ObjectDescription) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Test(org.junit.Test)

Example 2 with RouteParser

use of loghub.RouteParser in project LogHub by fbacchella.

the class TestParser method test2.

@Test
public void test2() throws IOException, InterruptedException {
    CharStream cs = CharStreams.fromStream(getClass().getClassLoader().getResourceAsStream("test.conf"), CharsetUtil.UTF_8);
    RouteLexer lexer = new RouteLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    RouteParser parser = new RouteParser(tokens);
    parser.removeErrorListeners();
    ConfigErrorListener errListener = new ConfigErrorListener();
    parser.addErrorListener(errListener);
    // begin parsing at init rule
    loghub.RouteParser.ConfigurationContext tree = parser.configuration();
    ConfigListener conf = new ConfigListener();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(conf, tree);
    Assert.assertEquals("stack not empty :" + conf.stack, 0, conf.stack.size());
    for (String s : new String[] { "oneref", "main", "groovy" }) {
        Assert.assertTrue("pipeline " + s + " not found", conf.pipelines.containsKey(s));
    }
    Assert.assertEquals("too much pipelines", 6, conf.pipelines.size());
    Assert.assertEquals("too much inputs", 1, conf.inputs.size());
    Assert.assertEquals("too much outputs", 1, conf.outputs.size());
    for (String s : new String[] { "logfile", "plugins" }) {
        Assert.assertTrue("property " + s + " not found", conf.properties.containsKey(s));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RouteLexer(loghub.RouteLexer) RouteParser(loghub.RouteParser) CharStream(org.antlr.v4.runtime.CharStream) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Test(org.junit.Test)

Example 3 with RouteParser

use of loghub.RouteParser in project LogHub by fbacchella.

the class Configuration method getTree.

private RouteParser.ConfigurationContext getTree(CharStream cs, ConfigListener conflistener) throws ConfigException {
    // Passing the input to the lexer to create tokens
    RouteLexer lexer = new RouteLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Passing the tokens to the parser to create the parse tree.
    RouteParser parser = new RouteParser(tokens);
    conflistener.parser = parser;
    conflistener.stream = cs;
    parser.removeErrorListeners();
    ConfigErrorListener errListener = new ConfigErrorListener();
    parser.addErrorListener(errListener);
    RouteParser.ConfigurationContext tree = parser.configuration();
    return tree;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RouteLexer(loghub.RouteLexer) RouteParser(loghub.RouteParser)

Aggregations

RouteLexer (loghub.RouteLexer)3 RouteParser (loghub.RouteParser)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 CharStream (org.antlr.v4.runtime.CharStream)2 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)2 Test (org.junit.Test)2 ObjectDescription (loghub.configuration.ConfigListener.ObjectDescription)1 ObjectWrapped (loghub.configuration.ConfigListener.ObjectWrapped)1