clj 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/clj.rb +2 -2
  2. data/lib/clj/parser.rb +25 -22
  3. data/lib/clj/types.rb +28 -12
  4. metadata +5 -6
data/lib/clj.rb CHANGED
@@ -16,9 +16,9 @@ class Clojure
16
16
  Clojure::Parser.new(*args).parse
17
17
  end
18
18
 
19
- def self.dump (what)
19
+ def self.dump (what, options = {})
20
20
  raise ArgumentError, 'cannot convert the passed value to clojure' unless what.respond_to? :to_clj
21
21
 
22
- what.to_clj
22
+ what.to_clj(options)
23
23
  end
24
24
  end
@@ -13,6 +13,23 @@ require 'strscan'
13
13
  class Clojure
14
14
 
15
15
  class Parser < StringScanner
16
+ UNPARSED = Object.new
17
+
18
+ IGNORE = %r(
19
+ (?:
20
+ //[^\n\r]*[\n\r]| # line comments
21
+ /\* # c-style comments
22
+ (?:
23
+ [^*/]| # normal chars
24
+ /[^*]| # slashes that do not start a nested comment
25
+ \*[^/]| # asterisks that do not end this comment
26
+ /(?=\*/) # single slash before this comment's end
27
+ )*
28
+ \*/ # the End of this comment
29
+ |[ \t\r\n,]+ # whitespaces: space, horicontal tab, lf, cr, and comma
30
+ )
31
+ )mx
32
+
16
33
  STRING = /" ((?:[^\x0-\x1f"\\] |
17
34
  # escaped special characters:
