[ANN] Citrus

122 views
Skip to first unread message

Michael Jackson

unread,
May 13, 2010, 5:37:14 PM5/13/10
to treet...@googlegroups.com
Hi all,

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

http://rubygems.org/gems/citrus
http://github.com/mjijackson/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://en.wikipedia.org/wiki/Parsing_expression_grammar
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

--
You received this message because you are subscribed to the Google Groups "Treetop Development" group.
To post to this group, send email to treet...@googlegroups.com.
To unsubscribe from this group, send email to treetop-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/treetop-dev?hl=en.

Jason Garber

unread,
May 14, 2010, 2:28:23 AM5/14/10
to treet...@googlegroups.com
Hey, this is cool, Michael! I had started to do the same thing awhile back, but lost steam.

Any idea how fast it is compared to Treetop? What I was working toward was something that could be installed once as a native extension and then run all sorts of grammars, like RedCloth, Cucumber, or SASS very fast. Seems like this could be a step in that direction.

Jason Garber

Michael Jackson

unread,
May 14, 2010, 8:45:52 AM5/14/10
to treet...@googlegroups.com
Hi Jason,

I ran a few little benchmarks this past week, and it seems like Citrus
should be in the same neighborhood as Treetop when it comes to raw
parsing speed. The nice thing about it is that the default mode is to
not memoize the parse results as you go, which can save some time and
memory with some types of grammars. Of course, the option is always
available to turn on memoization if your parse is doing too much
backtracking. But that is the only real attempt at optimization I've
done up to this point.

In the end though, it's a bit difficult to compare Treetop and Citrus
because one is a generator and the other is a combinator. If you have
any idea how to make it faster, I'm all ears.

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



Clifford Heath

unread,
Jul 2, 2010, 3:17:21 AM7/2/10
to treet...@googlegroups.com
On 14/05/2010, at 7:37 AM, Michael Jackson wrote:
> Just released this gem today, and thought others on this list might
> get some good use out of it. It's like Treetop, but takes a completely
> different approach.
>
> http://rubygems.org/gems/citrus
> http://github.com/mjijackson/citrus

Very cool Michael, kudos!

I forked it and made a few patches today:

* ParseException message contains line.column instead of offset
* ParserException contains the list of failed terminal rules tried at
themax offset
* Fixed bad regexps to handle back-slash escapes inside strings and
character classes
(couldn't put a backslashed [ into a char class, or a backslashed
closing quote into a string)
* Fixed a performance problem where every regexp was searching to the
end of the input,
but only being accepted if it matched at the start (I added \A to
the start of the Regexp)

Please feel free to pull all or any. I think I have enough knowledge
of Citrus now to
say I feel more confident extending it than I do with Treetop, and
would consider putting
semantic predicates in and switching over my CQL language
implementation.

Clifford Heath, http://github.com/cjheath

Reply all
Reply to author
Forward
0 new messages