All Projects → dryruby → sxp.rb

dryruby / sxp.rb

Licence: Unlicense license
A universal S-expression parser for Ruby.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to sxp.rb

Active bootstrap skin
Bootstrap skin for Active Admin 🚀 🚀 🚀
Stars: ✭ 133 (+171.43%)
Mutual labels:  rubygems
bitcache
[Retired] Distributed, content-addressable storage system.
Stars: ✭ 30 (-38.78%)
Mutual labels:  rubygems
vital
Design Framework
Stars: ✭ 53 (+8.16%)
Mutual labels:  rubygems
Gem Compiler
A RubyGems plugin that generates binary gems
Stars: ✭ 136 (+177.55%)
Mutual labels:  rubygems
Vueonrails
💎 Rails gem with the power of Vue.js components
Stars: ✭ 250 (+410.2%)
Mutual labels:  rubygems
sane patch
Making monkey patches sane again
Stars: ✭ 63 (+28.57%)
Mutual labels:  rubygems
Sepa king
Ruby gem for creating SEPA XML files
Stars: ✭ 125 (+155.1%)
Mutual labels:  rubygems
fcmpush
Firebase Cloud Messaging API wrapper for Ruby, suppot HTTP v1 API including access_token auto refresh feature.
Stars: ✭ 44 (-10.2%)
Mutual labels:  rubygems
yavdb
Yet Another Vulnerability Database
Stars: ✭ 14 (-71.43%)
Mutual labels:  rubygems
waxseal
Big official brass stamp to make signing gems dead simple.
Stars: ✭ 21 (-57.14%)
Mutual labels:  rubygems
Instagram Crawler
Crawl instagram photos, posts and videos for download.
Stars: ✭ 178 (+263.27%)
Mutual labels:  rubygems
Rubygems
Library packaging and distribution for Ruby.
Stars: ✭ 2,902 (+5822.45%)
Mutual labels:  rubygems
agency-jekyll-theme
Jekyll version of the newest Agency Bootstrap theme, plus new features: Google Analytics, Markdown support, custom pages, and more!
Stars: ✭ 222 (+353.06%)
Mutual labels:  rubygems
Waterdrop
WaterDrop is a standalone Karafka component library for generating Kafka messages
Stars: ✭ 136 (+177.55%)
Mutual labels:  rubygems
microformats-ruby
Ruby gem that parse HTML containing microformats/microformats2 and returns Ruby objects, a Ruby hash or a JSON hash
Stars: ✭ 89 (+81.63%)
Mutual labels:  rubygems
Guides
An effort to provide awesome documentation for the RubyGems ecosystem.
Stars: ✭ 128 (+161.22%)
Mutual labels:  rubygems
php.rb
[Retired] PHP.rb translates Ruby code into PHP code.
Stars: ✭ 86 (+75.51%)
Mutual labels:  rubygems
tss-rb
A Ruby implementation of Threshold Secret Sharing (Shamir) as defined in IETF Internet-Draft draft-mcgrew-tss-03.txt
Stars: ✭ 22 (-55.1%)
Mutual labels:  rubygems
The-Ruby-Workshop
A New, Interactive Approach to Learning Ruby
Stars: ✭ 26 (-46.94%)
Mutual labels:  rubygems
mercadopago
Gem to communicate with the MercadoPago API
Stars: ✭ 31 (-36.73%)
Mutual labels:  rubygems

SXP.rb: S-Expressions for Ruby

This is a Ruby implementation of a universal S-expression parser.

Gem Version Build Status Coverage Status

Features

  • Parses S-expressions in universal, Scheme, Common Lisp, or SPARQL syntax.
  • Adds a #to_sxp method to Ruby objects.
  • Compatible with Ruby >= 2.6, Rubinius >= 3.0, and JRuby 9+.

Basic syntax

S-Expressions derive from LISP, and include some basic datatypes common to all variants:

Pairs
Of the form (2 . 3)
Lists
Of the form (1 (2 3))
Symbols
Of the form with-hyphen ?@!$ a\ symbol\ with\ spaces
Strings
Of the form "Hello, world!"
Strings may include the following special characters:
  • \b — Backspace
  • \f — Form Feed
  • \n — New Line
  • \r — Carriage Return
  • \t — Horizontal Tab
  • \uxxxx — 2-byte Unicode character escape
  • \Uxxxxxxxx — 4-byte Unicode character escape
  • \" — Double-quote character
  • \\ — Backspace
Additionally, any other character may follow \, representing the character itself.
Characters
Of the form ...
Integers
Of the form -9876543210
Floating-point numbers
Of the form -0.0 6.28318 6.022e23
Rationals
Of the form 1/3

Additionally, variation-specific formats support additional datatypes:

Scheme

In addition to the standard datatypes, the Scheme dialect supports the following:

Lists
In addition to ( ... ), a square bracket pair may be used for reading lists of the form [ ... ].
Comments
A comment starts with ; and continues to the end of the line.
Sharp character sequences
Such as #t, #n, and #xXXX.
  • #n — Null
  • #f — False
  • #t — True
  • #bBBB — Binary number
  • #oOOO — Octal number
  • #dDDD — Decimal number
  • #xXXX — Hexadecimal number
  • #\C — A single Unicode character
  • #\space — A space character
  • #\newline — A newline character
  • #; — Skipped character
  • #! — Skipped to end of line

