rison 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.html CHANGED
@@ -21,15 +21,28 @@
21
21
 
22
22
  <h1>rison<span style="color:#F99">.rb</span></h1>
23
23
 
24
+ <h2>Installing</h2>
25
+
26
+ <p><tt>sudo gem install rison</tt> (or <a href="http://rubyforge.org/frs/?group_id=4896">download from RubyForge</a>)</p>
27
+
24
28
  <h2>Examples</h2>
25
29
 
26
30
  <pre><code> require 'rison'
31
+
27
32
 
28
33
  Rison.load('!t') # => true
29
34
 
30
35
  Rison.load('!(1,2,3)') # => [1, 2, 3]
31
36
 
32
- Rison.load('(a:0)') # => {'a' => 0}
37
+ Rison.load('(a:0)') # => {:a => 0}
38
+
39
+
40
+ Rison.dump(true) # => '!t'
41
+
42
+ Rison.dump([1, 2, 3]) # => '!(1,2,3)'
43
+
44
+ Rison.dump({:a => 0}) # => '(a:0)'
45
+
33
46
  </code></pre>
34
47
 
35
48
  <h2>Links</h2>
data/Rakefile.rb CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
5
5
 
6
6
  GEMSPEC = Gem::Specification.new do |s|
7
7
  s.name = 'rison'
8
- s.version = '1.0.0'
8
+ s.version = '1.1.0'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.has_rdoc = false
11
11
  s.summary = 'Pure Ruby parser for Rison (http://mjtemplate.org/examples/rison.html)'
@@ -35,13 +35,23 @@ Rake::TestTask.new do |t|
35
35
  end
36
36
 
37
37
 
38
- task :parse_stdin do
38
+ namespace :stdin do
39
39
  $:.unshift 'lib'
40
-
41
40
  require 'rison'
42
41
  require 'pp'
43
-
44
- pp Rison.load($stdin.read.chomp)
42
+
43
+ task :lex do
44
+ pp Rison.lex($stdin.read.chomp).to_a
45
+ end
46
+ task :parse do
47
+ parsed = Rison.parse(Rison.lex($stdin.read.chomp))
48
+
49
+ if parsed.has_error?
50
+ p parsed
51
+ else
52
+ pp Rison.load(parsed)
53
+ end
54
+ end
45
55
  end
46
56
 
47
57
 
data/lib/rison/dump.rb ADDED
@@ -0,0 +1,47 @@
1
+ module Rison
2
+ def self.dump(object)
3
+ case object
4
+ when NilClass then '!n'
5
+
6
+ when TrueClass then '!t'
7
+
8
+ when FalseClass then '!f'
9
+
10
+ when Symbol then dump(object.to_s)
11
+
12
+ when Rational then object.to_f.to_s
13
+
14
+ when Numeric then object.to_s
15
+
16
+ when String
17
+ if object.empty?
18
+ "''"
19
+ elsif id?(object)
20
+ object
21
+ else
22
+ quote(object)
23
+ end
24
+
25
+ when Hash
26
+ '(%s)' % (object.sort_by { |k, v| k.to_s }.map { |(k, v)| '%s:%s' % [ dump(k), dump(v) ] } * ',')
27
+
28
+ when Array
29
+ '!(%s)' % (object.map { |x| dump(x) } * ',')
30
+
31
+ else
32
+ raise ArgumentError, 'cannot serialize: %p' % object
33
+ end
34
+ end
35
+
36
+ def self.quote(string)
37
+ "'%s'" % escape(string)
38
+ end
39
+
40
+ def self.escape(string)
41
+ string.gsub('!', '!!').gsub("'", "!'")
42
+ end
43
+
44
+ def self.id?(string)
45
+ string !~ /^(-|\d)/ && string !~ /['!:(),*@$ ]/
46
+ end
47
+ end
@@ -16,9 +16,7 @@ module Rison
16
16
  evaluate(child_nodes[1])
17
17
  end
18
18
 
19
- for_member_pair do
20
- evaluate(child_nodes[0])
21
- end
19
+ # for_member_pair
22
20
 
23
21
  for_member_pairs do
24
22
  evaluate(child_nodes[2]).merge(evaluate(child_nodes[0]))
@@ -70,10 +68,12 @@ module Rison
70
68
  nil
71
69
  end
72
70
 
73
- # for_id_idstart
71
+ for_id_idstart do
72
+ evaluate(child_nodes[0]).to_sym
73
+ end
74
74
 
75
75
  for_id_idstart_idchars do
76
- evaluate(child_nodes[0]) + evaluate(child_nodes[1])
76
+ (evaluate(child_nodes[0]) + evaluate(child_nodes[1])).to_sym
77
77
  end
78
78
 
79
79
  # for_idchars_idchar
@@ -89,6 +89,8 @@ module Rison
89
89
  for_idchar_char do
90
90
  child_nodes[0].token.value
91
91
  end
92
+
93
+ # for_idchar_idstart
92
94
 
93
95
  for_idstart_char do
94
96
  child_nodes[0].token.value
@@ -122,13 +124,13 @@ module Rison
122
124
  "'"
123
125
  end
124
126
 
127
+ # for_strchar_idchar
128
+
125
129
  for_strchar_num do
126
130
  evaluate(child_nodes[0]).to_s
127
131
  end
128
132
 
129
- for_number_int do
130
- evaluate(child_nodes[0])
131
- end
133
+ # for_number_int
132
134
 
133
135
  for_number_int_frac do
134
136
  evaluate(child_nodes[0]) + evaluate(child_nodes[1])
data/lib/rison/grammar.rb CHANGED
@@ -60,12 +60,13 @@ module Rison
60
60
 
61
61
  for_symbol 'idchar' do
62
62
  # any Unicode character not in '!:(),*@$
63
- idchar_char %w| char_token |
63
+ idchar_char %w| idchar_safe_token |
64
+ idchar_idstart %w| idstart |
64
65
  end
65
66
 
66
67
  for_symbol 'idstart' do
67
68
  # any Unicode character not in -, digit, or idchar
68
- idstart_char %w| char_token |
69
+ idstart_char %w| idstart_safe_token |
69
70
  end
70
71
 
71
72
  for_symbol 'string' do
@@ -85,6 +86,7 @@ module Rison
85
86
  strchar_char %w| char_token |
86
87
  strchar_quoted_exclamation %w| !! |
87
88
  strchar_quoted_single_quote %w| !' |
89
+ strchar_idchar %w| idchar |
88
90
  strchar_num %w| number |
89
91
  end
90
92
 
data/lib/rison/lexer.rb CHANGED
@@ -20,7 +20,10 @@ module Rison
20
20
  ['e-?\d+', 'exponent_token'],
21
21
  ['\.\d+', 'frac_token'],
22
22
  ['-?\d+', 'integer_token'],
23
- ['.', 'char_token']
23
+
24
+ ["[^\\-0-9'!:(),*@$ ]", 'idstart_safe_token'],
25
+ ["[^'!:(),*@$ ]", 'idchar_safe_token'],
26
+ ["[^'!]", 'char_token']
24
27
 
25
28
  ].each do |(pattern, token)|
