SexpPath: A Ruby DSL for Pattern Matching S-Expressions
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.
July 14, 2009 at 6:06 pm
SexpPath is really sweet! One of the coolest DSLs out there, and definitely worth checking out if you're going to process S-expressions.
July 14, 2009 at 6:16 pm
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.
July 14, 2009 at 6:53 pm
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?
July 14, 2009 at 6:55 pm
Aslak: RubyParser also speak Sexps and runs nicely on 1.9 :-)
July 14, 2009 at 7:58 pm
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.
July 15, 2009 at 8:22 pm
There are a few related libs:
http://github.com/coatl/rubymacros/tree/master
http://github.com/raganwald/rewrite_rails/tree/master
http://allgems.ruby-forum.com/gems?search=macro