Sha256: 4a3d5f1779e9dbb0dcf4ffcb282c8aa0792bae773da0f472d0e8f057149d1122

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

= Sexpistol

Sexpistol is a simple library for parsing S-Expressions in Ruby. Sexpistol takes an S-Expression and turns it into a native Ruby data structure made up of nested sets of arrays.

=== Example

  (define test (lambda () (
    (print "blah")
    (print 1)
    (print 9.01)
    (print (+ 10 12 13))
  )))
  
would be parsed by Sexpistol like so:

  [:define, :test, [:lambda, [], [
    [:print, "blah"],
    [:print, 1],
    [:print, 9.01],
    [:print, [:"+", 10, 12, 13]]
  ]]]
  
=== Type mappings

Sexpistol supports all of the standard datatypes and converts them directly to their Ruby equivalents:

* Lists (a b c)
* Integers (1 2 3)
* Floats (1.0 2.0 42.9)
* Strings ("\t\"Hello world!\"\n")
* Symbols (symbol Symbol __symbol__ symbo_l symbol? symbol!)

Sexpistol also supports mapping the Ruby keyword literals (nil, true, false) to their native Ruby types, although this is disabled by default for compatibility. To enable it use `@parser.ruby_keyword_literals = true`, eg:
  
  @parser = Sexpistol.new
  @parser.parse_string("nil false true")
  #=> [:nil, :false, :true]
  
  @parser.ruby_keyword_literals = true
  @parser.parse_string("nil false true")
  #=> [nil, false, true]
  
=== Installation

For convenience Sexpistol is packaged as a RubyGem, to install it simply enter the following at your command line:

  gem install sexpistol
  
=== Usage

  # Create a new parser instance
  @parser = Sexpistol.new
  
  # Parse a string
  ast = @parser.parse_string("(string (to (parse)))")
  #=> [:string, [:to, [:parse]]]
  
  # Change the representation
  ast[1][0] = :is
  ast[1][1][0] = :parsed
  #=> [:string, [:is, [:parsed]]]
  
  # Turn the array structure back into an S-Expression
  @parser.to_sexp( ast )
  #=> "( string ( is ( parsed ) ) )"
  
=== Author & Credits

Author:: {Aaron Gough}[mailto:aaron@aarongough.com]

Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sexpistol-0.0.1 README.rdoc