Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

SexpPath: A Ruby DSL for Pattern Matching S-Expressions

By Ari Brown / July 14, 2009

With people occasionally talking about "Code vs. Data", it only makes sense that you should be able process over code as you would a string. Sexp Path is a code processing tool that allows you to search over and process Ruby code in the form of S-Expressions.

For those who don't know, an S-Expression (or simply, a "sexp") is an iterable way of representing code or data. Using Ryan Davis' Parse Tree, you can parse Ruby files and process over them using Sexp Path. It's a bit like like XPath or regular expressions for your code.

The foundation of Sexp Path is the query, formed with Q?{ ... }, which is applied to the sexp via the / method. These methods can be chained, and the results processed via the each method. Using this as an example, Sexp Path also supports named captures like Q?{ s(:class, atom % 'class_name', _, _) } in line 16 so that the second atom is accessible via the class_name attribute in line 25.

The code is stored on GitHub. Unclear of where the project is headed, Adam Sanderson, the creator, encourages forking and feedback.


  1. Magnus Holn says:

    SexpPath is really sweet! One of the coolest DSLs out there, and definitely worth checking out if you're going to process S-expressions.

  2. Stephen Stillwater says:

    Does anyone have a good link explaining what this is most useful for?

    I've heard a lot of about Sexps from Lisp folk, but I haven't had a moment of grokking yet.

  3. Aslak Helles√ły says:

    ParseTree will probably never work on Ruby 1.9 (http://blog.zenspider.com/2009/04/parsetree-eol.html) so I find it a little puzzling that new projects based on ParseTree keep popping up.

    Another sign that some people don't want to move to 1.9?

  4. Magnus Holn says:

    Aslak: RubyParser also speak Sexps and runs nicely on 1.9 :-)

  5. Adam Sanderson says:

    Stephan: I have no idea what it's good for yet, but it's still pretty neat. I intend on using it to scan ruby code and using it for some interesting meta programming.

    Aslak: Magnus is right, you can use RubyParser on 1.9, the dependency here is on SexpProessor, though I do use ParseTree for a few examples.

  6. roger says:

    There are a few related libs:


Other Posts to Enjoy

Twitter Mentions