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.

Leave a Reply

Your email address will not be published. Required fields are marked *