26
29
  for_pattern(pattern) { create_token(token || pattern) }
data/lib/rison/parser.rb CHANGED
@@ -8,332 +8,352 @@ module Rison
8
8
 
9
9
  start_with 0
10
10
 
11
- at_state(44) {
12
- for_symbols(",", "_End_", ")") { reduce_with "value_false" }
11
+ at_state(48) {
12
+ for_symbols("'") { shift_to 2 }
13
+ for_symbols("object") { shift_to 25 }
14
+ for_symbols("value") { shift_to 50 }
15
+ for_symbols("number") { shift_to 46 }
16
+ for_symbols("!") { shift_to 47 }
17
+ for_symbols("string") { shift_to 26 }
18
+ for_symbols("array") { shift_to 28 }
19
+ for_symbols("idstart") { shift_to 38 }
20
+ for_symbols("integer_token") { shift_to 5 }
21
+ for_symbols("!t") { shift_to 24 }
22
+ for_symbols(")") { shift_to 49 }
23
+ for_symbols("idstart_safe_token") { shift_to 14 }
24
+ for_symbols("!f") { shift_to 27 }
25
+ for_symbols("(") { shift_to 29 }
26
+ for_symbols("id") { shift_to 53 }
27
+ for_symbols("int") { shift_to 8 }
28
+ for_symbols("!n") { shift_to 1 }
29
+ for_symbols("elements") { shift_to 54 }
13
30
  }
14
31
 
15
- at_state(31) {
16
- for_symbols("integer_token") { shift_to 6 }
17
- for_symbols("!") { shift_to 11 }
18
- for_symbols("!t") { shift_to 33 }
19
- for_symbols("array") { shift_to 10 }
20
- for_symbols("number") { shift_to 45 }
21
- for_symbols("'") { shift_to 34 }
22
- for_symbols("string") { shift_to 9 }
23
- for_symbols("object") { shift_to 46 }
24
- for_symbols("id") { shift_to 15 }
25
- for_symbols("!f") { shift_to 44 }
26
- for_symbols("!n") { shift_to 1 }
27
- for_symbols("char_token") { shift_to 16 }
28
- for_symbols("idstart") { shift_to 2 }
29
- for_symbols("int") { shift_to 18 }
30
- for_symbols("value") { shift_to 32 }
31
- for_symbols("(") { shift_to 24 }
32
+ at_state(47) {
33
+ for_symbols("(") { shift_to 48 }
34
+ }
35
+
36
+ at_state(22) {
37
+ for_symbols(",", ":", "_End_", ")") { reduce_with "non_empty_string" }
32
38
  }
33
39
 
34
40
  at_state(19) {
35
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", ")", "char_token") { reduce_with "exponent_literal" }
41
+ for_symbols("!!", "idstart_safe_token", "integer_token", "!'", "'", "char_token", "idchar_safe_token") { reduce_with "strchar_num" }
36
42
  }
37
43
 
38
- at_state(7) {
39
- for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_idchars" }
44
+ at_state(5) {
45
+ for_symbols("!!", ",", "frac_token", "idstart_safe_token", "integer_token", ":", "!'", "_End_", "'", "exponent_token", "char_token", ")", "idchar_safe_token") { reduce_with "integer_literal" }
40
46
  }
41
47
 
42
- at_state(3) {
43
- for_symbols("idchar") { shift_to 3 }
44
- for_symbols("integer_token") { shift_to 6 }
45
- for_symbols("idchars") { shift_to 7 }
46
- for_symbols("int") { shift_to 5 }
47
- for_symbols("char_token") { shift_to 4 }
48
- for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_idchar" }
48
+ at_state(54) {
49
+ for_symbols(")") { shift_to 55 }
49
50
  }
50
51
 
51
- at_state(50) {
52
- for_symbols(",", "_End_", ")") { reduce_with "empty_array" }
52
+ at_state(45) {
53
+ for_symbols(",", ")") { reduce_with "pair_key_value" }
53
54
  }
54
55
 
55
- at_state(49) {
56
- for_symbols(",", "_End_", ")") { reduce_with "empty_object" }
56
+ at_state(41) {
57
+ for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_idchars" }
57
58
  }
58
59
 
59
- at_state(48) {
60
+ at_state(31) {
60
61
  for_symbols(",", "_End_", ")") { reduce_with "non_empty_object" }
61
62
  }
62
63
 
63
- at_state(45) {
64
- for_symbols(",", "_End_", ")") { reduce_with "value_number" }
64
+ at_state(8) {
65
+ for_symbols("exp") { shift_to 9 }
66
+ for_symbols("exponent_token") { shift_to 13 }
67
+ for_symbols("frac") { shift_to 11 }
68
+ for_symbols("frac_token") { shift_to 10 }
69
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "number_int" }
65
70
  }
66
71
 
67
- at_state(40) {
68
- for_symbols("!!", "integer_token", "!'", "'", "char_token") { reduce_with "strchar_num" }
72
+ at_state(7) {
73
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", ":", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "idchar_char" }
69
74
  }
70
75
 
71
- at_state(27) {
72
- for_symbols("id") { shift_to 29 }
73
- for_symbols("key") { shift_to 30 }
74
- for_symbols("'") { shift_to 34 }
75
- for_symbols("members") { shift_to 28 }
76
- for_symbols("char_token") { shift_to 16 }
77
- for_symbols("pair") { shift_to 26 }
78
- for_symbols("idstart") { shift_to 2 }
79
- for_symbols("string") { shift_to 25 }
76
+ at_state(53) {
77
+ for_symbols(",", "_End_", ")") { reduce_with "value_id" }
80
78
  }
81
79
 
82
- at_state(24) {
83
- for_symbols(")") { shift_to 49 }
84
- for_symbols("id") { shift_to 29 }
85
- for_symbols("members") { shift_to 47 }
86
- for_symbols("key") { shift_to 30 }
87
- for_symbols("'") { shift_to 34 }
88
- for_symbols("char_token") { shift_to 16 }
89
- for_symbols("pair") { shift_to 26 }
90
- for_symbols("idstart") { shift_to 2 }
91
- for_symbols("string") { shift_to 25 }
80
+ at_state(21) {
81
+ for_symbols("'") { shift_to 22 }
92
82
  }
93
83
 
94
- at_state(18) {
95
- for_symbols("exponent_token") { shift_to 19 }
96
- for_symbols("exp") { shift_to 23 }
97
- for_symbols("frac") { shift_to 21 }
98
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", "char_token", ")") { reduce_with "number_int" }
99
- for_symbols("frac_token") { shift_to 20 }
84
+ at_state(55) {
85
+ for_symbols(",", "_End_", ")") { reduce_with "non_empty_array" }
100
86
  }
101
87
 
102
- at_state(8) {
103
- for_symbols(",", ":", "_End_", ")") { reduce_with "id_idstart_idchars" }
88
+ at_state(52) {
89
+ for_symbols(")") { reduce_with "element_values" }
104
90
  }
105
91
 
106
- at_state(4) {
107
- for_symbols(",", "integer_token", ":", "_End_", "char_token", ")") { reduce_with "idchar_char" }
92
+ at_state(44) {
93
+ for_symbols("'") { shift_to 2 }
94
+ for_symbols("object") { shift_to 25 }
95
+ for_symbols("number") { shift_to 46 }
96
+ for_symbols("!") { shift_to 47 }
97
+ for_symbols("string") { shift_to 26 }
98
+ for_symbols("array") { shift_to 28 }
99
+ for_symbols("idstart") { shift_to 38 }
100
+ for_symbols("value") { shift_to 45 }
101
+ for_symbols("integer_token") { shift_to 5 }
102
+ for_symbols("!t") { shift_to 24 }
103
+ for_symbols("idstart_safe_token") { shift_to 14 }
104
+ for_symbols("!f") { shift_to 27 }
105
+ for_symbols("(") { shift_to 29 }
106
+ for_symbols("id") { shift_to 53 }
107
+ for_symbols("int") { shift_to 8 }
108
+ for_symbols("!n") { shift_to 1 }
108
109
  }
109
110
 
110
- at_state(32) {
111
- for_symbols(",", ")") { reduce_with "pair_key_value" }
111
+ at_state(42) {
112
+ for_symbols(",", ":", "_End_", ")") { reduce_with "id_idstart_idchars" }
112
113
  }
113
114
 
114
- at_state(14) {
115
- for_symbols("value") { shift_to 13 }
116
- for_symbols("integer_token") { shift_to 6 }
117
- for_symbols("!") { shift_to 11 }
118
- for_symbols("!t") { shift_to 33 }
119
- for_symbols("array") { shift_to 10 }
120
- for_symbols("number") { shift_to 45 }
121
- for_symbols("'") { shift_to 34 }
122
- for_symbols("string") { shift_to 9 }
123
- for_symbols("object") { shift_to 46 }
124
- for_symbols("id") { shift_to 15 }
125
- for_symbols("!f") { shift_to 44 }
126
- for_symbols("!n") { shift_to 1 }
127
- for_symbols("char_token") { shift_to 16 }
128
- for_symbols("idstart") { shift_to 2 }
129
- for_symbols("int") { shift_to 18 }
130
- for_symbols("elements") { shift_to 17 }
131
- for_symbols("(") { shift_to 24 }
115
+ at_state(36) {
116
+ for_symbols("members") { shift_to 37 }
117
+ for_symbols("'") { shift_to 2 }
118
+ for_symbols("key") { shift_to 43 }
119
+ for_symbols("idstart") { shift_to 38 }
120
+ for_symbols("string") { shift_to 33 }
121
+ for_symbols("idstart_safe_token") { shift_to 14 }
122
+ for_symbols("pair") { shift_to 35 }
123
+ for_symbols("id") { shift_to 34 }
132
124
  }
133
125
 
134
- at_state(2) {
135
- for_symbols("idchar") { shift_to 3 }
136
- for_symbols("integer_token") { shift_to 6 }
137
- for_symbols("int") { shift_to 5 }
138
- for_symbols(",", ":", "_End_", ")") { reduce_with "id_idstart" }
139
- for_symbols("char_token") { shift_to 4 }
140
- for_symbols("idchars") { shift_to 8 }
126
+ at_state(3) {
127
+ for_symbols(",", ":", "_End_", ")") { reduce_with "empty_string" }
141
128
  }
142
129
 
143
- at_state(47) {
144
- for_symbols(")") { shift_to 48 }
130
+ at_state(39) {
131
+ for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_int" }
145
132
  }
146
133
 
147
134
  at_state(35) {
148
- for_symbols("!!", "integer_token", "!'", "'", "char_token") { reduce_with "strchar_quoted_single_quote" }
149
- }
150
-
151
- at_state(29) {
152
- for_symbols(":") { reduce_with "key_id" }
135
+ for_symbols(",") { shift_to 36 }
136
+ for_symbols(")") { reduce_with "member_pair" }
153
137
  }
154
138
 
155
- at_state(21) {
156
- for_symbols("exp") { shift_to 22 }
157
- for_symbols("exponent_token") { shift_to 19 }
158
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", "char_token", ")") { reduce_with "number_int_frac" }
139
+ at_state(27) {
140
+ for_symbols(",", "_End_", ")") { reduce_with "value_false" }
159
141
  }
160
142
 
161
- at_state(16) {
162
- for_symbols(",", "integer_token", ":", "_End_", "char_token", ")") { reduce_with "idstart_char" }
143
+ at_state(18) {
144
+ for_symbols("!!", "idstart_safe_token", "integer_token", "!'", "'", "char_token", "idchar_safe_token") { reduce_with "strchar_quoted_exclamation" }
163
145
  }
164
146
 
165
- at_state(10) {
166
- for_symbols(",", "_End_", ")") { reduce_with "value_array" }
147
+ at_state(11) {
148
+ for_symbols("exp") { shift_to 12 }
149
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "number_int_frac" }
150
+ for_symbols("exponent_token") { shift_to 13 }
167
151
  }
168
152
 
169
- at_state(9) {
170
- for_symbols(",", "_End_", ")") { reduce_with "value_string" }
153
+ at_state(51) {
154
+ for_symbols("'") { shift_to 2 }
155
+ for_symbols("object") { shift_to 25 }
156
+ for_symbols("value") { shift_to 50 }
157
+ for_symbols("elements") { shift_to 52 }
158
+ for_symbols("number") { shift_to 46 }
159
+ for_symbols("!") { shift_to 47 }
160
+ for_symbols("string") { shift_to 26 }
161
+ for_symbols("array") { shift_to 28 }
162
+ for_symbols("idstart") { shift_to 38 }
163
+ for_symbols("integer_token") { shift_to 5 }
164
+ for_symbols("!t") { shift_to 24 }
165
+ for_symbols("idstart_safe_token") { shift_to 14 }
166
+ for_symbols("!f") { shift_to 27 }
167
+ for_symbols("(") { shift_to 29 }
168
+ for_symbols("id") { shift_to 53 }
169
+ for_symbols("int") { shift_to 8 }
170
+ for_symbols("!n") { shift_to 1 }
171
171
  }
172
172
 
173
- at_state(17) {
174
- for_symbols(")") { reduce_with "element_values" }
173
+ at_state(50) {
174
+ for_symbols(",") { shift_to 51 }
175
+ for_symbols(")") { reduce_with "element_value" }
175
176
  }
176
177
 
177
- at_state(0) {
178
- for_symbols("integer_token") { shift_to 6 }
179
- for_symbols("!") { shift_to 11 }
180
- for_symbols("!t") { shift_to 33 }
181
- for_symbols("array") { shift_to 10 }
182
- for_symbols("number") { shift_to 45 }
183
- for_symbols("'") { shift_to 34 }
184
- for_symbols("string") { shift_to 9 }
185
- for_symbols("object") { shift_to 46 }
186
- for_symbols("id") { shift_to 15 }
187
- for_symbols("!f") { shift_to 44 }
188
- for_symbols("!n") { shift_to 1 }
189
- for_symbols("char_token") { shift_to 16 }
190
- for_symbols("idstart") { shift_to 2 }
191
- for_symbols("int") { shift_to 18 }
192
- for_symbols("(") { shift_to 24 }
193
- for_symbols("value") { shift_to 53 }
178
+ at_state(43) {
179
+ for_symbols(":") { shift_to 44 }
194
180
  }
195
181
 
196
182
  at_state(38) {
197
- for_symbols("'") { reduce_with "strchars_chars" }
183
+ for_symbols("idchars") { shift_to 42 }
184
+ for_symbols(",", ":", "_End_", ")") { reduce_with "id_idstart" }
185
+ for_symbols("idstart") { shift_to 17 }
186
+ for_symbols("integer_token") { shift_to 5 }
187
+ for_symbols("idstart_safe_token") { shift_to 14 }
188
+ for_symbols("idchar") { shift_to 40 }
189
+ for_symbols("idchar_safe_token") { shift_to 7 }
190
+ for_symbols("int") { shift_to 39 }
198
191
  }
199
192
 
200
- at_state(34) {
201
- for_symbols("integer_token") { shift_to 6 }
202
- for_symbols("char_token") { shift_to 39 }
203
- for_symbols("strchar") { shift_to 36 }
204
- for_symbols("number") { shift_to 40 }
205
- for_symbols("!'") { shift_to 35 }
206
- for_symbols("'") { shift_to 43 }
207
- for_symbols("int") { shift_to 18 }
208
- for_symbols("!!") { shift_to 37 }
209
- for_symbols("strchars") { shift_to 41 }
193
+ at_state(37) {
194
+ for_symbols(")") { reduce_with "member_pairs" }
210
195
  }
211
196
 
212
- at_state(26) {
213
- for_symbols(")") { reduce_with "member_pair" }
214
- for_symbols(",") { shift_to 27 }
197
+ at_state(15) {
198
+ for_symbols("!!", "idstart_safe_token", "integer_token", "!'", "'", "char_token", "idchar_safe_token") { reduce_with "strchar_idchar" }
215
199
  }
216
200
 
217
- at_state(5) {
218
- for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_int" }
201
+ at_state(12) {
202
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "number_int_frac_exp" }
219
203
  }
220
204
 
221
- at_state(53) {
222
- for_symbols("_End_") { reduce_with "start" }
205
+ at_state(40) {
206
+ for_symbols(",", ":", "_End_", ")") { reduce_with "idchars_idchar" }
207
+ for_symbols("idstart") { shift_to 17 }
208
+ for_symbols("integer_token") { shift_to 5 }
209
+ for_symbols("idchars") { shift_to 41 }
210
+ for_symbols("idstart_safe_token") { shift_to 14 }
211
+ for_symbols("idchar") { shift_to 40 }
212
+ for_symbols("idchar_safe_token") { shift_to 7 }
213
+ for_symbols("int") { shift_to 39 }
223
214
  }
224
215
 
225
- at_state(52) {
226
- for_symbols(",", "_End_", ")") { reduce_with "non_empty_array" }
216
+ at_state(34) {
217
+ for_symbols(":") { reduce_with "key_id" }
227
218
  }
228
219
 
229
- at_state(46) {
230
- for_symbols(",", "_End_", ")") { reduce_with "value_object" }
220
+ at_state(32) {
221
+ for_symbols(",", "_End_", ")") { reduce_with "empty_object" }
231
222
  }
232
223
 
233
- at_state(41) {
234
- for_symbols("'") { shift_to 42 }
224
+ at_state(23) {
225
+ for_symbols("_End_") { reduce_with "start" }
235
226
  }
236
227
 
237
- at_state(37) {
238
- for_symbols("!!", "integer_token", "!'", "'", "char_token") { reduce_with "strchar_quoted_exclamation" }
228
+ at_state(17) {
229
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", ":", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "idchar_idstart" }
239
230
  }
240
231
 
241
- at_state(25) {
242
- for_symbols(":") { reduce_with "key_string" }
232
+ at_state(16) {
233
+ for_symbols("idstart") { shift_to 17 }
234
+ for_symbols("!'") { shift_to 6 }
235
+ for_symbols("strchars") { shift_to 20 }
236
+ for_symbols("'") { reduce_with "strchars_char" }
237
+ for_symbols("integer_token") { shift_to 5 }
238
+ for_symbols("idchar") { shift_to 15 }
239
+ for_symbols("char_token") { shift_to 4 }
240
+ for_symbols("idstart_safe_token") { shift_to 14 }
241
+ for_symbols("strchar") { shift_to 16 }
242
+ for_symbols("idchar_safe_token") { shift_to 7 }
243
+ for_symbols("int") { shift_to 8 }
244
+ for_symbols("number") { shift_to 19 }
245
+ for_symbols("!!") { shift_to 18 }
243
246
  }
244
247
 
245
- at_state(22) {
246
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", "char_token", ")") { reduce_with "number_int_frac_exp" }
248
+ at_state(4) {
249
+ for_symbols("!!", "idstart_safe_token", "integer_token", "!'", "'", "char_token", "idchar_safe_token") { reduce_with "strchar_char" }
247
250
  }
248
251
 
249
- at_state(13) {
250
- for_symbols(")") { reduce_with "element_value" }
251
- for_symbols(",") { shift_to 14 }
252
+ at_state(49) {
253
+ for_symbols(",", "_End_", ")") { reduce_with "empty_array" }
252
254
  }
253
255
 
254
- at_state(51) {
255
- for_symbols(")") { shift_to 52 }
256
+ at_state(25) {
257
+ for_symbols(",", "_End_", ")") { reduce_with "value_object" }
256
258
  }
257
259
 
258
- at_state(43) {
259
- for_symbols(",", ":", "_End_", ")") { reduce_with "empty_string" }
260
+ at_state(10) {
261
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "exponent_token", "char_token", ")", "idchar_safe_token") { reduce_with "frac_literal" }
260
262
  }
261
263
 
262
- at_state(42) {
263
- for_symbols(",", ":", "_End_", ")") { reduce_with "non_empty_string" }
264
+ at_state(28) {
265
+ for_symbols(",", "_End_", ")") { reduce_with "value_array" }
264
266
  }
265
267
 
266
- at_state(39) {
267
- for_symbols("!!", "integer_token", "!'", "'", "char_token") { reduce_with "strchar_char" }
268
+ at_state(26) {
269
+ for_symbols(",", "_End_", ")") { reduce_with "value_string" }
268
270
  }
269
271
 
270
- at_state(33) {
271
- for_symbols(",", "_End_", ")") { reduce_with "value_true" }
272
+ at_state(0) {
273
+ for_symbols("'") { shift_to 2 }
274
+ for_symbols("object") { shift_to 25 }
275
+ for_symbols("value") { shift_to 23 }
276
+ for_symbols("number") { shift_to 46 }
277
+ for_symbols("!") { shift_to 47 }
278
+ for_symbols("string") { shift_to 26 }
279
+ for_symbols("array") { shift_to 28 }
280
+ for_symbols("idstart") { shift_to 38 }
281
+ for_symbols("integer_token") { shift_to 5 }
282
+ for_symbols("!t") { shift_to 24 }
283
+ for_symbols("idstart_safe_token") { shift_to 14 }
284
+ for_symbols("!f") { shift_to 27 }
285
+ for_symbols("(") { shift_to 29 }
286
+ for_symbols("id") { shift_to 53 }
287
+ for_symbols("int") { shift_to 8 }
288
+ for_symbols("!n") { shift_to 1 }
272
289
  }
273
290
 
274
- at_state(36) {
275
- for_symbols("integer_token") { shift_to 6 }
276
- for_symbols("char_token") { shift_to 39 }
277
- for_symbols("'") { reduce_with "strchars_char" }
278
- for_symbols("strchar") { shift_to 36 }
279
- for_symbols("number") { shift_to 40 }
280
- for_symbols("!'") { shift_to 35 }
281
- for_symbols("int") { shift_to 18 }
282
- for_symbols("!!") { shift_to 37 }
283
- for_symbols("strchars") { shift_to 38 }
291
+ at_state(46) {
292
+ for_symbols(",", "_End_", ")") { reduce_with "value_number" }
284
293
  }
285
294
 
286
- at_state(30) {
287
- for_symbols(":") { shift_to 31 }
295
+ at_state(24) {
296
+ for_symbols(",", "_End_", ")") { reduce_with "value_true" }
288
297
  }
289
298
 
290
- at_state(12) {
291
- for_symbols("value") { shift_to 13 }
292
- for_symbols("elements") { shift_to 51 }
293
- for_symbols("integer_token") { shift_to 6 }
294
- for_symbols("!") { shift_to 11 }
295
- for_symbols("!t") { shift_to 33 }
296
- for_symbols("array") { shift_to 10 }
297
- for_symbols("number") { shift_to 45 }
298
- for_symbols("'") { shift_to 34 }
299
- for_symbols("string") { shift_to 9 }
300
- for_symbols("object") { shift_to 46 }
301
- for_symbols("id") { shift_to 15 }
302
- for_symbols("!f") { shift_to 44 }
303
- for_symbols("!n") { shift_to 1 }
304
- for_symbols("char_token") { shift_to 16 }
305
- for_symbols("idstart") { shift_to 2 }
306
- for_symbols("int") { shift_to 18 }
307
- for_symbols(")") { shift_to 50 }
308
- for_symbols("(") { shift_to 24 }
299
+ at_state(20) {
300
+ for_symbols("'") { reduce_with "strchars_chars" }
309
301
  }
310
302
 
311
- at_state(11) {
312
- for_symbols("(") { shift_to 12 }
303
+ at_state(13) {
304
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "exponent_literal" }
313
305
  }
314
306
 
315
- at_state(6) {
316
- for_symbols("!!", "frac_token", ",", "integer_token", ":", "!'", "'", "_End_", "char_token", ")", "exponent_token") { reduce_with "integer_literal" }
307
+ at_state(9) {
308
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "number_int_exp" }
317
309
  }
318
310
 
319
311
  at_state(1) {
320
312
  for_symbols(",", "_End_", ")") { reduce_with "value_null" }
321
313
  }
322
314
 
323
- at_state(28) {
324
- for_symbols(")") { reduce_with "member_pairs" }
315
+ at_state(33) {
316
+ for_symbols(":") { reduce_with "key_string" }
325
317
  }
326
318
 
327
- at_state(23) {
328
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", ")", "char_token") { reduce_with "number_int_exp" }
319
+ at_state(30) {
320
+ for_symbols(")") { shift_to 31 }
329
321
  }
330
322
 
331
- at_state(20) {
332
- for_symbols("!!", ",", "integer_token", "!'", "'", "_End_", "char_token", ")", "exponent_token") { reduce_with "frac_literal" }
323
+ at_state(29) {
324
+ for_symbols("'") { shift_to 2 }
325
+ for_symbols("members") { shift_to 30 }
326
+ for_symbols("key") { shift_to 43 }
327
+ for_symbols("idstart") { shift_to 38 }
328
+ for_symbols("string") { shift_to 33 }
329
+ for_symbols("idstart_safe_token") { shift_to 14 }
330
+ for_symbols(")") { shift_to 32 }
331
+ for_symbols("pair") { shift_to 35 }
332
+ for_symbols("id") { shift_to 34 }
333
333
  }
334
334
 
335
- at_state(15) {
336
- for_symbols(",", "_End_", ")") { reduce_with "value_id" }
335
+ at_state(14) {
336
+ for_symbols("!!", ",", "idstart_safe_token", "integer_token", ":", "!'", "_End_", "'", "char_token", ")", "idchar_safe_token") { reduce_with "idstart_char" }
337
+ }
338
+
339
+ at_state(6) {
340
+ for_symbols("!!", "idstart_safe_token", "integer_token", "!'", "'", "char_token", "idchar_safe_token") { reduce_with "strchar_quoted_single_quote" }
341
+ }
342
+
343
+ at_state(2) {
344
+ for_symbols("'") { shift_to 3 }
345
+ for_symbols("idstart") { shift_to 17 }
346
+ for_symbols("!'") { shift_to 6 }
347
+ for_symbols("integer_token") { shift_to 5 }
348
+ for_symbols("idchar") { shift_to 15 }
349
+ for_symbols("char_token") { shift_to 4 }
350
+ for_symbols("idstart_safe_token") { shift_to 14 }
351
+ for_symbols("strchar") { shift_to 16 }
352
+ for_symbols("idchar_safe_token") { shift_to 7 }
353
+ for_symbols("int") { shift_to 8 }
354
+ for_symbols("strchars") { shift_to 21 }
355
+ for_symbols("number") { shift_to 19 }
356
+ for_symbols("!!") { shift_to 18 }
337
357
  }
338
358
 
339
359
  end
data/lib/rison.rb CHANGED
@@ -2,6 +2,7 @@ require 'dhaka'
2
2
  require 'rison/lexer'
3
3
  require 'rison/parser'
4
4
  require 'rison/evaluator'
5
+ require 'rison/dump'
5
6
 
6
7
  module Rison
7
8
 
data/test/test_dump.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'test/unit'
2
+ require 'rison'
3
+ require 'rison/dump'
4
+
5
+ class RisonDumpTests < Test::Unit::TestCase
6
+ def r(object)
7
+ Rison.dump(object)
8
+ end
9
+
10
+ def test_true
11
+ assert_equal '!t', r(true)
12
+ end
13
+
14
+ def test_false
15
+ assert_equal '!f', r(false)
16
+ end
17
+
18
+ def test_nil
19
+ assert_equal '!n', r(nil)
20
+ end
21
+
22
+ def test_integers
23
+ assert_equal '0', r(0)
24
+ assert_equal '42', r(42)
25
+ assert_equal '-42', r(-42)
26
+ end
27
+
28
+ def test_fractions
29
+ assert_equal '1.5', r(Rational(3, 2))
30
+ assert_equal '99.99', r(Rational(9999, 100))
31
+ end
32
+
33
+ def test_ids
34
+ ['a', 'a-z', 'domain.com'].each { |s| assert_equal s, r(s) }
35
+ end
36
+
37
+ def test_strings
38
+ assert_equal "''", r('')
39
+ assert_equal "'0a'", r('0a')
40
+ assert_equal "'-h'", r('-h')
41
+ assert_equal "'can!'t'", r("can't")
42
+ assert_equal "'wow!!'", r('wow!')
43
+ assert_equal "'abc def'", r('abc def')
44
+ assert_equal "'user@domain.com'", r('user@domain.com')
45
+ assert_equal "'US $10'", r('US $10')
46
+ end
47
+
48
+ def test_empty_object
49
+ assert_equal '()', r({})
50
+ end
51
+
52
+ def test_non_empty_objects
53
+ assert_equal '(a:0)', r({:a => 0})
54
+ assert_equal '(id:!n,type:/common/document)', r({'id' => nil, 'type' => '/common/document'})
55
+ assert_equal '(id:!n,type:/common/document)', r({:id => nil, :type => :'/common/document'})
56
+ end
57
+
58
+ def test_empty_array
59
+ assert_equal '!()', r([])
60
+ end
61
+
62
+ def test_non_empty_arrays
63
+ assert_equal %{!(!t,!f,!n,'')}, r([true, false, nil, ''])
64
+ end
65
+ end
@@ -1,18 +1,18 @@
1
1
  require 'test/unit'
2
2
  require 'rison'
3
3
 
4
- class RisonTests < Test::Unit::TestCase
4
+ class RisonParserTests < Test::Unit::TestCase
5
5
 
6
6
  def rison(string)
7
7
  parsed = Rison.parse(Rison.lex(string))
8
8
 
9
- if parsed.has_error?
10
- p parsed
11
- end
12
-
13
- Rison.evaluate(parsed)
9
+ parsed.has_error?? parsed : Rison.evaluate(parsed)
14
10
  end
15
11
 
12
+ def assert_invalid(string)
13
+ rison(string).is_a?(Dhaka::ParseErrorResult)
14
+ end
15
+
16
16
  # cf. http://mjtemplate.org/examples/rison.html
17
17
 
18
18
  def test_true
@@ -27,10 +27,6 @@ class RisonTests < Test::Unit::TestCase
27
27
  assert_equal nil, rison('!n')
28
28
  end
29
29
 
30
- def test_empty_string
31
- assert_equal '', rison(%{''})
32
- end
33
-
34
30
  def test_zero
35
31
  assert_equal 0, rison('0')
36
32
  end
@@ -61,28 +57,31 @@ class RisonTests < Test::Unit::TestCase
61
57
  def test_fraction_and_exponent
62
58
  assert_equal 150, rison('1.5e2')
63
59
  end
64
-
65
- def test_single_character
66
- assert_equal 'a', rison('a')
60
+
61
+ def test_ids
62
+ %w( a a-z domain.com ).each { |s| assert_equal s.to_sym, rison(s) }
67
63
  end
68
64
 
69
- def test_string_with_leading_digit
65
+ def test_strings
66
+ assert_equal '', rison("''")
70
67
  assert_equal '0a', rison("'0a'")
68
+ assert_equal '-h', rison("'-h'")
69
+ assert_equal "can't", rison("'can!'t'")
70
+ assert_equal 'wow!', rison("'wow!!'")
71
+ assert_equal 'abc def', rison("'abc def'")
72
+ assert_equal 'user@domain.com', rison("'user@domain.com'")
73
+ assert_equal 'US $10', rison("'US $10'")
71
74
  end
72
-
73
- def test_unquoted_string
74
- assert_equal 'abc def', rison('abc def')
75
- end
76
-
75
+
77
76
  def test_empty_object
78
77
  assert_equal Hash.new, rison('()')
79
78
  end
80
79
 
81
80
  def test_non_empty_objects
82
- expected = {'a' => 0}
81
+ expected = {:a => 0}
83
82
  assert_equal expected, rison('(a:0)')
84
83
 
85
- expected = {'id' => nil, 'type' => '/common/document'}
84
+ expected = {:id => nil, :type => :'/common/document'}
86
85
  assert_equal expected, rison('(id:!n,type:/common/document)')
87
86
  end
88
87
 
@@ -94,29 +93,8 @@ class RisonTests < Test::Unit::TestCase
94
93
  assert_equal [ true, false, nil, '' ], rison(%{!(!t,!f,!n,'')})
95
94
  end
96
95
 
97
- def test_unquoted_string_with_leading_minus
98
- assert_equal '-h', rison('-h')
96
+ def test_invalid_expressions
97
+ assert_invalid 'abc def'
99
98
  end
100
99
 
101
- def test_unquoted_string_with_minus
102
- assert_equal 'a-z', rison('a-z')
103
- end
104
-
105
- def test_domain_name
106
- assert_equal 'domain.com', rison('domain.com')
107
- end
108
-
109
- def test_email
110
- assert_equal 'user@domain.com', rison('user@domain.com')
111
- end
112
-
113
- def test_money
114
- assert_equal 'US $10', rison('US $10')
115
- end
116
-
117
- def test_quoting
118
- assert_equal "can't", rison("'can!'t'")
119
- assert_equal 'wow!', rison("'wow!!'")
120
- end
121
-
122
100
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4.7
3
3
  specification_version: 2
4
4
  name: rison
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2007-11-18 00:00:00 +00:00
6
+ version: 1.1.0
7
+ date: 2007-11-19 00:00:00 +00:00
8
8
  summary: Pure Ruby parser for Rison (http://mjtemplate.org/examples/rison.html)
9
9
  require_paths:
10
10
  - lib
@@ -37,12 +37,14 @@ authors:
37
37
  - Tim Fletcher
38
38
  files:
39
39
  - lib/rison
40
+ - lib/rison/dump.rb
40
41
  - lib/rison/evaluator.rb
41
42
  - lib/rison/grammar.rb
42
43
  - lib/rison/lexer.rb
43
44
  - lib/rison/parser.rb
44
45
  - lib/rison.rb
45
- - test/test_rison.rb
46
+ - test/test_dump.rb
47
+ - test/test_parser.rb
46
48
  - COPYING.txt
47
49
  - Rakefile.rb
48
50
  - README.html