Sorcerer — Recovering the Source
Sorcerer will generate Ruby code from a Ripper-like abstract syntax tree (i.e. S-Expressions).
Sorcerer is targetted mainly at small snippets of Ruby code, expressable in a single line. Longer examples may be re-sourced, but they will be rendered in a single line format.
Limitations
Sorcerer is only testing on Ruby 1.9.
Technically, Sorcerer should work on Ruby 1.8, but since Ripper is 1.9 I’ve only tried it on that platform.
Links
Documents:: http://github.com/jimweirich/sorcerer Git Clone:: git://github.com/jimweirich/sorcerer.git Issue Tracking:: http://www.pivotaltracker.com/projects/56858 Bug Reporting:: http://onestepback.org/cgi-bin/bugs.cgi?project=sorcerer
Examples
sexp = [:binary,
[:var_ref, [:@ident, "a", [1, 0]]],
:+,
[:var_ref, [:@ident, "b", [1, 4]]]]
puts Sorcerer.source(sexp)
will generate
a + b
Ripper may be used to produce the s-expressions used by Sorcerer. The following will produce the same output.
sexp = Ripper::SexpBuilder.new("a + b").parse
puts Sorcerer.source(sexp)
Options
Multi-Line Output
If you want multi-line output of source, add the multiline option to the source command.
For example, given:
sexp = Ripper::SexpBuilder.new("def foo; end").parse
Then the following
puts Sorcerer.source(sexp)
generates single line output (the default):
def foo; end
And the following
puts Sorcerer.source(sexp, multiline: true)
generates multi-line output
def foo
end
Debugging Output
If you wish to see the S-Expressions processed by Sorcerer and the output emitted, then use the debug option:
puts Sorcerer.source(sexp, debug: true)
History
- 0.0.7 – Basic single line version
- 0.1.0 – Added support for multi-line output. Improved rendering of a number of constructs