Common Lisp

In addition to the standard datatypes, the Common Lisp dialect supports the following:

Comments
A comment starts with ; and continues to the end of the line.
Symbols
In addition to base symbols, any character sequence delimited by | is treated as a symbol.
Sharp character sequences
Such as #t, #n, and #xXXX.
  • #bBBB — Binary number
  • #oOOO — Octal number
  • #xXXX — Hexadecimal number
  • #C — A single Unicode character
  • #\newline — A newline character
  • #\space — A space character
  • #\backspace — A backspace character
  • #\tab — A tab character
  • #\linefeed — A linefeed character
  • #\page — A page feed character
  • #\return — A carriage return character
  • #\rubout — A rubout character
  • #'function — A function definition

SPARQL/RDF

In addition to the standard datatypes, the SPARQL dialect supports the following:

Lists
In addition to ( ... ), a square bracket pair may be used for reading lists of the form [ ... ].
Comments
A comment starts with # or ; and continues to the end of the line.
Literals
Strings are interpreted as an RDF Literal with datatype xsd:string. It can be followed by @lang to create a language-tagged string, or ^^IRI to create a datatyped-literal. Examples:
  • "a plain literal"
  • "a literal with a language"@en
  • "a typed literal"^^<http://example/>
  • "a typed literal with a PNAME"^^xsd:string
IRIs
An IRI is formed as in SPARQL, either enclosed by <...>, or having the form of a PNAME. If a base iri is defined in a containing base expression, IRIs using the <...> are resolved against that base iri. If the PNAME form is used, the prefix must be defined in a containing prefix expression. Examples:
  • <http://example/foo>
  • (base <http://example.com> <foo>)
  • (prefix ((foo: <http://example.com/>)) foo:bar)
  • a # synonym for rdf:type
Blank Nodes
An blank node is formed as in SPARQL. Examples:
  • _:
  • _:id
Variables
A SPARQL variable is defined using either ? or $ prefixes, as in SPARQL. Examples:
  • ?var
  • $var
Numbers and booleans
As with SPARQL. Examples:
  • true, false
  • 123, -18
  • 123.0, 456.
  • 1.0e0, 1.0E+6

Examples

require 'sxp'

Parsing basic S-expressions

SXP.read "(* 6 7)"  #=> [:*, 6, 7]

SXP.read <<-EOF
  (define (fact n)
    (if (= n 0)
        1
        (* n (fact (- n 1)))))
EOF

#=> [:define, [:fact, :n],
      [:if, [:"=", :n, 0],
            1,
            [:*, :n, [:fact, [:-, :n, 1]]]]]

Parsing Scheme S-expressions

SXP::Reader::Scheme.read %q((and #t #f))             #=> [:and, true, false]

Parsing Common Lisp S-expressions

SXP::Reader::CommonLisp.read %q((or t nil))          #=> [:or, true, nil]

Parsing SPARQL S-expressions

require 'rdf'

SXP::Reader::SPARQL.read %q((base <https://ar.to/>))  #=> [:base, RDF::URI('https://ar.to/')]

Writing an SXP with formatting

SXP::Generator.print([:and, true, false])   #=> (and #t #f)

Documentation

  • Full documentation available on RubyDoc

  • {SXP}

Parsing SXP

  • {SXP::Reader}
    • {SXP::Reader::Basic}
      • {SXP::Reader::CommonLisp}
      • {SXP::Reader::Extended}
        • {SXP::Reader::Scheme}
        • {SXP::Reader::SPARQL}

Manipulating SXP

  • {SXP::Pair}
    • {SXP::List}

Generating SXP

  • {SXP::Generator}

Dependencies

  • Ruby (>= 2.6)
  • RDF.rb (~> 3.2), only needed for SPARQL S-expressions

Installation

The recommended installation method is via RubyGems. To install the latest official release of the SXP.rb gem, do:

% [sudo] gem install sxp

Download

To get a local working copy of the development repository, do:

% git clone git://github.com/dryruby/sxp.rb.git

Alternatively, you can download the latest development version as a tarball as follows:

% wget https:/github.com/dryruby/sxp.rb/tarball/master

Resources

Authors

Contributors

Contributing

This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.

  • Do your best to adhere to the existing coding conventions and idioms.
  • Don't use hard tabs, and don't leave trailing whitespace on any line.
  • Do document every method you add using YARD annotations. Read the tutorial or just look at the existing code for examples.
  • Don't touch the .gemspec, VERSION or AUTHORS files. If you need to change them, do so on your private branch only.
  • Do feel free to add yourself to the CREDITS file and the corresponding list in the the README. Alphabetical order applies.
  • Do note that in order for us to merge any non-trivial changes (as a rule of thumb, additions larger than about 15 lines of code), we need an explicit public domain dedication on record from you, which you will be asked to agree to on the first commit to a repo within the organization.

License

SXP.rb is free and unencumbered public domain software. For more information, see https://unlicense.org/ or the accompanying UNLICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].