sexpistol 0.1.1 → 0.1.2

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: c36941d9da82ca386be18aaba9fcea89d742b28a6a6ff9617967187bf99f88ed
4
- data.tar.gz: a2c621597fa0fff8217e7f1a5f85d5b87c2888b8ccb4a8b54bc0f48618785872
3
+ metadata.gz: a23e0ce0544e9283ef642b526913ab4cc8e02b2d5cb99639b39093e2fdbad776
4
+ data.tar.gz: 79ff60a0e79eb517f9ace663c86a29eb0923721516316161ac8a85e212164857
5
5
  SHA512:
6
- metadata.gz: 0eba95203d5f768aa0cbf5be24fa4d23a86adad03eb00e2bc7191dc08bf049788fdd306fde8c97bd13f47136bc3deb91670a37d5e48421ac9e2b94a7fe644ed8
7
- data.tar.gz: 61938f7c6826683bd3514a51941a45d01ca514ea2bd153329185da7604213e7ce12b3585bee666ea48196e3c8d0150c53b7536e1a5093e30e67f700e73564c9d
6
+ metadata.gz: 12df1844e76a6c3f054e10ce2b27a6438c5774cb8b099296263e2ecb05e5d56d269d5cf1f7cf6bafe3f3e6331cf53243b470cd61d311ec6f86b23d00e133ca56
7
+ data.tar.gz: d572e231a3aef14615db2587d4889a14b69d6912c56ca592bdb8f503316e55be559e491fd76219ecf46368232722104e25c0ca0ceb1db02c3d7ee8deb4b95f45
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sexpistol (0.1.0)
4
+ sexpistol (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -10,10 +10,12 @@ class Sexpistol
10
10
  INTEGER = /([\-+]?[0-9]+)(?=[\s()])/.freeze
11
11
  SYMBOL = /[^0-9()\s]+[^()\s]*/.freeze
12
12
 
13
- def initialize(string)
13
+ def initialize(string, parse_ruby_keyword_literals = false)
14
14
  raise 'String given is not an s-expression' if string.strip[0] != '('
15
15
  raise 'Invalid s-expression' if string.count('(') != string.count(')')
16
16
 
17
+ @parse_ruby_keyword_literals = parse_ruby_keyword_literals
18
+
17
19
  super(string.strip)
18
20
  end
19
21
 
@@ -22,7 +24,7 @@ class Sexpistol
22
24
  case token = fetch_token
23
25
  when ')' then break
24
26
  when '(' then exp << parse(level + 1)
25
- when String, Integer, Float, Symbol then exp << token
27
+ when String, Integer, Float, Symbol then exp << convert_ruby_keyword_literals(token)
26
28
  end
27
29
  end
28
30
 
@@ -45,5 +47,16 @@ class Sexpistol
45
47
 
46
48
  raise "Invalid token at position #{pos} near '#{scan(/.{0,20}/)}'."
47
49
  end
50
+
51
+ def convert_ruby_keyword_literals(token)
52
+ return token unless @parse_ruby_keyword_literals && token.is_a?(Symbol)
53
+
54
+ case token
55
+ when :nil then nil
56
+ when :true then true
57
+ when :false then false
58
+ else token
59
+ end
60
+ end
48
61
  end
49
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sexpistol
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/sexpistol.rb CHANGED
@@ -5,49 +5,33 @@ require 'sexpistol/version'
5
5
  require 'sexpistol/s_expression_array'
6
6
 
7
7
  class Sexpistol
8
- def self.parse(string, parse_ruby_keyword_literals: false)
9
- tree = Sexpistol::Parser.new(string).parse
10
- return convert_ruby_keyword_literals(tree) if parse_ruby_keyword_literals
11
-
12
- tree
13
- end
14
-
15
- def self.to_sexp(data, scheme_compatability: false)
16
- data = convert_scheme_literals(data) if scheme_compatability
8
+ class << self
9
+ def parse(string, parse_ruby_keyword_literals: false)
10
+ Sexpistol::Parser.new(string, parse_ruby_keyword_literals).parse
11
+ end
17
12
 
18
- return "\"#{data}\"" if data.is_a?(String)
19
- return data.to_s unless data.is_a?(Array)
13
+ def to_sexp(data, scheme_compatability: false)
14
+ data = convert_scheme_literals(data) if scheme_compatability
20
15
 
21
- if data.is_a?(SExpressionArray)
22
- data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')
23
- else
24
- "(#{data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')})"
25
- end
26
- end
16
+ return "\"#{data}\"" if data.is_a?(String)
17
+ return data.to_s unless data.is_a?(Array)
27
18
 
28
- def self.convert_ruby_keyword_literals(expression)
29
- recursive_map(expression) do |x|
30
- case x
31
- when :nil then nil
32
- when :true then true
33
- when :false then false
34
- else x
19
+ if data.is_a?(SExpressionArray)
20
+ data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')
21
+ else
22
+ "(#{data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')})"
35
23
  end
36
24
  end
37
- end
38
-
39
- def self.convert_scheme_literals(data)
40
- case data
41
- when nil then []
42
- when true then :'#t'
43
- when false then :'#f'
44
- else data
45
- end
46
- end
47
25
 
48
- def self.recursive_map(data, &block)
49
- return data.map { |x| recursive_map(x, &block) } if data.is_a?(Array)
26
+ private
50
27
 
51
- block.call(data)
28
+ def convert_scheme_literals(data)
29
+ case data
30
+ when nil then []
31
+ when true then :'#t'
32
+ when false then :'#f'
33
+ else data
34
+ end
35
+ end
52
36
  end
53
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sexpistol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Gough