Announce: grappa 1.0.0-beta.8; event-driven parsing!

classic Classic list List threaded Threaded
2 messages Options
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Announce: grappa 1.0.0-beta.8; event-driven parsing!

fge
Hello,

A new version of grappa is out and it brings with it a feature which I have wanted for as long as I have known parboiled...

You don't need the stack, Vars or Actions anymore to pull out your parser's productions! Thanks to Guava once again and its EventBus.

You can now do things like these with Grappa:

    public final class StringListener<V>
    {
        private final StringBuilder sb = new StringBuilder();

        @Subscribe
        public void capture(@Untainted @Nonnull final BasicMatchEvent<V> event)
        {
            System.out.println("Called");
            sb.append(event.getMatch());
        }

        public String getContent()
        {
            return sb.toString();
        }
    }

Combined with this parser:

    public class MyParser
        extends EventBusParser<Object>
    {
        MyParser()
        {
            addEvent("normal", BasicMatchEvent.class);
            addEvent("special", BasicMatchEvent.class);
        }

        Rule normal()
        {
            return sequence(oneOrMore(noneOf("\\\"")), fireEvent("normal"));
        }

        Rule special()
        {
            return sequence('\\', ANY, fireEvent("special"));
        }

        Rule content()
        {
            return join(normal()).using(special()).min(0);
        }

        Rule rule()
        {
            return sequence('"', content(), '"', EOI);
        }
    }

register your instance:

    final MyParser parser = Parboiled.createParser(MyParser.class);
    final StringListener<Object> listener = new StringListener<Object>();
    parser.addListener(listener);

And your `StringListener` instance will contain the match!

More uses of EventBus are envisioned in the future; in particular, producing debugging output, whether that be rule tracing, parse tree building etc... Possibilities are vast. And for the latter, there is also TreeTraverser (from Guava, again!) which will be an immense help.

At this stage, I am going to take full advantage of this API to write example parsers to show what you can do ;)

Happy parsing!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Announce: grappa 1.0.0-beta.8; event-driven parsing!

olemartin
Great! Looks really useful. Keep up the good work!
Loading...