sexp 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/sexp.rb +105 -0
- metadata +53 -0
data/sexp.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require_gem 'multi'
|
5
|
+
require 'smulti'
|
6
|
+
|
7
|
+
module SExpressionParser
|
8
|
+
class Base
|
9
|
+
def initialize(text, *start)
|
10
|
+
@list = start
|
11
|
+
parse(text)
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(item)
|
15
|
+
@list.push(item)
|
16
|
+
end
|
17
|
+
|
18
|
+
def leave(text)
|
19
|
+
@unwanted = text
|
20
|
+
end
|
21
|
+
|
22
|
+
def unwanted
|
23
|
+
return @unwanted
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_parse(item, rest)
|
27
|
+
add(item)
|
28
|
+
return parse(rest)
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_leave(item, rest)
|
32
|
+
add(item)
|
33
|
+
leave(rest)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
SymbolRE = /[\w\-\+\/\*\?\!]/
|
38
|
+
SymbolREMany = /[\w\-\+\/\*\?\!]*/
|
39
|
+
NumberRE = /[\d\.]/
|
40
|
+
NumberREMany = /[\d\.]*/
|
41
|
+
|
42
|
+
class Main
|
43
|
+
def initialize(text)
|
44
|
+
smulti(:parse, /\s/) {|c, rest| parse(rest) }
|
45
|
+
smulti(:parse, /\(/) {|c, rest| @res = List.new(rest) }
|
46
|
+
smulti(:parse, /\"/) {|c, rest| @res = String.new(rest) }
|
47
|
+
smulti(:parse, NumberRE) {|c, rest| @res = Number.new(rest, c) }
|
48
|
+
smulti(:parse, SymbolRE) {|c, rest| @res = Symbol.new(rest, c) }
|
49
|
+
parse(text)
|
50
|
+
end
|
51
|
+
|
52
|
+
def unwanted; return @res.unwanted(); end
|
53
|
+
def value; return @res.value() ; end
|
54
|
+
end
|
55
|
+
|
56
|
+
class List < Base
|
57
|
+
def initialize(*args)
|
58
|
+
smulti(:parse, /\)/ ) {|s, rest| leave(rest) }
|
59
|
+
smulti(:parse, /\s+/ ) {|s, rest| parse(rest) }
|
60
|
+
smulti(:parse, // ) {|s, rest|
|
61
|
+
item = Main.new(rest)
|
62
|
+
add(item.value)
|
63
|
+
parse(item.unwanted)
|
64
|
+
}
|
65
|
+
super(*args)
|
66
|
+
end
|
67
|
+
|
68
|
+
def value; @list; end
|
69
|
+
end
|
70
|
+
|
71
|
+
class String < Base
|
72
|
+
def initialize(*args)
|
73
|
+
smulti(:parse, '\"') {|s, rest| add_parse(s, rest) }
|
74
|
+
smulti(:parse, '"' ) {|s, rest| leave(rest) }
|
75
|
+
smulti(:parse, /./ ) {|s, rest| add_parse(s, rest) }
|
76
|
+
smulti(:parse, // ) { throw "Couldn't parse string" }
|
77
|
+
super(*args)
|
78
|
+
end
|
79
|
+
|
80
|
+
def value; @list.join(''); end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Symbol < Base
|
84
|
+
def initialize(*args)
|
85
|
+
smulti(:parse, SymbolREMany) {|s, rest| add_leave(s, rest) }
|
86
|
+
super(*args)
|
87
|
+
end
|
88
|
+
|
89
|
+
def value; @list.join('').to_sym; end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Number < Base
|
93
|
+
def initialize(*args)
|
94
|
+
smulti(:parse, NumberREMany) {|s, rest| add_leave(s, rest) }
|
95
|
+
super(*args)
|
96
|
+
end
|
97
|
+
|
98
|
+
def value; @list.join('').to_f; end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class Object; def to_sexp; inspect(); end; end
|
103
|
+
class Symbol; def to_sexp; id2name(); end; end
|
104
|
+
class Array; def to_sexp; "(#{map{|x| x.to_sexp }.join(' ')})"; end; end
|
105
|
+
class String; def parse_sexp; SExpressionParser::Main.new(self).value ; end; end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: sexp
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.1"
|
7
|
+
date: 2006-02-05 00:00:00 -08:00
|
8
|
+
summary: SExpression Reading/Writing for Ruby
|
9
|
+
require_paths:
|
10
|
+
- .
|
11
|
+
email: christophercyll@gmail.com
|
12
|
+
homepage: http://cyll.org/sexp
|
13
|
+
rubyforge_project: sexp
|
14
|
+
description:
|
15
|
+
autorequire: sexp
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Topher Cyll
|
30
|
+
files:
|
31
|
+
- sexp.rb
|
32
|
+
test_files: []
|
33
|
+
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
extra_rdoc_files: []
|
37
|
+
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
requirements: []
|
43
|
+
|
44
|
+
dependencies:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: multi
|
47
|
+
version_requirement:
|
48
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0.1"
|
53
|
+
version:
|