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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/sexpistol/parser.rb +15 -2
- data/lib/sexpistol/version.rb +1 -1
- data/lib/sexpistol.rb +21 -37
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a23e0ce0544e9283ef642b526913ab4cc8e02b2d5cb99639b39093e2fdbad776
|
4
|
+
data.tar.gz: 79ff60a0e79eb517f9ace663c86a29eb0923721516316161ac8a85e212164857
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12df1844e76a6c3f054e10ce2b27a6438c5774cb8b099296263e2ecb05e5d56d269d5cf1f7cf6bafe3f3e6331cf53243b470cd61d311ec6f86b23d00e133ca56
|
7
|
+
data.tar.gz: d572e231a3aef14615db2587d4889a14b69d6912c56ca592bdb8f503316e55be559e491fd76219ecf46368232722104e25c0ca0ceb1db02c3d7ee8deb4b95f45
|
data/Gemfile.lock
CHANGED
data/lib/sexpistol/parser.rb
CHANGED
@@ -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
|
data/lib/sexpistol/version.rb
CHANGED
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
19
|
-
|
13
|
+
def to_sexp(data, scheme_compatability: false)
|
14
|
+
data = convert_scheme_literals(data) if scheme_compatability
|
20
15
|
|
21
|
-
|
22
|
-
data.
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
49
|
-
return data.map { |x| recursive_map(x, &block) } if data.is_a?(Array)
|
26
|
+
private
|
50
27
|
|
51
|
-
|
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
|