18
35
  \\["\\\/bfnrt] |
@@ -38,6 +55,8 @@ class Parser < StringScanner
38
55
 
39
56
  REGEXP = /#"((\\.|[^"])+)"/
40
57
 
58
+ INSTANT = /#inst#{IGNORE}*"(.*?)"/
59
+
41
60
  VECTOR_OPEN = /\[/
42
61
  VECTOR_CLOSE = /\]/
43
62
 
@@ -54,23 +73,6 @@ class Parser < StringScanner
54
73
  FALSE = /false/
55
74
  NIL = /nil/
56
75
 
57
- IGNORE = %r(
58
- (?:
59
- //[^\n\r]*[\n\r]| # line comments
60
- /\* # c-style comments
61
- (?:
62
- [^*/]| # normal chars
63
- /[^*]| # slashes that do not start a nested comment
64
- \*[^/]| # asterisks that do not end this comment
65
- /(?=\*/) # single slash before this comment's end
66
- )*
67
- \*/ # the End of this comment
68
- |[ \t\r\n,]+ # whitespaces: space, horicontal tab, lf, cr, and comma
69
- )+
70
- )mx
71
-
72
- UNPARSED = Object.new
73
-
74
76
  def initialize (source, options = {})
75
77
  super(source)
76
78
 
@@ -115,6 +117,7 @@ class Parser < StringScanner
115
117
  when scan(FLOAT) then Float(self[1])
116
118
  when scan(INTEGER) then Integer(self[1])
117
119
  when scan(REGEXP) then /#{self[1]}/
120
+ when scan(INSTANT) then DateTime.rfc3339(self[1])
118
121
  when scan(STRING) then parse_string
119
122
  when scan(KEYWORD) then self[1].to_sym
120
123
  when scan(TRUE) then true
@@ -165,7 +168,7 @@ class Parser < StringScanner
165
168
  result << value
166
169
  when scan(VECTOR_CLOSE)
167
170
  break
168
- when skip(IGNORE)
171
+ when skip(/#{IGNORE}+/)
169
172
  ;
170
173
  else
171
174
  raise SyntaxError, 'wat do'
@@ -184,7 +187,7 @@ class Parser < StringScanner
184
187
  result << value
185
188
  when scan(LIST_CLOSE)
186
189
  break
187
- when skip(IGNORE)
190
+ when skip(/#{IGNORE}+/)
188
191
  ;
189
192
  else
190
193
  raise SyntaxError, 'wat do'
@@ -203,7 +206,7 @@ class Parser < StringScanner
203
206
  result << value
204
207
  when scan(SET_CLOSE)
205
208
  break
206
- when skip(IGNORE)
209
+ when skip(/#{IGNORE}+/)
207
210
  ;
208
211
  else
209
212
  raise SyntaxError, 'wat do'
@@ -223,7 +226,7 @@ class Parser < StringScanner
223
226
  until eos?
224
227
  case
225
228
  when (key = parse(false)) != UNPARSED
226
- skip(IGNORE)
229
+ skip(/#{IGNORE}*/)
227
230
 
228
231
  if (value = parse(false)) == UNPARSED
229
232
  raise SyntaxError, 'no value for the hash'
@@ -232,7 +235,7 @@ class Parser < StringScanner
232
235
  result[key] = value
233
236
  when scan(HASH_CLOSE)
234
237
  break
235
- when skip(IGNORE)
238
+ when skip(/#{IGNORE}+/)
236
239
  ;
237
240
  else
238
241
  raise SyntaxError, 'wat do'
@@ -8,50 +8,66 @@
8
8
  # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
9
  #++
10
10
 
11
+ require 'date'
12
+
11
13
  [String, Symbol, Numeric, TrueClass, FalseClass, NilClass].each {|klass|
12
14
  klass.instance_eval {
13
- alias_method :to_clj, :inspect
15
+ define_method :to_clj do |options = {}|
16
+ inspect
17
+ end
14
18
  }
15
19
  }
16
20
 
17
21
  class Rational
18
- alias to_clj to_s
22
+ def to_clj (options = {})
23
+ to_s
24
+ end
19
25
  end
20
26
 
21
27
  class Regexp
22
- def to_clj
28
+ def to_clj (options = {})
23
29
  '#"' + inspect[1 .. -2] + '"'
24
30
  end
25
31
  end
26
32
 
33
+ class DateTime
34
+ def to_clj (options = {})
35
+ if options[:alpha]
36
+ '#inst "' + rfc3339 + '"'
37
+ else
38
+ to_time.to_i.to_s
39
+ end
40
+ end
41
+ end
42
+
27
43
  class Date
28
- def to_clj
29
- to_time.to_clj
44
+ def to_clj (options = {})
45
+ to_datetime.to_clj(options)
30
46
  end
31
47
  end
32
48
 
33
49
  class Time
34
- def to_clj
35
- to_i.to_s
50
+ def to_clj (options = {})
51
+ to_datetime.to_clj(options)
36
52
  end
37
53
  end
38
54
 
39
55
  if defined? BigDecimal
40
56
  class BigDecimal
41
- def to_clj
57
+ def to_clj (options = {})
42
58
  inspect + 'M'
43
59
  end
44
60
  end
45
61
  end
46
62
 
47
63
  class Array
48
- def to_clj
49
- '[' + map { |o| o.to_clj }.join(' ') + ']'
64
+ def to_clj (options = {})
65
+ '[' + map { |o| o.to_clj(options) }.join(' ') + ']'
50
66
  end
51
67
  end
52
68
 
53
69
  class Hash
54
- def to_clj
55
- '{' + map { |k, v| k.to_clj + ' ' + v.to_clj }.join(' ') + '}'
70
+ def to_clj (options = {})
71
+ '{' + map { |k, v| k.to_clj(options) + ' ' + v.to_clj(options) }.join(' ') + '}'
56
72
  end
57
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &15905000 !ruby/object:Gem::Requirement
16
+ requirement: &20555460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *15905000
24
+ version_requirements: *20555460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &15889080 !ruby/object:Gem::Requirement
27
+ requirement: &20554680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *15889080
35
+ version_requirements: *20554680
36
36
  description:
37
37
  email: meh@paranoici.org
38
38
  executables: []
@@ -67,4 +67,3 @@ signing_key:
67
67
  specification_version: 3
68
68
  summary: Like json, but with clojure sexps.
69
69
  test_files: []
70
- has_rdoc: