sxp 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b421d3fcd687a0e3e946087339e58f4a323be02bc3a1e66c024785324019b14a
4
- data.tar.gz: be19832d64b95ab0db9e0a23b0d41ea48b9e6d2bc2405cb3186cd9c9307c4d39
3
+ metadata.gz: 8ac25f66cc3d44ef1ea0ee37141ce092c9740ada5d58e000472043ed0e53dec7
4
+ data.tar.gz: 3969e76131d5e12a512a182ba9f03de3f1dcc098b60cd067089f0d75443117ce
5
5
  SHA512:
6
- metadata.gz: 7937ebb8b36f9cd16bc9ec74c2d385a3a5d6c0f62e0f6a6b350199ee4a1f532780a63335a03c38ad79207fcdead281d03fc55d07c341b9d8acc8fed78a3254db
7
- data.tar.gz: fa13fc009baf866729d7fda6050949095218634125b873ecc3121d878a19f32fd563a010f8d628c0e0197fcd4b26913c9b0d44088e9dbad0f411c120aa702d47
6
+ metadata.gz: da0445f22b06f2ed5cb31a44dd37bc4c49a078205335f420857b1105b30f1d95296254e4d7f0ce80c5d4779862f0792c45e409758c893eb0131eced633df243c
7
+ data.tar.gz: f8018ae5ffb2740cf12e0ca28f67c4f3b92dd466b0a2a2db73ed896f6fdb801a4a3148daf3c575474890db74d278ad2cecd114336fd9f85e021555456ea7393b
data/AUTHORS CHANGED
@@ -1,2 +1,2 @@
1
1
  * Arto Bendiken <arto@bendiken.net>
2
- * Gregg Kellogg <gregg@kellogg-assoc.com>
2
+ * Gregg Kellogg <gregg@greggkellogg.net>
data/README.md CHANGED
@@ -2,15 +2,166 @@
2
2
 
3
3
  This is a Ruby implementation of a universal [S-expression][] parser.
4
4
 
5
- [![Gem Version](https://badge.fury.io/rb/sxp.png)](http://badge.fury.io/rb/sxp)
6
- [![Build Status](https://travis-ci.org/dryruby/sxp.rb.png?branch=master)](http://travis-ci.org/dryruby/sxp.rb)
5
+ [![Gem Version](https://badge.fury.io/rb/sxp.png)](https:/badge.fury.io/rb/sxp)
6
+ [![Build Status](https://github.com/dryruby/sxp.rb/workflows/CI/badge.svg?branch=develop)](https://github.com/dryruby/sxp.rb/actions?query=workflow%3ACI)
7
+ [![Coverage Status](https://coveralls.io/repos/dryruby/sxp.rb/badge.svg?branch=develop)](https://coveralls.io/r/dryruby/sxp.rb?branch=develop)
7
8
 
8
9
  ## Features
9
10
 
10
11
  * Parses S-expressions in universal, [Scheme][], [Common Lisp][], or
11
12
  [SPARQL][] syntax.
12
13
  * Adds a `#to_sxp` method to Ruby objects.
13
- * Compatible with Ruby >= 2.4, Rubinius >= 3.0, and JRuby 9+.
14
+ * Compatible with Ruby >= 2.6, Rubinius >= 3.0, and JRuby 9+.
15
+
16
+ ## Basic syntax
17
+
18
+ S-Expressions derive from LISP, and include some basic datatypes common to all variants:
19
+
20
+ <dl>
21
+ <dt>Pairs</dt>
22
+ <dd>Of the form <code>(2 . 3)</code></dd>
23
+ <dt>Lists</dt>
24
+ <dd>Of the form <code>(1 (2 3))</code></dd>
25
+ <dt>Symbols</dt>
26
+ <dd>Of the form <code>with-hyphen ?@!$ a\ symbol\ with\ spaces</code></dd>
27
+ <dt>Strings</dt>
28
+ <dd>Of the form <code>"Hello, world!"</code><br/>
29
+ Strings may include the following special characters:
30
+ <ul>
31
+ <li><code>\b</code> &mdash; Backspace</li>
32
+ <li><code>\f</code> &mdash; Form Feed</li>
33
+ <li><code>\n</code> &mdash; New Line</li>
34
+ <li><code>\r</code> &mdash; Carriage Return</li>
35
+ <li><code>\t</code> &mdash; Horizontal Tab</li>
36
+ <li><code>\u<i>xxxx</i></code> &mdash; 2-byte Unicode character escape</li>
37
+ <li><code>\U<i>xxxxxxxx</i></code> &mdash; 4-byte Unicode character escape</li>
38
+ <li><code>\"</code> &mdash; Double-quote character</li>
39
+ <li><code>\\</code> &mdash; Backspace</li>
40
+ </ul>
41
+ Additionally, any other character may follow <code>\</code>, representing the character itself.
42
+ </dd>
43
+ <dt>Characters</dt>
44
+ <dd>Of the form <code>...</code></dd>
45
+ <dt>Integers</dt>
46
+ <dd>Of the form <code>-9876543210</code></dd>
47
+ <dt>Floating-point numbers</dt>
48
+ <dd>Of the form <code>-0.0 6.28318 6.022e23</code></dd>
49
+ <dt>Rationals</dt>
50
+ <dd>Of the form <code>1/3</code></dd>
51
+ </dl>
52
+
53
+ Additionally, variation-specific formats support additional datatypes:
54
+
55
+ ### Scheme
56
+
57
+ In addition to the standard datatypes, the Scheme dialect supports the following:
58
+
59
+ <dl>
60
+ <dt>Lists</dt>
61
+ <dd>In addition to <code>( ... )</code>, a square bracket pair may be used for reading lists of the form <code>[ ... ]</code>.
62
+ </dd>
63
+ <dt>Comments</dt>
64
+ <dd>A comment starts with <code>;</code> and continues to the end of the line.
65
+ <dt>Sharp character sequences</dt>
66
+ <dd>Such as <code>#t</code>, <code>#n</code>, and <code>#xXXX</code>.<br>
67
+ <ul>
68
+ <li><code>#n</code> &mdash; Null</li>
69
+ <li><code>#f</code> &mdash; False</li>
70
+ <li><code>#t</code> &mdash; True</li>
71
+ <li><code>#b<i>BBB</i></code> &mdash; Binary number</li>
72
+ <li><code>#o<i>OOO</i></code> &mdash; Octal number</li>
73
+ <li><code>#d<i>DDD</i></code> &mdash; Decimal number</li>
74
+ <li><code>#x<i>XXX</i></code> &mdash; Hexadecimal number</li>
75
+ <li><code>#\<i>C</i></code> &mdash; A single Unicode character</li>
76
+ <li><code>#\space</code> &mdash; A space character</li>
77
+ <li><code>#\newline</code> &mdash; A newline character</li>
78
+ <li><code>#;</code> &mdash; Skipped character</li>
79
+ <li><code>#!</code> &mdash; Skipped to end of line</li>
80
+ </ul>
81
+ </dd>
82
+ </dl>
83
+
84
+ ### Common Lisp
85
+
86
+ In addition to the standard datatypes, the Common Lisp dialect supports the following:
87
+
88
+ <dl>
89
+ <dt>Comments</dt>
90
+ <dd>A comment starts with <code>;</code> and continues to the end of the line.
91
+ <dt>Symbols</dt>
92
+ <dd>In addition to base symbols, any character sequence delimited by <code>|</code> is treated as a symbol.</dd>
93
+ <dt>Sharp character sequences</dt>
94
+ <dd>Such as <code>#t</code>, <code>#n</code>, and <code>#xXXX</code>.<br>
95
+ <ul>
96
+ <li><code>#b<i>BBB</i></code> &mdash; Binary number</li>
97
+ <li><code>#o<i>OOO</i></code> &mdash; Octal number</li>
98
+ <li><code>#x<i>XXX</i></code> &mdash; Hexadecimal number</li>
99
+ <li><code>#C</code> &mdash; A single Unicode character</li>
100
+ <li><code>#\newline</code> &mdash; A newline character</li>
101
+ <li><code>#\space</code> &mdash; A space character</li>
102
+ <li><code>#\backspace</code> &mdash; A backspace character</li>
103
+ <li><code>#\tab</code> &mdash; A tab character</li>
104
+ <li><code>#\linefeed</code> &mdash; A linefeed character</li>
105
+ <li><code>#\page</code> &mdash; A page feed character</li>
106
+ <li><code>#\return</code> &mdash; A carriage return character</li>
107
+ <li><code>#\rubout</code> &mdash; A rubout character</li>
108
+ <li><code>#'<i>function</i></code> &mdash; A function definition</li>
109
+ </ul>
110
+ </dd>
111
+ </dl>
112
+
113
+ ### SPARQL/RDF
114
+
115
+ In addition to the standard datatypes, the SPARQL dialect supports the following:
116
+
117
+ <dl>
118
+ <dt>Lists</dt>
119
+ <dd>In addition to <code>( ... )</code>, a square bracket pair may be used for reading lists of the form <code>[ ... ]</code>.
120
+ </dd>
121
+ <dt>Comments</dt>
122
+ <dd>A comment starts with <code>#</code> or <code>;</code> and continues to the end of the line.
123
+ <dt>Literals</dt>
124
+ <dd>Strings are interpreted as an RDF Literal with datatype <code>xsd:string</code>. It can be followed by <code>@<i>lang</i></code> to create a language-tagged string, or <code>^^<i>IRI</i></code> to create a datatyped-literal. Examples:
125
+ <ul>
126
+ <li><code>"a plain literal"</code></li>
127
+ <li><code>"a literal with a language"@en</code></li>
128
+ <li><code>"a typed literal"^^&lt;http://example/></code></li>
129
+ <li><code>"a typed literal with a PNAME"^^xsd:string</code></li>
130
+ </ul>
131
+ </dd>
132
+ <dt>IRIs</dt>
133
+ <dd>An IRI is formed as in SPARQL, either enclosed by <code>&lt;...></code>, or having the form of a <code>PNAME</code>. If a <var>base iri</var> is defined in a containing <var>base</var> expression, IRIs using the <code>&lt;...></code> are resolved against that base iri. If the <code>PNAME</code> form is used, the prefix must be defined in a containing <var>prefix</var> expression. Examples:
134
+ <ul>
135
+ <li><code>&lt;http://example/foo></code></li>
136
+ <li><code>(base &lthttp://example.com> &lt;foo>)</code></li>
137
+ <li><code>(prefix ((foo: &lt;http://example.com/>)) foo:bar)</code></li>
138
+ <li><code>a</code> # synonym for rdf:type</li>
139
+ </ul>
140
+ </dd>
141
+ <dt>Blank Nodes</dt>
142
+ <dd>An blank node is formed as in SPARQL. Examples:
143
+ <ul>
144
+ <li><code>_:</code></li>
145
+ <li><code>_:id</code></li>
146
+ </ul>
147
+ </dd>
148
+ <dt>Variables</dt>
149
+ <dd>A SPARQL variable is defined using either <code>?</code> or <code>$</code> prefixes, as in SPARQL. Examples:
150
+ <ul>
151
+ <li><code>?var</code></li>
152
+ <li><code>$var</code></li>
153
+ </ul>
154
+ </dd>
155
+ <dt>Numbers and booleans</dt>
156
+ <dd>As with SPARQL. Examples:
157
+ <ul>
158
+ <li>true, false</li>
159
+ <li>123, -18</li>
160
+ <li>123.0, 456.</li>
161
+ <li>1.0e0, 1.0E+6</li>
162
+ </ul>
163
+ </dd>
164
+ </dl>
14
165
 
15
166
  ## Examples
16
167
 
@@ -44,7 +195,7 @@ This is a Ruby implementation of a universal [S-expression][] parser.
44
195
 
45
196
  require 'rdf'
46
197
 
47
- SXP::Reader::SPARQL.read %q((base <http://ar.to/>)) #=> [:base, RDF::URI('http://ar.to/')]
198
+ SXP::Reader::SPARQL.read %q((base <https://ar.to/>)) #=> [:base, RDF::URI('https://ar.to/')]
48
199
 
49
200
  ### Writing an SXP with formatting
50
201
 
@@ -52,7 +203,7 @@ This is a Ruby implementation of a universal [S-expression][] parser.
52
203
 
53
204
  ## Documentation
54
205
 
55
- * Full documentation available on [RubyDoc](http://rubydoc.info/gems/sxp/file/README.md)
206
+ * Full documentation available on [RubyDoc](https:/rubydoc.info/gems/sxp/file/README.md)
56
207
 
57
208
  * {SXP}
58
209
 
@@ -71,23 +222,20 @@ This is a Ruby implementation of a universal [S-expression][] parser.
71
222
  ### Generating SXP
72
223
  * {SXP::Generator}
73
224
 
74
- Dependencies
75
- ------------
225
+ # Dependencies
76
226
 
77
- * [Ruby](http://ruby-lang.org/) (>= 2.4)
78
- * [RDF.rb](http://rubygems.org/gems/rdf) (~> 3.1), only needed for SPARQL
227
+ * [Ruby](https:/ruby-lang.org/) (>= 2.6)
228
+ * [RDF.rb](https:/rubygems.org/gems/rdf) (~> 3.2), only needed for SPARQL
79
229
  S-expressions
80
230
 
81
- Installation
82
- ------------
231
+ # Installation
83
232
 
84
- The recommended installation method is via [RubyGems](http://rubygems.org/).
233
+ The recommended installation method is via [RubyGems](https:/rubygems.org/).
85
234
  To install the latest official release of the SXP.rb gem, do:
86
235
 
87
236
  % [sudo] gem install sxp
88
237
 
89
- Download
90
- --------
238
+ ## Download
91
239
 
92
240
  To get a local working copy of the development repository, do:
93
241
 
@@ -96,33 +244,48 @@ To get a local working copy of the development repository, do:
96
244
  Alternatively, you can download the latest development version as a tarball
97
245
  as follows:
98
246
 
99
- % wget http://github.com/dryruby/sxp.rb/tarball/master
247
+ % wget https:/github.com/dryruby/sxp.rb/tarball/master
100
248
 
101
- Resources
102
- ---------
249
+ ## Resources
103
250
 
104
- * <http://rubydoc.info/gems/sxp.rb>
105
- * <http://github.com/dryruby/sxp.rb>
106
- * <http://rubygems.org/gems/sxp.rb>
251
+ * <https://rubydoc.info/gems/sxp.rb>
252
+ * <https://github.com/dryruby/sxp.rb>
253
+ * <https://rubygems.org/gems/sxp.rb>
107
254
 
108
- Authors
109
- -------
255
+ ## Authors
110
256
 
111
- * [Arto Bendiken](https://github.com/bendiken) - <http://ar.to/>
112
- * [Gregg Kellogg](http://github.com/gkellogg) - <http://greggkellogg.net/>
257
+ * [Arto Bendiken](https://github.com/artob) - <https://ar.to/>
258
+ * [Gregg Kellogg](https://github.com/gkellogg) - <https://greggkellogg.net/>
113
259
 
114
- Contributors
115
- ------------
260
+ ## Contributors
116
261
 
117
- * [Ben Lavender](https://github.com/bhuga) - <http://bhuga.net/>
262
+ * [Ben Lavender](https://github.com/bhuga) - <https://bhuga.net/>
118
263
 
119
- License
120
- -------
264
+ ## Contributing
265
+ This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange development and release activity. All submissions _must_ be on a feature branch based on the _develop_ branch to ease staging and integration.
266
+
267
+ * Do your best to adhere to the existing coding conventions and idioms.
268
+ * Don't use hard tabs, and don't leave trailing whitespace on any line.
269
+ * Do document every method you add using [YARD][] annotations. Read the
270
+ [tutorial][YARD-GS] or just look at the existing code for examples.
271
+ * Don't touch the `.gemspec`, `VERSION` or `AUTHORS` files. If you need to
272
+ change them, do so on your private branch only.
273
+ * Do feel free to add yourself to the `CREDITS` file and the corresponding
274
+ list in the the `README`. Alphabetical order applies.
275
+ * Do note that in order for us to merge any non-trivial changes (as a rule
276
+ of thumb, additions larger than about 15 lines of code), we need an
277
+ explicit [public domain dedication][PDD] on record from you,
278
+ which you will be asked to agree to on the first commit to a repo within the organization.
279
+
280
+ ## License
121
281
 
122
282
  SXP.rb is free and unencumbered public domain software. For more
123
- information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
283
+ information, see <https://unlicense.org/> or the accompanying UNLICENSE file.
124
284
 
125
- [S-expression]: http://en.wikipedia.org/wiki/S-expression
126
- [Scheme]: http://scheme.info/
127
- [Common Lisp]: http://en.wikipedia.org/wiki/Common_Lisp
285
+ [S-expression]: https://en.wikipedia.org/wiki/S-expression
286
+ [Scheme]: https://scheme.info/
287
+ [Common Lisp]: https://en.wikipedia.org/wiki/Common_Lisp
128
288
  [SPARQL]: https://jena.apache.org/documentation/notes/sse.html
289
+ [YARD]: https://yardoc.org/
290
+ [YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
291
+ [PDD]: https://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
data/UNLICENSE CHANGED
@@ -21,4 +21,4 @@ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
21
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
22
  OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
- For more information, please refer to <http://unlicense.org/>
24
+ For more information, please refer to <https:/unlicense.org/>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
data/bin/sxp2json CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env ruby -rubygems
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
1
+ #!/usr/bin/env ruby
2
+ $::unshift(File.expand_path("../../lib", __FILE__))
3
3
  require 'sxp'
4
4
 
5
5
  abort "Usage: #{File.basename($0)} input.sxp > output.json" if ARGV.empty?
data/bin/sxp2rdf CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env ruby -rubygems
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
1
+ #!/usr/bin/env ruby
2
+ $::unshift(File.expand_path("../../lib", __FILE__))
3
3
  require 'sxp'
4
4
 
5
5
  abort "Usage: #{File.basename($0)} input.sxp > output.rdf" if ARGV.empty?
data/bin/sxp2xml CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env ruby -rubygems
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
1
+ #!/usr/bin/env ruby
2
+ $::unshift(File.expand_path("../../lib", __FILE__))
3
3
  require 'sxp'
4
4
 
5
5
  abort "Usage: #{File.basename($0)} input.sxp > output.xml" if ARGV.empty?
data/bin/sxp2yaml CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env ruby -rubygems
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
1
+ #!/usr/bin/env ruby
2
+ $::unshift(File.expand_path("../../lib", __FILE__))
3
3
  require 'sxp'
4
4
 
5
5
  abort "Usage: #{File.basename($0)} input.sxp > output.yaml" if ARGV.empty?
@@ -1,6 +1,79 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'bigdecimal'
3
+ require 'matrix'
4
+ require 'time'
5
+
6
+ ##
7
+ # Extensions for Ruby's `Object` class.
8
+ class Object
9
+ ##
10
+ # Returns the SXP representation of this object.
11
+ #
12
+ # @return [String]
13
+ def to_sxp(**options)
14
+ to_s.to_json
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Extensions for Ruby's `NilClass` class.
20
+ class NilClass
21
+ ##
22
+ # Returns the SXP representation of this object.
23
+ #
24
+ # @return [String]
25
+ def to_sxp(**options)
26
+ '#n'
27
+ end
28
+ end
29
+
30
+ ##
31
+ # Extensions for Ruby's `FalseClass` class.
32
+ class FalseClass
33
+ ##
34
+ # Returns the SXP representation of this object.
35
+ #
36
+ # @return [String]
37
+ def to_sxp(**options)
38
+ '#f'
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Extensions for Ruby's `TrueClass` class.
44
+ class TrueClass
45
+ ##
46
+ # Returns the SXP representation of this object.
47
+ #
48
+ # @return [String]
49
+ def to_sxp(**options)
50
+ '#t'
51
+ end
52
+ end
53
+
54
+ ##
55
+ # Extensions for Ruby's `String` class.
56
+ class String
57
+ ##
58
+ # Returns the SXP representation of this object.
59
+ #
60
+ # @return [String]
61
+ def to_sxp(**options)
62
+ inspect
63
+ end
64
+ end
65
+
1
66
  ##
2
67
  # Extensions for Ruby's `Symbol` class.
3
68
  class Symbol
69
+ ##
70
+ # Returns the SXP representation of this object.
71
+ #
72
+ # @return [String]
73
+ def to_sxp(**options)
74
+ to_s
75
+ end
76
+
4
77
  ##
5
78
  # Returns `true` if this is a keyword symbol.
6
79
  #
@@ -10,17 +83,244 @@ class Symbol
10
83
  end
11
84
  end
12
85
 
13
- # Update RDF::URI if RDF is loaded
86
+ ##
87
+ # Extensions for Ruby's `Integer` class.
88
+ class Integer
89
+ ##
90
+ # Returns the SXP representation of this object.
91
+ #
92
+ # @return [String]
93
+ def to_sxp(**options)
94
+ to_s
95
+ end
96
+ end
97
+
98
+ ##
99
+ # Extensions for Ruby's `BigDecimal` class.
100
+ class BigDecimal
101
+ ##
102
+ # Returns the SXP representation of this object.
103
+ #
104
+ # @return [String]
105
+ def to_sxp(**options)
106
+ to_f.to_s
107
+ end
108
+ end
109
+
110
+ ##
111
+ # Extensions for Ruby's `Float` class.
112
+ class Float
113
+ ##
114
+ # Returns the SXP representation of this object.
115
+ #
116
+ # @return [String]
117
+ def to_sxp(**options)
118
+ case
119
+ when nan? then 'nan.0'
120
+ when infinite? then (infinite? > 0 ? '+inf.0' : '-inf.0')
121
+ else to_s
122
+ end
123
+ end
124
+ end
125
+
126
+ ##
127
+ # Extensions for Ruby's `Array` class.
128
+ class Array
129
+ ##
130
+ # Returns the SXP representation of this object.
131
+ #
132
+ # @return [String]
133
+ def to_sxp()
134
+ '(' << map { |x| x.to_sxp(**options) }.join(' ') << ')'
135
+ end
136
+ end
137
+
138
+ ##
139
+ # Extensions for Ruby's `Vector` class.
140
+ class Vector
141
+ ##
142
+ # Returns the SXP representation of this object.
143
+ #
144
+ # @return [String]
145
+ def to_sxp(**options)
146
+ '#(' << to_a.map { |x| x.to_sxp(**options) }.join(' ') << ')'
147
+ end
148
+ end
149
+
150
+ ##
151
+ # Extensions for Ruby's `Hash` class.
152
+ class Hash
153
+ ##
154
+ # Returns the SXP representation of this object.
155
+ #
156
+ # @return [String]
157
+ def to_sxp(**options)
158
+ to_a.to_sxp(**options)
159
+ end
160
+ end
161
+
162
+ ##
163
+ # Extensions for Ruby's `Time` class.
164
+ class Time
165
+ ##
166
+ # Returns the SXP representation of this object.
167
+ #
168
+ # @return [String]
169
+ def to_sxp(**options)
170
+ '#@' << (respond_to?(:xmlschema) ? xmlschema : to_i).to_s
171
+ end
172
+ end
173
+
174
+ ##
175
+ # Extensions for Ruby's `Regexp` class.
176
+ class Regexp
177
+ ##
178
+ # Returns the SXP representation of this object.
179
+ #
180
+ # @return [String]
181
+ def to_sxp(**options)
182
+ '#' << inspect
183
+ end
184
+ end
185
+
14
186
  begin
15
- require 'rdf'
187
+ require 'rdf' # For SPARQL/RDF
16
188
 
17
189
  ##
18
- # Extensions for RDF::URI
190
+ # Extensions for Ruby's `Array` class.
191
+ # These extensions depend on RDF being loaded
192
+ class Array
193
+ ##
194
+ # Returns the SXP representation of this object.
195
+ #
196
+ # If array is of the form `[:base, uri, ..]`, the base_uri is taken from the second value
197
+ #
198
+ # If array is of the form `[:prefix, [..], ..]`, prefixes are taken from the second value
199
+ #
200
+ # Prefixes always are terminated by a ':'
201
+ #
202
+ # @param [Hash{Symbol => RDF::URI}] prefixes(nil)
203
+ # @param [RDF::URI] base_uri(nil)
204
+ # @return [String]
205
+ def to_sxp(prefixes: nil, base_uri: nil, **options)
206
+ if self.first == :base && self.length == 3 && self[1].is_a?(RDF::URI)
207
+ base_uri = self[1]
208
+ '(' << (
209
+ self[0,2].map(&:to_sxp) <<
210
+ self.last.to_sxp(prefixes: prefixes, base_uri: base_uri, **options)
211
+ ).join(' ') << ')'
212
+ elsif self.first == :prefix && self.length == 3 && self[1].is_a?(Array)
213
+ prefixes = prefixes ? prefixes.dup : {}
214
+ self[1].each do |defn|
215
+ prefixes[defn.first.to_s.chomp(':').to_sym] = RDF::URI(defn.last) if
216
+ defn.is_a?(Array) && defn.length == 2
217
+ end
218
+ pfx_sxp = self[1].map {|(p,s)|["#{p.to_s.chomp(':')}:".to_sym, RDF::URI(s)]}.to_sxp
219
+ '(' << [
220
+ :prefix,
221
+ pfx_sxp,
222
+ self.last.to_sxp(prefixes: prefixes, base_uri: base_uri, **options)
223
+ ].join(' ') << ')'
224
+ else
225
+ '(' << map { |x| x.to_sxp(prefixes: prefixes, base_uri: base_uri, **options) }.join(' ') << ')'
226
+ end
227
+ end
228
+ end
229
+
19
230
  class RDF::URI
231
+ ##
232
+ # Returns the SXP representation of this a URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
233
+ #
234
+ # @param [Hash{Symbol => RDF::URI}] prefixes(nil)
235
+ # @param [RDF::URI] base_uri(nil)
236
+ # @return [String]
237
+ def to_sxp(prefixes: nil, base_uri: nil, **options)
238
+ return lexical if lexical
239
+ pn = pname(prefixes: prefixes || {})
240
+ return pn unless to_s == pn
241
+ md = self == base_uri ? '' : self.relativize(base_uri)
242
+ "<#{md}>"
243
+ end
244
+
20
245
  # Original lexical value of this URI to allow for round-trip serialization.
21
246
  def lexical=(value); @lexical = value; end
22
247
  def lexical; @lexical; end
23
248
  end
249
+
250
+ class RDF::Node
251
+ ##
252
+ # Returns the SXP representation of this object.
253
+ #
254
+ # @return [String]
255
+ def to_sxp(**options)
256
+ to_s
257
+ end
258
+ end
259
+
260
+ class RDF::Literal
261
+ ##
262
+ # Returns the SXP representation of a Literal.
263
+ #
264
+ # @return [String]
265
+ def to_sxp(**options)
266
+ case datatype
267
+ when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double, RDF::XSD.decimal, RDF::XSD.time
268
+ # Retain stated lexical form if possible
269
+ valid? ? to_s : object.to_sxp(**options)
270
+ else
271
+ text = value.dump
272
+ text << "@#{language}" if self.has_language?
273
+ text << "^^#{datatype.to_sxp(**options)}" if self.has_datatype?
274
+ text
275
+ end
276
+ end
277
+
278
+ class Double
279
+ ##
280
+ # Returns the SXP representation of this object.
281
+ #
282
+ # @return [String]
283
+ def to_sxp(**options)
284
+ case
285
+ when nan? then 'nan.0'
286
+ when infinite? then (infinite? > 0 ? '+inf.0' : '-inf.0')
287
+ else canonicalize.to_s.downcase
288
+ end
289
+ end
290
+ end
291
+ end
292
+
293
+ class RDF::Query
294
+ # Transform Query into an Array form of an SXP
295
+ #
296
+ # If Query is named, it's treated as a GroupGraphPattern, otherwise, a BGP
297
+ #
298
+ # @return [Array]
299
+ def to_sxp(**options)
300
+ res = [:bgp] + patterns
301
+ (named? ? [:graph, graph_name, res] : res).to_sxp(**options)
302
+ end
303
+ end
304
+
305
+ class RDF::Query::Pattern
306
+ # Transform Query Pattern into an SXP
307
+ #
308
+ # @return [String]
309
+ def to_sxp(**options)
310
+ [:triple, subject, predicate, object].to_sxp(**options)
311
+ end
312
+ end
313
+
314
+ class RDF::Query::Variable
315
+ ##
316
+ # Transform Query variable into an SXP.
317
+ #
318
+ # @return [String]
319
+ def to_sxp(**options)
320
+ prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
321
+ unbound? ? "#{prefix}#{name}".to_sym.to_sxp : ["#{prefix}#{name}".to_sym, value].to_sxp
322
+ end
323
+ end
24
324
  rescue LoadError
25
- # Ignore
325
+ # Ignore if RDF not loaded
26
326
  end
data/lib/sxp/generator.rb CHANGED
@@ -16,18 +16,41 @@ module SXP
16
16
 
17
17
  ##
18
18
  # @param [Object] obj
19
- def initialize(obj, indent)
19
+ # @param [Integer] indent
20
+ # @param [Hash{Symbol => RDF::URI}] prefixes (nil)
21
+ # @param [RDF::URI] base_uri (nil)
22
+ def initialize(obj, indent, prefixes: nil, base_uri: nil)
20
23
  @indent = indent
21
24
  @elements = []
25
+ @prefixes = prefixes
26
+ @base_uri = base_uri
22
27
  if obj.is_a?(Array)
23
- obj.compact.each {|o| @elements << Block.new(o, indent + 1)}
28
+ # If this is a base or prefix element, update our representations
29
+ if obj.first == :base && obj.length == 3 && obj[1].is_a?(RDF::URI)
30
+ base_uri = obj[1]
31
+ @elements << Block.new(:base, indent + 1)
32
+ @elements << Block.new(obj[1], indent + 1)
33
+ @elements << Block.new(obj.last, indent + 1, prefixes: prefixes, base_uri: base_uri)
34
+ elsif obj.first == :prefix && obj.length == 3 && obj[1].is_a?(Array)
35
+ prefixes = prefixes ? prefixes.dup : {}
36
+ obj[1].each do |defn|
37
+ prefixes[defn.first.to_s.chomp(':').to_sym] = RDF::URI(defn.last) if defn.is_a?(Array) && defn.length == 2
38
+ end
39
+ @elements << Block.new(:prefix, indent + 1)
40
+ @elements << Block.new(obj[1], indent + 1)
41
+ @elements << Block.new(obj.last, indent + 1, prefixes: prefixes, base_uri: base_uri)
42
+ else
43
+ obj.compact.each do |o|
44
+ @elements << Block.new(o, indent + 1, prefixes: prefixes, base_uri: base_uri)
45
+ end
46
+ end
24
47
  else
25
48
  @elements = obj
26
49
  end
27
50
  end
28
51
 
29
52
  ##
30
- # Agregate length over each element accounting for spaces
53
+ # Aggregate length over each element accounting for spaces
31
54
  #
32
55
  # @return [Integer]
33
56
  # If indent is not not nil, returns zero
@@ -35,7 +58,7 @@ module SXP
35
58
  if @elements.is_a?(Array)
36
59
  @elements.map(&:length).inject(:+).to_i + @elements.length - 1
37
60
  else
38
- @elements.to_sxp.length
61
+ @elements.to_sxp(prefixes: @prefixes, base_uri: @base_uri).length
39
62
  end
40
63
  end
41
64
 
@@ -44,8 +67,8 @@ module SXP
44
67
  # This should only be called on a block when
45
68
  # no indentation is to be applied
46
69
  # @return [String]
47
- def to_sxp
48
- @elements.to_sxp
70
+ def to_sxp(prefixes: nil, base_uri: nil)
71
+ @elements.to_sxp(prefixes: prefixes || @prefixes, base_uri: base_uri || @base_uri)
49
72
  end
50
73
 
51
74
  ##
@@ -67,7 +90,7 @@ module SXP
67
90
  first, *elems = @elements
68
91
  unless first.sxp?
69
92
  # It's atomic, write out after paren
70
- buffer += first.to_sxp + "\n"
93
+ buffer += first.to_sxp(prefixes: @prefixes, base_uri: @base_uri) + "\n"
71
94
  else
72
95
  buffer += "\n"
73
96
  elems.unshift(first)
@@ -77,7 +100,7 @@ module SXP
77
100
  end
78
101
  buffer += do_indent + ")\n"
79
102
  else
80
- buffer += do_indent + @elements.to_sxp + "\n"
103
+ buffer += do_indent + @elements.to_sxp(prefixes: @prefixes, base_uri: @base_uri) + "\n"
81
104
  end
82
105
  buffer
83
106
  end
data/lib/sxp/pair.rb CHANGED
@@ -27,7 +27,7 @@ module SXP
27
27
  # Returns `true` if the tail of this pair is not `nil` or another pair.
28
28
  #
29
29
  # @return [Boolean]
30
- # @see http://srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
30
+ # @see https:/srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
31
31
  def dotted?
32
32
  !proper?
33
33
  end
@@ -36,7 +36,7 @@ module SXP
36
36
  # Returns `true` if the tail of this pair is `nil` or another pair.
37
37
  #
38
38
  # @return [Boolean]
39
- # @see http://srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
39
+ # @see https:/srfi.schemers.org/srfi-1/srfi-1.html#ImproperLists
40
40
  def proper?
41
41
  tail.nil? || tail.is_a?(Pair)
42
42
  end
@@ -1,9 +1,11 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ require 'matrix'
3
+
2
4
  module SXP; class Reader
3
5
  ##
4
6
  # A Common Lisp S-expressions parser.
5
7
  #
6
- # @see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node14.html
8
+ # @see https:/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node14.html
7
9
  class CommonLisp < Basic
8
10
  OPTIONS = {nil: nil, t: true, quote: :quote, function: :function}
9
11
 
@@ -16,7 +18,7 @@ module SXP; class Reader
16
18
 
17
19
  # Escape characters, used in the form `#\Backspace`. Case is treated
18
20
  # insensitively
19
- # @see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node22.html
21
+ # @see https:/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node22.html
20
22
  CHARACTERS = {
21
23
  'newline' => "\n",
22
24
  'space' => " ",
@@ -88,7 +90,8 @@ module SXP; class Reader
88
90
  #
89
91
  # @return [Array]
90
92
  def read_vector
91
- raise NotImplementedError, "#{self.class}#read_vector" # TODO
93
+ list = read_list(')')
94
+ Vector.[](*list)
92
95
  end
93
96
 
94
97
  ##
@@ -114,7 +117,7 @@ module SXP; class Reader
114
117
  # eroneously read characters back in the input stream
115
118
  #
116
119
  # @return [String]
117
- # @see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node22.html
120
+ # @see https:/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node22.html
118
121
  def read_character
119
122
  lit = read_literal
120
123
 
@@ -3,7 +3,7 @@ module SXP; class Reader
3
3
  ##
4
4
  # A Scheme R4RS S-expressions parser.
5
5
  #
6
- # @see http://people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
6
+ # @see https:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
7
7
  class Scheme < Extended
8
8
  DECIMAL = /^[+-]?(\d*)?\.\d*$/
9
9
  INTEGER_BASE_2 = /^[+-]?[01]+$/
@@ -14,7 +14,7 @@ module SXP; class Reader
14
14
 
15
15
  # Escape characters, used in the form `#\newline`. Case is treated
16
16
  # insensitively
17
- # @see http://people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
17
+ # @see https:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
18
18
  CHARACTERS = {
19
19
  'newline' => "\n",
20
20
  'space' => " ",
@@ -64,8 +64,8 @@ module SXP; class Reader
64
64
  when ?d, ?D then read_integer(10)
65
65
  when ?x, ?X then read_integer(16)
66
66
  when ?\\ then read_character
67
- when ?; then skip; read
68
- when ?! then skip_line; read # shebang
67
+ when ?; then skip # comment character
68
+ when ?! then skip_line; skip # shebang
69
69
  else raise Error, "invalid sharp-sign read syntax: ##{char.chr}"
70
70
  end
71
71
  end
@@ -76,7 +76,7 @@ module SXP; class Reader
76
76
  # eroneously read characters back in the input stream
77
77
  #
78
78
  # @return [String]
79
- # @see http://people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
79
+ # @see https:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
80
80
  def read_character
81
81
  lit = read_literal
82
82
 
@@ -1,13 +1,13 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'rdf' # @see http://rubygems.org/gems/rdf
2
+ require 'rdf' # @see https:/rubygems.org/gems/rdf
3
3
 
4
4
  module SXP; class Reader
5
5
  ##
6
6
  # A SPARQL Syntax Expressions (SSE) parser.
7
7
  #
8
- # Requires [RDF.rb](http://rdf.rubyforge.org/).
8
+ # Requires [RDF.rb](https:/rubygems.org/gems/rdf/).
9
9
  #
10
- # @see http://openjena.org/wiki/SSE
10
+ # @see https:/openjena.org/wiki/SSE
11
11
  class SPARQL < Extended
12
12
  # Alias for rdf:type
13
13
  A = /^a$/
@@ -83,12 +83,12 @@ module SXP; class Reader
83
83
  end
84
84
 
85
85
  ##
86
- # Reads SSE Tokens, including {RDF::Literal}, {RDF::URI} and RDF::Node.
86
+ # Reads SSE Tokens, including `RDF::Literal`, `RDF::URI` and `RDF::Node`.
87
87
  #
88
88
  # Performs forward reference for prefix and base URI representations and saves in
89
89
  # {#base_uri} and {#prefixes} accessors.
90
90
  #
91
- # Transforms tokens matching a {PNAME} pattern into {RDF::URI} instances if a match is
91
+ # Transforms tokens matching a {PNAME} pattern into `RDF::URI` instances if a match is
92
92
  # found with a previously identified {PREFIX}.
93
93
  # @return [Object]
94
94
  def read_token
@@ -129,8 +129,6 @@ module SXP; class Reader
129
129
  uri = RDF::URI(base.to_s + suffix)
130
130
  #STDERR.puts "read_tok lexical uri: #{uri.inspect}"
131
131
 
132
- # Cause URI to be serialized as a lexical
133
- uri.lexical = value
134
132
  [:atom, uri]
135
133
  else
136
134
  tok
@@ -184,9 +182,7 @@ module SXP; class Reader
184
182
 
185
183
  # If we have a base URI, use that when constructing a new URI
186
184
  uri = if self.base_uri && RDF::URI(buffer).relative?
187
- u = self.base_uri.join(buffer)
188
- u.lexical = "<#{buffer}>" unless u.to_s == buffer # So that it can be re-serialized properly
189
- u
185
+ self.base_uri.join(buffer)
190
186
  else
191
187
  RDF::URI(buffer)
192
188
  end
@@ -211,7 +207,7 @@ module SXP; class Reader
211
207
  #
212
208
  # Atoms parsed including `base`, `prefix`, `true`, `false`, numeric, BNodes and variables.
213
209
  #
214
- # Creates {RDF::Literal}, RDF::Node, or {RDF::Query::Variable} instances where appropriate.
210
+ # Creates `RDF::Literal`, `RDF::Node`, or `RDF::Query::Variable` instances where appropriate.
215
211
  #
216
212
  # @return [Object]
217
213
  def read_atom
data/lib/sxp.rb CHANGED
@@ -1,23 +1,8 @@
1
1
  require 'rational'
2
2
  require 'stringio'
3
3
 
4
- if RUBY_VERSION < '1.8.7'
5
- # @see http://rubygems.org/gems/backports
6
- begin
7
- require 'backports/1.8.7'
8
- rescue LoadError
9
- begin
10
- require 'rubygems'
11
- require 'backports/1.8.7'
12
- rescue LoadError
13
- abort "SXP.rb requires Ruby 1.8.7 or the Backports gem (hint: `gem install backports')."
14
- end
15
- end
16
- end
17
-
18
4
  require 'sxp/version'
19
5
  require 'sxp/extensions'
20
- require 'sxp/writer'
21
6
 
22
7
  module SXP
23
8
  autoload :Pair, 'sxp/pair'
metadata CHANGED
@@ -1,58 +1,86 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sxp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
8
8
  - Gregg Kellogg
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-12-11 00:00:00.000000000 Z
12
+ date: 2021-12-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
15
+ name: rdf
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: matrix
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: amazing_print
16
44
  requirement: !ruby/object:Gem::Requirement
17
45
  requirements:
18
46
  - - "~>"
19
47
  - !ruby/object:Gem::Version
20
- version: '3.9'
48
+ version: '1.4'
21
49
  type: :development
22
50
  prerelease: false
23
51
  version_requirements: !ruby/object:Gem::Requirement
24
52
  requirements:
25
53
  - - "~>"
26
54
  - !ruby/object:Gem::Version
27
- version: '3.9'
55
+ version: '1.4'
28
56
  - !ruby/object:Gem::Dependency
29
- name: yard
57
+ name: rspec
30
58
  requirement: !ruby/object:Gem::Requirement
31
59
  requirements:
32
60
  - - "~>"
33
61
  - !ruby/object:Gem::Version
34
- version: 0.9.20
62
+ version: '3.10'
35
63
  type: :development
36
64
  prerelease: false
37
65
  version_requirements: !ruby/object:Gem::Requirement
38
66
  requirements:
39
67
  - - "~>"
40
68
  - !ruby/object:Gem::Version
41
- version: 0.9.20
69
+ version: '3.10'
42
70
  - !ruby/object:Gem::Dependency
43
- name: rdf
71
+ name: yard
44
72
  requirement: !ruby/object:Gem::Requirement
45
73
  requirements:
46
74
  - - "~>"
47
75
  - !ruby/object:Gem::Version
48
- version: '3.1'
49
- type: :runtime
76
+ version: '0.9'
77
+ type: :development
50
78
  prerelease: false
51
79
  version_requirements: !ruby/object:Gem::Requirement
52
80
  requirements:
53
81
  - - "~>"
54
82
  - !ruby/object:Gem::Version
55
- version: '3.1'
83
+ version: '0.9'
56
84
  description: Universal S-expression parser with specific support for Common Lisp,
57
85
  Scheme, and RDF/SPARQL
58
86
  email:
@@ -87,12 +115,11 @@ files:
87
115
  - lib/sxp/reader/scheme.rb
88
116
  - lib/sxp/reader/sparql.rb
89
117
  - lib/sxp/version.rb
90
- - lib/sxp/writer.rb
91
- homepage: http://sxp.rubyforge.org/
118
+ homepage: https://github.com/dryruby/sxp/
92
119
  licenses:
93
120
  - Unlicense
94
121
  metadata: {}
95
- post_install_message:
122
+ post_install_message:
96
123
  rdoc_options: []
97
124
  require_paths:
98
125
  - lib
@@ -100,15 +127,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
127
  requirements:
101
128
  - - ">="
102
129
  - !ruby/object:Gem::Version
103
- version: '2.4'
130
+ version: '2.6'
104
131
  required_rubygems_version: !ruby/object:Gem::Requirement
105
132
  requirements:
106
133
  - - ">="
107
134
  - !ruby/object:Gem::Version
108
135
  version: '0'
109
136
  requirements: []
110
- rubygems_version: 3.0.6
111
- signing_key:
137
+ rubygems_version: 3.3.3
138
+ signing_key:
112
139
  specification_version: 4
113
140
  summary: A pure-Ruby implementation of a universal S-expression parser.
114
141
  test_files: []
data/lib/sxp/writer.rb DELETED
@@ -1,216 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require 'bigdecimal'
3
- require 'time'
4
-
5
- ##
6
- # Extensions for Ruby's `Object` class.
7
- class Object
8
- ##
9
- # Returns the SXP representation of this object.
10
- #
11
- # @return [String]
12
- def to_sxp
13
- to_s.to_json
14
- end
15
- end
16
-
17
- ##
18
- # Extensions for Ruby's `NilClass` class.
19
- class NilClass
20
- ##
21
- # Returns the SXP representation of this object.
22
- #
23
- # @return [String]
24
- def to_sxp
25
- '#n'
26
- end
27
- end
28
-
29
- ##
30
- # Extensions for Ruby's `FalseClass` class.
31
- class FalseClass
32
- ##
33
- # Returns the SXP representation of this object.
34
- #
35
- # @return [String]
36
- def to_sxp
37
- '#f'
38
- end
39
- end
40
-
41
- ##
42
- # Extensions for Ruby's `TrueClass` class.
43
- class TrueClass
44
- ##
45
- # Returns the SXP representation of this object.
46
- #
47
- # @return [String]
48
- def to_sxp
49
- '#t'
50
- end
51
- end
52
-
53
- ##
54
- # Extensions for Ruby's `String` class.
55
- class String
56
- ##
57
- # Returns the SXP representation of this object.
58
- #
59
- # @return [String]
60
- def to_sxp
61
- inspect
62
- end
63
- end
64
-
65
- ##
66
- # Extensions for Ruby's `Symbol` class.
67
- class Symbol
68
- ##
69
- # Returns the SXP representation of this object.
70
- #
71
- # @return [String]
72
- def to_sxp
73
- to_s
74
- end
75
- end
76
-
77
- ##
78
- # Extensions for Ruby's `Integer` class.
79
- class Integer
80
- ##
81
- # Returns the SXP representation of this object.
82
- #
83
- # @return [String]
84
- def to_sxp
85
- to_s
86
- end
87
- end
88
-
89
- ##
90
- # Extensions for Ruby's `BigDecimal` class.
91
- class BigDecimal
92
- ##
93
- # Returns the SXP representation of this object.
94
- #
95
- # @return [String]
96
- def to_sxp
97
- to_f.to_s
98
- end
99
- end
100
-
101
- ##
102
- # Extensions for Ruby's `Float` class.
103
- class Float
104
- ##
105
- # Returns the SXP representation of this object.
106
- #
107
- # @return [String]
108
- def to_sxp
109
- case
110
- when nan? then 'nan.0'
111
- when infinite? then (infinite? > 0 ? '+inf.0' : '-inf.0')
112
- else to_s
113
- end
114
- end
115
- end
116
-
117
- ##
118
- # Extensions for Ruby's `Array` class.
119
- class Array
120
- ##
121
- # Returns the SXP representation of this object.
122
- #
123
- # @return [String]
124
- def to_sxp
125
- '(' << map { |x| x.to_sxp }.join(' ') << ')'
126
- end
127
- end
128
-
129
- ##
130
- # Extensions for Ruby's `Time` class.
131
- class Time
132
- ##
133
- # Returns the SXP representation of this object.
134
- #
135
- # @return [String]
136
- def to_sxp
137
- '#@' << (respond_to?(:xmlschema) ? xmlschema : to_i).to_s
138
- end
139
- end
140
-
141
- ##
142
- # Extensions for Ruby's `Regexp` class.
143
- class Regexp
144
- ##
145
- # Returns the SXP representation of this object.
146
- #
147
- # @return [String]
148
- def to_sxp
149
- '#' << inspect
150
- end
151
- end
152
-
153
- begin
154
- require 'rdf' # For SPARQL
155
-
156
- class RDF::URI
157
- ##
158
- # Returns the SXP representation of this object.
159
- #
160
- # @return [String]
161
- def to_sxp; lexical || "<#{self}>"; end
162
- end
163
-
164
- class RDF::Node
165
- ##
166
- # Returns the SXP representation of this object.
167
- #
168
- # @return [String]
169
- def to_sxp; to_s; end
170
- end
171
-
172
- class RDF::Literal
173
- ##
174
- # Returns the SXP representation of a Literal.
175
- #
176
- # @return [String]
177
- def to_sxp
178
- case datatype
179
- when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double, RDF::XSD.decimal, RDF::XSD.time
180
- # Retain stated lexical form if possible
181
- valid? ? to_s : object.to_sxp
182
- else
183
- text = value.dump
184
- text << "@#{language}" if self.has_language?
185
- text << "^^#{datatype.to_sxp}" if self.has_datatype?
186
- text
187
- end
188
- end
189
- end
190
-
191
- class RDF::Query
192
- # Transform Query into an Array form of an SXP
193
- #
194
- # If Query is named, it's treated as a GroupGraphPattern, otherwise, a BGP
195
- #
196
- # @return [Array]
197
- def to_sxp
198
- res = [:bgp] + patterns
199
- (named? ? [:graph, graph_name, res] : res).to_sxp
200
- end
201
- end
202
-
203
- class RDF::Query::Pattern
204
- # Transform Query Pattern into an SXP
205
- # @return [String]
206
- def to_sxp
207
- [:triple, subject, predicate, object].to_sxp
208
- end
209
- end
210
-
211
- class RDF::Query::Variable
212
- def to_sxp; to_s; end
213
- end
214
- rescue LoadError => e
215
- # Ignore if RDF not loaded
216
- end