Skip to end of metadata
Go to start of metadata

With Groovy's nice closure support, we can avoid most anonymous class and hence get a much less cluttered parser code. In the Tutorial section, we described how a calculator can be built using Java API. We will now present a groovy version that is much shorter.

  • org.codehaus.parsec.groovy.ParserCategory class provides many useful convenience methods that make parser construction handy.
  • operator "|" is overloaded for the parser "plus" operation, where the second alternative parser is tried when the first fails.
  • [a parser list].first() is equivalent to Parsers.sum(new Parser[]{a parser list}).
  • SomeInterface.impl{some closure here} is a convenience function that adapts a closure to any target interface.
  • groovy allows the use of local mutable variable in closures, that's why we don't need an array here to get around the "final" requirement in Java.
  • everything else is similar to the Java version, just simpler.

The source code of the ParserCategory class can be downloaded here, binary here

  • No labels