sexp_processor 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/History.txt +7 -0
  2. data/lib/sexp.rb +1 -1
  3. data/lib/sexp_processor.rb +67 -62
  4. metadata +9 -9
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 3.0.5 / 2010-09-01
2
+
3
+ * 2 minor enhancements:
4
+
5
+ * Added in_context to clean up code.
6
+ * optimize inspect to avoid needlessly caching @line
7
+
1
8
  === 3.0.4 / 2010-03-27
2
9
 
3
10
  * 1 minor enhancement:
data/lib/sexp.rb CHANGED
@@ -134,7 +134,7 @@ class Sexp < Array # ZenTest FULL
134
134
 
135
135
  def inspect # :nodoc:
136
136
  sexp_str = self.map {|x|x.inspect}.join(', ')
137
- if line && ENV['VERBOSE'] then
137
+ if ENV['VERBOSE'] && line then
138
138
  "s(#{sexp_str}).line(#{line})"
139
139
  else
140
140
  "s(#{sexp_str})"
@@ -34,7 +34,7 @@ require 'sexp'
34
34
 
35
35
  class SexpProcessor
36
36
 
37
- VERSION = '3.0.4'
37
+ VERSION = '3.0.5'
38
38
 
39
39
  ##
40
40
  # Automatically shifts off the Sexp type before handing the
@@ -121,9 +121,9 @@ class SexpProcessor
121
121
  public_methods.each do |name|
122
122
  case name
123
123
  when /^process_(.*)/ then
124
- @processors[$1.intern] = name.intern
124
+ @processors[$1.to_sym] = name.to_sym
125
125
  when /^rewrite_(.*)/ then
126
- @rewriters[$1.intern] = name.intern
126
+ @rewriters[$1.to_sym] = name.to_sym
127
127
  end
128
128
  end
129
129
  end
@@ -139,11 +139,9 @@ class SexpProcessor
139
139
  def rewrite(exp)
140
140
  type = exp.first
141
141
 
142
- self.context.unshift type
143
-
144
- exp.map! { |sub| Array === sub ? rewrite(sub) : sub }
145
-
146
- self.context.shift
142
+ in_context type do
143
+ exp.map! { |sub| Array === sub ? rewrite(sub) : sub }
144
+ end
147
145
 
148
146
  begin
149
147
  meth = @rewriters[type]
@@ -165,7 +163,7 @@ class SexpProcessor
165
163
  exp = self.rewrite(exp) if self.context.empty?
166
164
 
167
165
  unless @unsupported_checked then
168
- m = public_methods.grep(/^process_/) { |o| o.to_s.sub(/^process_/, '').intern }
166
+ m = public_methods.grep(/^process_/) { |o| o.to_s.sub(/^process_/, '').to_sym }
169
167
  supported = m - (m - @unsupported)
170
168
 
171
169
  raise UnsupportedNodeError, "#{supported.inspect} shouldn't be in @unsupported" unless supported.empty?
@@ -179,75 +177,74 @@ class SexpProcessor
179
177
  raise "type should be a Symbol, not: #{exp.first.inspect}" unless
180
178
  Symbol === type
181
179
 
182
- self.context.unshift type
180
+ in_context type do
181
+ if @debug.has_key? type then
182
+ str = exp.inspect
183
+ puts "// DEBUG: #{str}" if str =~ @debug[type]
184
+ end
183
185
 
184
- if @debug.has_key? type then
185
- str = exp.inspect
186
- puts "// DEBUG: #{str}" if str =~ @debug[type]
187
- end
186
+ exp_orig = nil
187
+ exp_orig = exp.deep_clone if $DEBUG or
188
+ @debug.has_key? type or @exceptions.has_key?(type)
188
189
 
189
- exp_orig = nil
190
- exp_orig = exp.deep_clone if $DEBUG or
191
- @debug.has_key? type or @exceptions.has_key?(type)
190
+ raise UnsupportedNodeError, "'#{type}' is not a supported node type" if
191
+ @unsupported.include? type
192
192
 
193
- raise UnsupportedNodeError, "'#{type}' is not a supported node type" if
194
- @unsupported.include? type
193
+ if @debug.has_key? type then
194
+ str = exp.inspect
195
+ puts "// DEBUG (rewritten): #{str}" if str =~ @debug[type]
196
+ end
195
197
 
196
- if @debug.has_key? type then
197
- str = exp.inspect
198
- puts "// DEBUG (rewritten): #{str}" if str =~ @debug[type]
199
- end
198
+ # now do a pass with the real processor (or generic)
199
+ meth = @processors[type] || @default_method
200
+ if meth then
200
201
 
201
- # now do a pass with the real processor (or generic)
202
- meth = @processors[type] || @default_method
203
- if meth then
202
+ if @warn_on_default and meth == @default_method then
203
+ warn "WARNING: Using default method #{meth} for #{type}"
204
+ end
204
205
 
