[ANN] Citrus ~ Parsing Expressions for Ruby

Hi all,

Just released this gem today, and thought others might get some good
use out of it. It's like Treetop, but takes a completely different
approach.

http://rubygems.org/gems/citrus

The README is below. Enjoy.

···

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

~* Citrus *~

Parsing Expressions for Ruby

Citrus is a compact and powerful parsing library for Ruby that combines the
elegance and expressiveness of the language with the simplicity and power of
parsing expression grammars.

Citrus grammars look very much like Treetop grammars but take a completely
different approach. Instead of generating parsers from your grammars, Citrus
evaluates grammars and rules in memory as Ruby modules. In fact, you can even
define your grammars as Ruby modules in the first place, entirely skipping the
parsing/evaluation step.

Terminals are represented as either strings or regular expressions. Support for
sequences, choices, labels, repetition, and lookahead (both positive and
negative) are all included, as well as character classes and the dot-matches-
anything symbol.

To try it out, fire up an IRB session from the root of the project and run one
of the examples.

    $ irb -Ilib
    > require 'citrus'
     => true
    > Citrus.load 'examples/calc'
     => [Calc]
    > match = Calc.parse '1 + 5'
     => #<Citrus::Match ...
    > match.value
     => 6

Be sure to try requiring `citrus/debug' (instead of just `citrus') if you'd like
some better visualization of the match results.

The code base is very small and it's well-documented and tested, so it should be
fairly easy to understand for anyone who is familiar with parsing expressions.

  ** Links **

http://pdos.csail.mit.edu/~baford/packrat/


http://treetop.rubyforge.org/index.html

  ** Installation **

Via RubyGems:

    $ sudo gem install citrus

From a local copy:

    $ git clone git://github.com/mjijackson/citrus.git
    $ cd citrus
    $ rake package && sudo rake install

  ** License **

Copyright 2010 Michael Jackson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

--
Michael Jackson
http://mjijackson.com
@mjijackson

are you the real Michael Jackson?

···

2010/5/13 Michael Jackson <mjijackson@gmail.com>

Hi all,

Just released this gem today, and thought others might get some good
use out of it. It's like Treetop, but takes a completely different
approach.

citrus | RubyGems.org | your community gem host
GitHub - mjackson/citrus: Parsing Expressions for Ruby

The README is below. Enjoy.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

~* Citrus *~

Parsing Expressions for Ruby

Citrus is a compact and powerful parsing library for Ruby that combines the
elegance and expressiveness of the language with the simplicity and power
of
parsing expression grammars.

Citrus grammars look very much like Treetop grammars but take a completely
different approach. Instead of generating parsers from your grammars,
Citrus
evaluates grammars and rules in memory as Ruby modules. In fact, you can
even
define your grammars as Ruby modules in the first place, entirely skipping
the
parsing/evaluation step.

Terminals are represented as either strings or regular expressions. Support
for
sequences, choices, labels, repetition, and lookahead (both positive and
negative) are all included, as well as character classes and the
dot-matches-
anything symbol.

To try it out, fire up an IRB session from the root of the project and run
one
of the examples.

   $ irb -Ilib
   > require 'citrus'
    => true
   > Citrus.load 'examples/calc'
    => [Calc]
   > match = Calc.parse '1 + 5'
    => #<Citrus::Match ...
   > match.value
    => 6

Be sure to try requiring `citrus/debug' (instead of just `citrus') if you'd
like
some better visualization of the match results.

The code base is very small and it's well-documented and tested, so it
should be
fairly easy to understand for anyone who is familiar with parsing
expressions.

** Links **

Page Redirector
Parsing expression grammar - Wikipedia
http://treetop.rubyforge.org/index.html

** Installation **

Via RubyGems:

   $ sudo gem install citrus

From a local copy:

   $ git clone git://github.com/mjijackson/citrus.git
   $ cd citrus
   $ rake package && sudo rake install

** License **

Copyright 2010 Michael Jackson

Permission is hereby granted, free of charge, to any person obtaining a
copy
of this software and associated documentation files (the "Software"), to
deal
in the Software without restriction, including without limitation the
rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

