sexpistol 0.0.6 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc DELETED
@@ -1,73 +0,0 @@
1
- = Sexpistol
2
-
3
- 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.
4
-
5
- === Example
6
-
7
- (define test (lambda () (
8
- (print "Hello world!\n")
9
- (print 1)
10
- (print 9.01)
11
- (print 2.0e10)
12
- (print (+ 10 12 13))
13
- )))
14
-
15
- would be parsed by Sexpistol like so:
16
-
17
- [:define, :test, [:lambda, [], [
18
- [:print, "Hello world!\n"],
19
- [:print, 1],
20
- [:print, 9.01],
21
- [:print, 2.0e10],
22
- [:print, [:+, 10, 12, 13]]
23
- ]]]
24
-
25
- === Type mappings
26
-
27
- Sexpistol supports all of the standard datatypes and converts them directly to their Ruby equivalents:
28
-
29
- * Lists (a b c)
30
- * Integers (1 2 3)
31
- * Floats (1.0 2.0 42.9 3e6 1.2e2)
32
- * Strings ("\t\"Hello world!\"\n")
33
- * Symbols (symbol Symbol ____symbol____ symbo_l symbol? symbol! + - / ++ a+ e$, etc...)
34
-
35
- 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:
36
-
37
- @parser = Sexpistol.new
38
- @parser.parse_string("nil false true")
39
- #=> [:nil, :false, :true]
40
-
41
- @parser.ruby_keyword_literals = true
42
- @parser.parse_string("nil false true")
43
- #=> [nil, false, true]
44
-
45
- === Installation
46
-
47
- For convenience Sexpistol is packaged as a RubyGem, to install it simply enter the following at your command line:
48
-
49
- gem install sexpistol
50
-
51
- === Usage
52
-
53
- # Create a new parser instance
54
- @parser = Sexpistol.new
55
-
56
- # Parse a string
57
- ast = @parser.parse_string("(string (to (parse)))")
58
- #=> [:string, [:to, [:parse]]]
59
-
60
- # Change the representation
61
- ast[1][0] = :is
62
- ast[1][1][0] = :parsed
63
- #=> [:string, [:is, [:parsed]]]
64
-
65
- # Turn the array structure back into an S-Expression
66
- @parser.to_sexp( ast )
67
- #=> "( string ( is ( parsed ) ) )"
68
-
69
- === Author & Credits
70
-
71
- Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
72
-
73
- Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license
data/Rakefile DELETED
@@ -1,42 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- begin
9
- require 'jeweler'
10
- Jeweler::Tasks.new do |gemspec|
11
- gemspec.name = "sexpistol"
12
- gemspec.summary = "An S-Expression Parser Library for Ruby"
13
- gemspec.description = "Sexpistol is an easy-to-use S-Expression parser for Ruby. It is fast and has no dependencies."
14
- gemspec.email = "aaron@aarongough.com"
15
- gemspec.homepage = "http://github.com/aarongough/sexpistol"
16
- gemspec.authors = ["Aaron Gough"]
17
- gemspec.rdoc_options << '--line-numbers' << '--inline-source'
18
- gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
- end
20
- rescue LoadError
21
- puts "Jeweler not available. Install it with: gem install jeweler"
22
- end
23
-
24
-
25
- desc 'Test sexpistol.'
26
- Rake::TestTask.new(:test) do |t|
27
- t.libs << 'lib/*.rb'
28
- t.libs << 'test'
29
- t.pattern = 'test/**/*_test.rb'
30
- t.verbose = true
31
- end
32
-
33
-
34
- desc 'Generate documentation for sexpistol.'
35
- Rake::RDocTask.new(:rdoc) do |rdoc|
36
- rdoc.rdoc_dir = 'rdoc'
37
- rdoc.title = 'Koi'
38
- rdoc.options << '--line-numbers' << '--inline-source'
39
- rdoc.rdoc_files.include('README.rdoc')
40
- rdoc.rdoc_files.include('lib/**/*.rb')
41
- rdoc.rdoc_files.include('app/**/*.rb')
42
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.6
@@ -1,154 +0,0 @@
1
- # This class contains our logic for parsing
2
- # S-Expressions. They are turned into a
3
- # native Ruby representation like:
4
- # [:def, :something [:lambda, [:a], [:do_something]]]
5
- class Sexpistol
6
-
7
- attr_accessor :ruby_keyword_literals
8
-
9
- def initialize
10
- @ruby_keyword_literals = false
11
-
12
- # Setup all of our token patterns as instance variables
13
- # so they don't have to be re-instantiated for each
14
- # run & match
15
- @string_literal_pattern = /"([^"\\]|\\.)*"/
16
- @integer_literal_pattern = /[\-\+]?[0-9]+/
17
- @float_literal_pattern = /[\-\+]?[0-9]+\.[0-9]+(e[0-9]+)?/
18
- @symbol_pattern = /[^\"\'\,\(\)]+/
19
-
20
- @string_replacement_token = "__++STRING_LITERAL++__"
21
- end
22
-
23
- # Parse a string containing an S-Expression into a
24
- # nested set of Ruby arrays
25
- def parse_string( string )
26
- string_array = split_outside_strings(string)
27
- tokens = process_tokens( string_array )
28
- structure( tokens )[1]
29
- end
30
-
31
- # Iterate over an array of strings and turn each
32
- # item into it's relevant token. ie: "string" -> :string, "1" -> 1
33
- def process_tokens( token_array )
34
- tokens = []
35
- token_array.each do |t|
36
- if(@ruby_keyword_literals)
37
- tokens << nil and next if(t == "nil")
38
- tokens << true and next if(t == "true")
39
- tokens << false and next if(t == "false")
40
- end
41
- tokens << "'" and next if(t == "'")
42
- tokens << t and next if(t == "(" || t == ")")
43
- tokens << t.to_f and next if( is_float?(t))
44
- tokens << t.to_i and next if( is_integer?(t))
45
- tokens << t.to_sym and next if( is_symbol?(t))
46
- tokens << eval(t) and next if( is_string_literal?(t))
47
- raise "\nUnrecognized token: #{t}\n"
48
- end
49
- return tokens
50
- end
51
-
52
- # Iterate over a flat array of tokens and turn it
53
- # a nested set of arrays by detecting '(' and ')'
54
- def structure( token_array, offset = 0, internal = false )
55
- program = []
56
- while(offset < token_array.length)
57
- if( token_array[offset] == "(" )
58
- offset, array = structure( token_array, offset + 1, true )
59
- program << array
60
- elsif( token_array[offset] == "'" )
61
- offset, array = structure( token_array, offset + 1, true )
62
- program << array.unshift( :quote )
63
- elsif( token_array[offset] == ")" )
64
- break
65
- else
66
- program << token_array[offset]
67
- end
68
- offset += 1
69
- end
70
- return [offset, program]
71
- end
72
-
73
- # Split up a string into an array where delimited by whitespace,
74
- # except inside string literals
75
- def split_outside_strings( string )
76
- # Find and extract all the string literals
77
- string_literals = []
78
- string = string.gsub(@string_literal_pattern) do |x|
79
- string_literals << x
80
- @string_replacement_token
81
- end
82
- # Make sure the s-expression is valid
83
- unless( string.count("(") == string.count(")") )
84
- raise Exception, "Invalid S-Expression. The number of opening and closing parentheses does not match."
85
- end
86
- # Split the string up on whitespace and parentheses
87
- array = string.gsub("(", " ( ").gsub(")", " ) ").split(" ")
88
- # replace the special string token with the original string literals
89
- array.collect! do |x|
90
- if( x == @string_replacement_token)
91
- string_literals.shift
92
- else
93
- x
94
- end
95
- end
96
- return array
97
- end
98
-
99
- # Test to see whether or not a string represents an integer
100
- def is_integer?( string )
101
- is_match?( string, @integer_literal_pattern )
102
- end
103
-
104
- # Test to see whether or not a string represents a float
105
- def is_float?( string )
106
- is_match?( string, @float_literal_pattern )
107
- end
108
-
109
- # Test to see whether or not a string represents a symbol
110
- def is_symbol?( string )
111
- is_match?( string, @symbol_pattern )
112
- end
113
-
114
- # Test to see whether or not a string represents a string literal
115
- def is_string_literal?( string )
116
- is_match?( string, @string_literal_pattern )
117
- end
118
-
119
- # Convert a set of nested arrays back into an S-Expression
120
- def to_sexp( data )
121
- if( data.is_a?(Array))
122
- mapped = data.map do |item|
123
- if( item.is_a?(Array))
124
- to_sexp(item)
125
- else
126
- if(item === false)
127
- "#f"
128
- elsif(item === true)
129
- "#t"
130
- else
131
- item.to_s
132
- end
133
- end
134
- end
135
- "(" + mapped.join(" ") + ")"
136
- else
137
- if(data === false)
138
- "#f"
139
- elsif(data === true)
140
- "#t"
141
- else
142
- data.to_s
143
- end
144
- end
145
- end
146
-
147
- private
148
-
149
- def is_match?( string, pattern )
150
- match = string.match(pattern)
151
- return false unless match
152
- match[0].length == string.length
153
- end
154
- end
@@ -1,34 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class BenchmarkTest < Test::Unit::TestCase
4
-
5
- require 'benchmark'
6
-
7
- test "benchmark sexpistol" do
8
- puts "\nRunning performance test...\n"
9
- parser = Sexpistol.new
10
- parser.ruby_keyword_literals = true
11
- Benchmark.bmbm do |b|
12
- b.report do
13
- 5000.times do
14
- parser.parse_string <<-EOD
15
-
16
- (display "This is a test string!")
17
-
18
- (define test (lambda () (begin
19
- (display (== 1 1))
20
- (display (== true true))
21
- (display (== false false))
22
- (display (== nil nil))
23
- (display (== 2.09 1.08))
24
- (display (== 2e6 2e12))
25
- )))
26
-
27
- EOD
28
- end
29
- end
30
- end
31
- puts
32
- end
33
-
34
- end
@@ -1,21 +0,0 @@
1
- module Test::Unit
2
- # Used to fix a minor minitest/unit incompatibility in flexmock
3
- AssertionFailedError = Class.new(StandardError)
4
-
5
- class TestCase
6
-
7
- def self.test(name, &block)
8
- test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
9
- defined = instance_method(test_name) rescue false
10
- raise "#{test_name} is already defined in #{self}" if defined
11
- if block_given?
12
- define_method(test_name, &block)
13
- else
14
- define_method(test_name) do
15
- flunk "No implementation provided for #{name}"
16
- end
17
- end
18
- end
19
-
20
- end
21
- end
data/test/test_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
-
4
- require_files = []
5
- require_files << File.join(File.dirname(__FILE__), '..', 'lib', 'sexpistol.rb')
6
- require_files.concat Dir[File.join(File.dirname(__FILE__), 'setup', '*.rb')]
7
-
8
- require_files.each do |file|
9
- require File.expand_path(file)
10
- end
@@ -1,35 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class FloatLiteralTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse sexp containing an implicitly positive float literal" do
10
- ast = @parser.parse_string("10.00")
11
- assert_equal [10.00], ast
12
- end
13
-
14
- test "should parse sexp containing an explicitly positive float literal" do
15
- ast = @parser.parse_string("+910.00")
16
- assert_equal [910.00], ast
17
- end
18
-
19
- test "should parse sexp containing an explicitly negative float literal" do
20
- ast = @parser.parse_string("-10.00")
21
- assert_equal [-10.00], ast
22
- end
23
-
24
- test "should parse sexp containing a large float literal" do
25
- ast = @parser.parse_string("1.0000127829")
26
- assert_equal [1.0000127829], ast
27
- end
28
-
29
- test "should parse sexp containing a float defined in scientific notation" do
30
- ast = @parser.parse_string("1.0e6")
31
- assert_equal [1.0e6], ast
32
- end
33
-
34
-
35
- end
@@ -1,24 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class IntegerLiteralTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse sexp containing an implicitly positive integer literal" do
10
- ast = @parser.parse_string("10")
11
- assert_equal [10], ast
12
- end
13
-
14
- test "should parse sexp containing an explicitly positive integer literal" do
15
- ast = @parser.parse_string("+910")
16
- assert_equal [910], ast
17
- end
18
-
19
- test "should parse sexp containing an explicitly negative integer literal" do
20
- ast = @parser.parse_string("-10")
21
- assert_equal [-10], ast
22
- end
23
-
24
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class RubyKeywordLiteralsTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse nil as literal" do
10
- @parser.ruby_keyword_literals = true
11
- ast = @parser.parse_string('nil')
12
- assert_equal [nil], ast
13
- end
14
-
15
- test "should not parse nil as literal" do
16
- @parser.ruby_keyword_literals = false
17
- ast = @parser.parse_string('nil')
18
- assert_equal [:nil], ast
19
- end
20
-
21
- test "should parse true as literal" do
22
- @parser.ruby_keyword_literals = true
23
- ast = @parser.parse_string('true')
24
- assert_equal [true], ast
25
- end
26
-
27
- test "should not parse true as literal" do
28
- @parser.ruby_keyword_literals = false
29
- ast = @parser.parse_string('true')
30
- assert_equal [:true], ast
31
- end
32
-
33
- test "should parse false as literal" do
34
- @parser.ruby_keyword_literals = true
35
- ast = @parser.parse_string('false')
36
- assert_equal [false], ast
37
- end
38
-
39
- test "should notparse false as literal" do
40
- @parser.ruby_keyword_literals = false
41
- ast = @parser.parse_string('false')
42
- assert_equal [:false], ast
43
- end
44
-
45
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class SchemeCompatabilityTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse #t as true" do
10
- ast = @parser.parse_string('#t')
11
- assert_equal [:"#t"], ast
12
- end
13
-
14
- test "should parse #f as false" do
15
- ast = @parser.parse_string('#f')
16
- assert_equal [:"#f"], ast
17
- end
18
-
19
- test "should allow comma quoting" do
20
- ast = @parser.parse_string("(this is '( a test))")
21
- assert_equal [[:this, :is, [:quote, [:a, :test]]]], ast
22
- end
23
-
24
- test "should allow complicated comma quoting" do
25
- ast = @parser.parse_string("(this is '( a test (also)))")
26
- assert_equal [[:this, :is, [:quote, [:a, :test, [:also]]]]], ast
27
- end
28
-
29
- end
@@ -1,39 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class StringLiteralTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse empty string literal" do
10
- ast = @parser.parse_string('""')
11
- assert_equal [""], ast
12
- end
13
-
14
- test "should parse string literal" do
15
- ast = @parser.parse_string('"test"')
16
- assert_equal ["test"], ast
17
- end
18
-
19
- test "should parse string literal containing escaped quotes" do
20
- ast = @parser.parse_string('"te\"st"')
21
- assert_equal ["te\"st"], ast
22
- end
23
-
24
- test "should parse string literal containing escaped characters" do
25
- ast = @parser.parse_string('"\n\t\r"')
26
- assert_equal ["\n\t\r"], ast
27
- end
28
-
29
- test "should parse string literal containing spaces" do
30
- ast = @parser.parse_string('"blah foo"')
31
- assert_equal ["blah foo"], ast
32
- end
33
-
34
- test "should parse string literal containing newlines" do
35
- ast = @parser.parse_string('"blah' + "\n" + 'foo"')
36
- assert_equal ["blah\nfoo"], ast
37
- end
38
-
39
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class StructureTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should create nested set of arrays from s-expression" do
10
- ast = @parser.parse_string('(this (is (an (s_expression) (also) blah) foo) (test))')
11
- assert_equal [[:this, [:is, [:an, [:s_expression], [:also], :blah], :foo], [:test]]], ast
12
- end
13
-
14
- test "should create nested set of arrays from s-expression with string literals" do
15
- ast = @parser.parse_string('(this (is (an ("s_expression"))))')
16
- assert_equal [[:this, [:is, [:an, ["s_expression"]]]]], ast
17
- end
18
-
19
- test "should raise error on broken s-expression" do
20
- assert_raises Exception do
21
- ast = @parser.parse_string('(this (is (an (s_expression) too)')
22
- end
23
- end
24
-
25
- test "should parser () as empty list" do
26
- ast = @parser.parse_string('()')
27
- assert_equal [[]], ast
28
- end
29
-
30
- end
@@ -1,74 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class SymbolTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should parse simple symbol" do
10
- ast = @parser.parse_string("test")
11
- assert_equal [:test], ast
12
- end
13
-
14
- test "should parse symbol with trailing exclamation mark" do
15
- ast = @parser.parse_string("test!")
16
- assert_equal [:test!], ast
17
- end
18
-
19
- test "should parse symbol with trailing question mark" do
20
- ast = @parser.parse_string("test?")
21
- assert_equal [:test?], ast
22
- end
23
-
24
- test "should parse symbol containing underscores" do
25
- ast = @parser.parse_string("te__st")
26
- assert_equal [:te__st], ast
27
- end
28
-
29
- test "should parse symbol with leading underscores" do
30
- ast = @parser.parse_string("__test")
31
- assert_equal [:__test], ast
32
- end
33
-
34
- test "should parse symbol with trailing underscores" do
35
- ast = @parser.parse_string("test__")
36
- assert_equal [:test__], ast
37
- end
38
-
39
- test "should parse CamelCase symbol" do
40
- ast = @parser.parse_string("TestSymbol")
41
- assert_equal [:TestSymbol], ast
42
- end
43
-
44
- test "should parse complex symbol" do
45
- ast = @parser.parse_string("__TestSymbol_TEST__?")
46
- assert_equal [:__TestSymbol_TEST__?], ast
47
- end
48
-
49
- test "should parse symbol containing addition operators" do
50
- ast = @parser.parse_string("+")
51
- assert_equal [:+], ast
52
- end
53
-
54
- test "should parse symbol containing multiplication operators" do
55
- ast = @parser.parse_string("*")
56
- assert_equal [:*], ast
57
- end
58
-
59
- test "should parse symbol containing subtraction operators" do
60
- ast = @parser.parse_string("-")
61
- assert_equal [:-], ast
62
- end
63
-
64
- test "should parse symbol containing division operators" do
65
- ast = @parser.parse_string("/")
66
- assert_equal [:"/"], ast
67
- end
68
-
69
- test "should parse symbol containing any character except single and double quotes, backquote, parentheses and comma" do
70
- ast = @parser.parse_string("~1!2@3#4$%5^6&7*890-_+=|\]}[{poiuytrewqasdfghjklmnbvcxzZXCVBNMLKJHGFDSAQWERTYUIOP:;/?><")
71
- assert_equal [:"~1!2@3#4$%5^6&7*890-_+=|\]}[{poiuytrewqasdfghjklmnbvcxzZXCVBNMLKJHGFDSAQWERTYUIOP:;/?><"], ast
72
- end
73
-
74
- end
@@ -1,51 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
-
3
- class ToSexpTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @parser = Sexpistol.new
7
- end
8
-
9
- test "should convert nested arrays back into an S-Expression" do
10
- ast = [:string, [:is, [:parsed]]]
11
- sexp = @parser.to_sexp(ast)
12
- assert_equal "(string (is (parsed)))", sexp
13
- end
14
-
15
- test "should structure containing integers and strings back into an S-Expression" do
16
- ast = ["String!", [1, [2, "Other string."]]]
17
- sexp = @parser.to_sexp(ast)
18
- assert_equal "(String! (1 (2 Other string.)))", sexp
19
- end
20
-
21
- test "should output true and false using scheme notation" do
22
- ast = [true, [false, [true, false]]]
23
- sexp = @parser.to_sexp(ast)
24
- assert_equal "(#t (#f (#t #f)))", sexp
25
- end
26
-
27
- test "when not passed array to_sexp should print value (integer)" do
28
- ast = 1
29
- sexp = @parser.to_sexp(ast)
30
- assert_equal "1", sexp
31
- end
32
-
33
- test "when not passed array to_sexp should print value (string)" do
34
- ast = "test"
35
- sexp = @parser.to_sexp(ast)
36
- assert_equal "test", sexp
37
- end
38
-
39
- test "when not passed array to_sexp should print value (symbol)" do
40
- ast = :test
41
- sexp = @parser.to_sexp(ast)
42
- assert_equal "test", sexp
43
- end
44
-
45
- test "lists passed to to_sexp should have not extraneous spaces" do
46
- ast = [1, 2, 3]
47
- sexp = @parser.to_sexp(ast)
48
- assert_equal "(1 2 3)", sexp
49
- end
50
-
51
- end