205
- if @warn_on_default and meth == @default_method then
206
- warn "WARNING: Using default method #{meth} for #{type}"
207
- end
206
+ exp.shift if @auto_shift_type and meth != @default_method
208
207
 
209
- exp.shift if @auto_shift_type and meth != @default_method
208
+ result = error_handler(type, exp_orig) do
209
+ self.send(meth, exp)
210
+ end
210
211
 
211
- result = error_handler(type, exp_orig) do
212
- self.send(meth, exp)
213
- end
212
+ raise SexpTypeError, "Result must be a #{@expected}, was #{result.class}:#{result.inspect}" unless @expected === result
214
213
 
215
- raise SexpTypeError, "Result must be a #{@expected}, was #{result.class}:#{result.inspect}" unless @expected === result
216
-
217
- self.assert_empty(meth, exp, exp_orig) if @require_empty
218
- else
219
- unless @strict then
220
- until exp.empty? do
221
- sub_exp = exp.shift
222
- sub_result = nil
223
- if Array === sub_exp then
224
- sub_result = error_handler(type, exp_orig) do
225
- process(sub_exp)
214
+ self.assert_empty(meth, exp, exp_orig) if @require_empty
215
+ else
216
+ unless @strict then
217
+ until exp.empty? do
218
+ sub_exp = exp.shift
219
+ sub_result = nil
220
+ if Array === sub_exp then
221
+ sub_result = error_handler(type, exp_orig) do
222
+ process(sub_exp)
223
+ end
224
+ raise "Result is a bad type" unless Array === sub_exp
225
+ raise "Result does not have a type in front: #{sub_exp.inspect}" unless Symbol === sub_exp.first unless sub_exp.empty?
226
+ else
227
+ sub_result = sub_exp
226
228
  end
227
- raise "Result is a bad type" unless Array === sub_exp
228
- raise "Result does not have a type in front: #{sub_exp.inspect}" unless Symbol === sub_exp.first unless sub_exp.empty?
229
- else
230
- sub_result = sub_exp
229
+ result << sub_result
231
230
  end
232
- result << sub_result
233
- end
234
231
 
235
- # NOTE: this is costly, but we are in the generic processor
236
- # so we shouldn't hit it too much with RubyToC stuff at least.
237
- #if Sexp === exp and not exp.sexp_type.nil? then
238
- begin
239
- result.sexp_type = exp.sexp_type
240
- rescue Exception
241
- # nothing to do, on purpose
232
+ # NOTE: this is costly, but we are in the generic processor
233
+ # so we shouldn't hit it too much with RubyToC stuff at least.
234
+ #if Sexp === exp and not exp.sexp_type.nil? then
235
+ begin
236
+ result.sexp_type = exp.sexp_type
237
+ rescue Exception
238
+ # nothing to do, on purpose
239
+ end
240
+ else
241
+ msg = "Bug! Unknown node-type #{type.inspect} to #{self.class}"
242
+ msg += " in #{exp_orig.inspect} from #{caller.inspect}" if $DEBUG
243
+ raise UnknownNodeError, msg
242
244
  end
243
- else
244
- msg = "Bug! Unknown node-type #{type.inspect} to #{self.class}"
245
- msg += " in #{exp_orig.inspect} from #{caller.inspect}" if $DEBUG
246
- raise UnknownNodeError, msg
247
245
  end
248
246
  end
249
247
 
250
- self.context.shift
251
248
  result
252
249
  end
253
250
 
@@ -323,6 +320,14 @@ class SexpProcessor
323
320
  env.scope(&block)
324
321
  end
325
322
 
323
+ def in_context type
324
+ self.context.unshift type
325
+
326
+ yield
327
+
328
+ self.context.shift
329
+ end
330
+
326
331
  ##
327
332
  # I really hate this here, but I hate subdirs in my lib dir more...
328
333
  # I guess it is kinda like shaving... I'll split this out when it
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 3
7
7
  - 0
8
- - 4
9
- version: 3.0.4
8
+ - 5
9
+ version: 3.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Davis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-27 00:00:00 -07:00
17
+ date: 2010-09-01 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,8 +27,8 @@ dependencies:
27
27
  segments:
28
28
  - 2
29
29
  - 0
30
- - 3
31
- version: 2.0.3
30
+ - 4
31
+ version: 2.0.4
32
32
  type: :development
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -40,9 +40,9 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
42
  - 1
43
- - 5
43
+ - 6
44
44
  - 0
45
- version: 1.5.0
45
+ version: 1.6.0
46
46
  type: :development
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
@@ -54,9 +54,9 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  segments:
56
56
  - 2
57
- - 5
57
+ - 6
58
58
  - 0
59
- version: 2.5.0
59
+ version: 2.6.0
60
60
  type: :development
61
61
  version_requirements: *id003
62
62
  description: |-