--
Michael Jackson
http://mjijackson.com
@mjijackson

Hi Michael,

Out of curiosity: What are you using citrus for? What is the typical/intended usage?

greetings,
kaspar

Yes. I'm real. :wink:

···

--
Michael Jackson

@mjijackson

On Thu, May 13, 2010 at 3:58 PM, Louis-Philippe <default@spiralix.org> wrote:

are you the real Michael Jackson?

2010/5/13 Michael Jackson <mjijackson@gmail.com>

Hi all,

Just released this gem today, and thought others might get some good
use out of it. It's like Treetop, but takes a completely different
approach.

citrus | RubyGems.org | your community gem host
GitHub - mjackson/citrus: Parsing Expressions for Ruby

The README is below. Enjoy.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

~* Citrus *~

Parsing Expressions for Ruby

Citrus is a compact and powerful parsing library for Ruby that combines the
elegance and expressiveness of the language with the simplicity and power
of
parsing expression grammars.

Citrus grammars look very much like Treetop grammars but take a completely
different approach. Instead of generating parsers from your grammars,
Citrus
evaluates grammars and rules in memory as Ruby modules. In fact, you can
even
define your grammars as Ruby modules in the first place, entirely skipping
the
parsing/evaluation step.

Terminals are represented as either strings or regular expressions. Support
for
sequences, choices, labels, repetition, and lookahead (both positive and
negative) are all included, as well as character classes and the
dot-matches-
anything symbol.

To try it out, fire up an IRB session from the root of the project and run
one
of the examples.

$ irb -Ilib
> require 'citrus'
=> true
> Citrus.load 'examples/calc'
=> [Calc]
> match = Calc.parse '1 + 5'
=> #<Citrus::Match ...
> match.value
=> 6

Be sure to try requiring `citrus/debug' (instead of just `citrus') if you'd
like
some better visualization of the match results.

The code base is very small and it's well-documented and tested, so it
should be
fairly easy to understand for anyone who is familiar with parsing
expressions.

** Links **

Page Redirector
Parsing expression grammar - Wikipedia
http://treetop.rubyforge.org/index.html

** Installation **

Via RubyGems:

$ sudo gem install citrus

From a local copy:

$ git clone git://github.com/mjijackson/citrus.git
$ cd citrus
$ rake package && sudo rake install

** License **

Copyright 2010 Michael Jackson

Permission is hereby granted, free of charge, to any person obtaining a
copy
of this software and associated documentation files (the "Software"), to
deal
in the Software without restriction, including without limitation the
rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

--
Michael Jackson
http://mjijackson.com
@mjijackson

At this point Citrus is a parser combinator. It is able to take
language grammars and create parsers from them. These parsers are just
Ruby modules. You can combine these grammars together to create more
complex grammars. You can also add semantic information to these
grammars, so that when you encounter certain tokens in the input
string you can easily interpret their meaning.

So, you could say that Citrus is a general-purpose parsing library. It
is to be used whenever you would like to build a parser for a
non-trivial set of syntactic rules.

A simple example is included with the code in the examples directory.
It's called Calc. It's a grammar for arithmetic that is able to parse
and evaluate mathematical expressions while respecting operator
precedence and ignoring whitespace. This would be impossible to do
with a regular expression, simply because mathematical expressions may
be recursive. Even if mathematical expressions were not recursive,
they would still be very difficult to parse properly using a regular
expression, not to mention interpret!

You could use Citrus to parse any well-defined document. For example,
you could easily build a parser for something you find in an RFC. You
could even create your own mini computer language if you were
ambitious enough.

Currently I'm just developing the project for fun. I'm interested in
computer languages and syntax, and I wanted to use Ruby to create a
syntactical analysis tool. Turns out that Ruby was an excellent
choice, for so many reasons.

···

--
Michael Jackson

@mjijackson

On Mon, May 17, 2010 at 1:34 AM, Kaspar Schiess <eule@space.ch> wrote:

Hi Michael,

Out of curiosity: What are you using citrus for? What is the
typical/intended usage?

greetings,
kaspar