Home

Awesome

Sorcerer -- Recovering the Source

Master
Master Build Status

Sorcerer will generate Ruby code from a Ripper-like abstract syntax tree (i.e. S-Expressions).

Sorcerer is targeted 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.

Version: 2.0.1

Limitations

Sorcerer only supports Ruby 2.0 & up.

Links

DescriptionLink
Documentshttp://github.com/rspec-given/sorcerer
Git Clonegit://github.com/rspec-given/sorcerer.git
Issues / Bug Trackinghttps://github.com/rspec-given/sorcerer/issues
Continuous Integrationhttp://travis-ci.org/#!/rspec-given/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

No Options

By default, sorcerer will output its source in single line mode.

For example, given:

  sexp = Ripper::SexpBuilder.new("def foo; bar; end").parse

Then the following

  puts Sorcerer.source(sexp)

generates single line output (the default):

def foo; bar; end

Multi-Line Output

If you want multi-line output of source, add the multiline option to the source command.

For example, given the sexp generated above, then this

  puts Sorcerer.source(sexp, multiline: true)

generates multi-line output

def foo
bar
end

(Note that all multi-line output will have a final newline.)

Indentation

By default, sorcerer does not indent its multiline output. Adding the "indent" option will cause the output to be indented.

For example, given the sexp generated above, then the following

  puts Sorcerer.source(sexp, indent: true)

generates indented output:

def foo
  bar
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)

License

Sorcerer is available under the terms of the MIT license. See the MIT-LICENSE file for details.

History