rubyjs 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +128 -0
- data/Rakefile +9 -0
- data/bin/rubyjs +140 -0
- data/examples/ex1/Rakefile +7 -0
- data/examples/ex1/ex1.js +771 -0
- data/examples/ex1/ex1.rb +42 -0
- data/examples/ex1/index.html +7 -0
- data/examples/hw/Rakefile +9 -0
- data/examples/hw/hw.js +635 -0
- data/examples/hw/hw.rb +7 -0
- data/examples/hw/index.html +7 -0
- data/patches/parse_tree.rb.diff +34 -0
- data/rubyjs.gemspec +24 -0
- data/src/rubyjs.rb +4 -0
- data/src/rubyjs/code_generator.rb +474 -0
- data/src/rubyjs/compiler.rb +2007 -0
- data/src/rubyjs/debug_name_generator.rb +75 -0
- data/src/rubyjs/encoder.rb +171 -0
- data/src/rubyjs/eval_into.rb +59 -0
- data/src/rubyjs/lib/core.rb +1008 -0
- data/src/rubyjs/lib/json.rb +101 -0
- data/src/rubyjs/model.rb +287 -0
- data/src/rubyjs/name_generator.rb +71 -0
- data/src/rwt/AbsolutePanel.rb +161 -0
- data/src/rwt/DOM.Konqueror.rb +89 -0
- data/src/rwt/DOM.Opera.rb +65 -0
- data/src/rwt/DOM.rb +1044 -0
- data/src/rwt/Event.Opera.rb +35 -0
- data/src/rwt/Event.rb +429 -0
- data/src/rwt/HTTPRequest.IE6.rb +5 -0
- data/src/rwt/HTTPRequest.rb +74 -0
- data/src/rwt/Label.rb +164 -0
- data/src/rwt/Panel.rb +90 -0
- data/src/rwt/RootPanel.rb +16 -0
- data/src/rwt/UIObject.rb +495 -0
- data/src/rwt/Widget.rb +193 -0
- data/src/rwt/ported-from/AbsolutePanel.java +158 -0
- data/src/rwt/ported-from/DOM.java +571 -0
- data/src/rwt/ported-from/DOMImpl.java +426 -0
- data/src/rwt/ported-from/DOMImplOpera.java +82 -0
- data/src/rwt/ported-from/DOMImplStandard.java +234 -0
- data/src/rwt/ported-from/HTTPRequest.java +81 -0
- data/src/rwt/ported-from/HTTPRequestImpl.java +103 -0
- data/src/rwt/ported-from/Label.java +163 -0
- data/src/rwt/ported-from/Panel.java +99 -0
- data/src/rwt/ported-from/UIObject.java +614 -0
- data/src/rwt/ported-from/Widget.java +221 -0
- data/test/benchmark/bm_call_conv1.js +16 -0
- data/test/benchmark/bm_call_conv2.js +14 -0
- data/test/benchmark/bm_var_acc1.js +13 -0
- data/test/benchmark/bm_var_acc2.js +11 -0
- data/test/benchmark/bm_vm1_block.js +15 -0
- data/test/benchmark/bm_vm1_block.rb +15 -0
- data/test/benchmark/bm_vm1_const.js +13 -0
- data/test/benchmark/bm_vm1_const.rb +13 -0
- data/test/benchmark/bm_vm1_ensure.js +17 -0
- data/test/benchmark/bm_vm1_ensure.rb +15 -0
- data/test/benchmark/common.js +4 -0
- data/test/benchmark/common.rb +5 -0
- data/test/benchmark/params.yaml +7 -0
- data/test/browser.test.html +4059 -0
- data/test/browser.test.js +3225 -0
- data/test/common.Browser.rb +13 -0
- data/test/common.rb +8 -0
- data/test/gen_browser_test_suite.rb +129 -0
- data/test/gen_test_suite.rb +41 -0
- data/test/run_benchs.rb +58 -0
- data/test/run_tests.rb +22 -0
- data/test/test_args.rb +24 -0
- data/test/test_array.rb +26 -0
- data/test/test_case.rb +35 -0
- data/test/test_class.rb +55 -0
- data/test/test_eql.rb +9 -0
- data/test/test_exception.rb +61 -0
- data/test/test_expr.rb +12 -0
- data/test/test_hash.rb +29 -0
- data/test/test_if.rb +28 -0
- data/test/test_inspect.rb +10 -0
- data/test/test_lebewesen.rb +39 -0
- data/test/test_massign.rb +66 -0
- data/test/test_new.rb +12 -0
- data/test/test_range.rb +53 -0
- data/test/test_regexp.rb +22 -0
- data/test/test_send.rb +65 -0
- data/test/test_simple_output.rb +5 -0
- data/test/test_splat.rb +21 -0
- data/test/test_string.rb +51 -0
- data/test/test_yield.rb +152 -0
- data/utils/js/Makefile +9 -0
- data/utils/js/RunScript.class +0 -0
- data/utils/js/RunScript.java +73 -0
- data/utils/js/js.jar +0 -0
- data/utils/js/run.sh +3 -0
- data/utils/jsc/Makefile +7 -0
- data/utils/jsc/README +3 -0
- data/utils/jsc/RunScript.c +93 -0
- data/utils/jsc/run.sh +15 -0
- data/utils/yuicompressor/README +1 -0
- data/utils/yuicompressor/yuicompressor-2.2.5.jar +0 -0
- data/vendor/ParseTree-1.7.1-patched/History.txt +217 -0
- data/vendor/ParseTree-1.7.1-patched/Manifest.txt +22 -0
- data/vendor/ParseTree-1.7.1-patched/README.txt +110 -0
- data/vendor/ParseTree-1.7.1-patched/Rakefile +41 -0
- data/vendor/ParseTree-1.7.1-patched/bin/parse_tree_abc +89 -0
- data/vendor/ParseTree-1.7.1-patched/bin/parse_tree_audit +28 -0
- data/vendor/ParseTree-1.7.1-patched/bin/parse_tree_deps +62 -0
- data/vendor/ParseTree-1.7.1-patched/bin/parse_tree_show +49 -0
- data/vendor/ParseTree-1.7.1-patched/demo/printer.rb +20 -0
- data/vendor/ParseTree-1.7.1-patched/lib/composite_sexp_processor.rb +49 -0
- data/vendor/ParseTree-1.7.1-patched/lib/parse_tree.rb +1013 -0
- data/vendor/ParseTree-1.7.1-patched/lib/sexp.rb +235 -0
- data/vendor/ParseTree-1.7.1-patched/lib/sexp_processor.rb +330 -0
- data/vendor/ParseTree-1.7.1-patched/lib/unique.rb +15 -0
- data/vendor/ParseTree-1.7.1-patched/test/pt_testcase.rb +1221 -0
- data/vendor/ParseTree-1.7.1-patched/test/something.rb +53 -0
- data/vendor/ParseTree-1.7.1-patched/test/test_all.rb +13 -0
- data/vendor/ParseTree-1.7.1-patched/test/test_composite_sexp_processor.rb +69 -0
- data/vendor/ParseTree-1.7.1-patched/test/test_parse_tree.rb +216 -0
- data/vendor/ParseTree-1.7.1-patched/test/test_sexp.rb +291 -0
- data/vendor/ParseTree-1.7.1-patched/test/test_sexp_processor.rb +244 -0
- data/vendor/ParseTree-1.7.1-patched/validate.sh +31 -0
- data/vendor/ParseTree-1.7.1/History.txt +217 -0
- data/vendor/ParseTree-1.7.1/Manifest.txt +22 -0
- data/vendor/ParseTree-1.7.1/README.txt +110 -0
- data/vendor/ParseTree-1.7.1/Rakefile +41 -0
- data/vendor/ParseTree-1.7.1/bin/parse_tree_abc +89 -0
- data/vendor/ParseTree-1.7.1/bin/parse_tree_audit +28 -0
- data/vendor/ParseTree-1.7.1/bin/parse_tree_deps +62 -0
- data/vendor/ParseTree-1.7.1/bin/parse_tree_show +49 -0
- data/vendor/ParseTree-1.7.1/demo/printer.rb +20 -0
- data/vendor/ParseTree-1.7.1/lib/composite_sexp_processor.rb +49 -0
- data/vendor/ParseTree-1.7.1/lib/parse_tree.rb +1004 -0
- data/vendor/ParseTree-1.7.1/lib/sexp.rb +235 -0
- data/vendor/ParseTree-1.7.1/lib/sexp_processor.rb +330 -0
- data/vendor/ParseTree-1.7.1/lib/unique.rb +15 -0
- data/vendor/ParseTree-1.7.1/test/pt_testcase.rb +1221 -0
- data/vendor/ParseTree-1.7.1/test/something.rb +53 -0
- data/vendor/ParseTree-1.7.1/test/test_all.rb +13 -0
- data/vendor/ParseTree-1.7.1/test/test_composite_sexp_processor.rb +69 -0
- data/vendor/ParseTree-1.7.1/test/test_parse_tree.rb +216 -0
- data/vendor/ParseTree-1.7.1/test/test_sexp.rb +291 -0
- data/vendor/ParseTree-1.7.1/test/test_sexp_processor.rb +244 -0
- data/vendor/ParseTree-1.7.1/validate.sh +31 -0
- metadata +230 -0
@@ -0,0 +1,235 @@
|
|
1
|
+
##
|
2
|
+
# Sexps are the basic storage mechanism of SexpProcessor. Sexps have
|
3
|
+
# a +type+ (to be renamed +node_type+) which is the first element of
|
4
|
+
# the Sexp. The type is used by SexpProcessor to determine whom to
|
5
|
+
# dispatch the Sexp to for processing.
|
6
|
+
|
7
|
+
$TESTING ||= false # unless defined $TESTING
|
8
|
+
|
9
|
+
class Sexp < Array # ZenTest FULL
|
10
|
+
|
11
|
+
@@array_types = [ :array, :args, ]
|
12
|
+
|
13
|
+
##
|
14
|
+
# Create a new Sexp containing +args+.
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
super(args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.from_array(a)
|
21
|
+
ary = Array === a ? a : [a]
|
22
|
+
|
23
|
+
result = self.new
|
24
|
+
|
25
|
+
ary.each do |x|
|
26
|
+
case x
|
27
|
+
when Sexp
|
28
|
+
result << x
|
29
|
+
when Array
|
30
|
+
result << self.from_array(x)
|
31
|
+
else
|
32
|
+
result << x
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
def ==(obj) # :nodoc:
|
40
|
+
if obj.class == self.class then
|
41
|
+
super
|
42
|
+
else
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def ===(sexp)
|
48
|
+
return nil unless Sexp === sexp
|
49
|
+
pattern = self # this is just for my brain
|
50
|
+
|
51
|
+
return true if pattern == sexp
|
52
|
+
|
53
|
+
sexp.each do |subset|
|
54
|
+
return true if pattern === subset
|
55
|
+
end
|
56
|
+
|
57
|
+
return nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def =~(pattern)
|
61
|
+
return pattern === self
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Returns true if the node_type is +array+ or +args+.
|
66
|
+
#
|
67
|
+
# REFACTOR: to TypedSexp - we only care when we have units.
|
68
|
+
|
69
|
+
def array_type?
|
70
|
+
type = self.first
|
71
|
+
@@array_types.include? type
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Enumeratates the sexp yielding to +b+ when the node_type == +t+.
|
76
|
+
|
77
|
+
def each_of_type(t, &b)
|
78
|
+
each do | elem |
|
79
|
+
if Sexp === elem then
|
80
|
+
elem.each_of_type(t, &b)
|
81
|
+
b.call(elem) if elem.first == t
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Replaces all elements whose node_type is +from+ with +to+. Used
|
88
|
+
# only for the most trivial of rewrites.
|
89
|
+
|
90
|
+
def find_and_replace_all(from, to)
|
91
|
+
each_with_index do | elem, index |
|
92
|
+
if Sexp === elem then
|
93
|
+
elem.find_and_replace_all(from, to)
|
94
|
+
else
|
95
|
+
self[index] = to if elem == from
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def gsub(pattern, repl)
|
101
|
+
return repl if pattern == self
|
102
|
+
|
103
|
+
new = self.map do |subset|
|
104
|
+
case subset
|
105
|
+
when Sexp then
|
106
|
+
subset.gsub(pattern, repl)
|
107
|
+
else
|
108
|
+
subset
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
return Sexp.from_array(new)
|
113
|
+
end
|
114
|
+
|
115
|
+
def inspect # :nodoc:
|
116
|
+
sexp_str = self.map {|x|x.inspect}.join(', ')
|
117
|
+
return "s(#{sexp_str})"
|
118
|
+
end
|
119
|
+
|
120
|
+
def method_missing(meth, delete=false)
|
121
|
+
matches = find_all { | sexp | Sexp === sexp and sexp.first == meth }
|
122
|
+
|
123
|
+
case matches.size
|
124
|
+
when 0 then
|
125
|
+
nil
|
126
|
+
when 1 then
|
127
|
+
match = matches.first
|
128
|
+
delete match if delete
|
129
|
+
match
|
130
|
+
else
|
131
|
+
raise NoMethodError, "multiple nodes for #{meth} were found in #{inspect}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def pretty_print(q) # :nodoc:
|
136
|
+
q.group(1, 's(', ')') do
|
137
|
+
q.seplist(self) {|v| q.pp v }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Returns the Sexp without the node_type.
|
143
|
+
|
144
|
+
def sexp_body
|
145
|
+
self[1..-1]
|
146
|
+
end
|
147
|
+
|
148
|
+
##
|
149
|
+
# If run with debug, Sexp will raise if you shift on an empty
|
150
|
+
# Sexp. Helps with debugging.
|
151
|
+
|
152
|
+
def shift
|
153
|
+
raise "I'm empty" if self.empty?
|
154
|
+
super
|
155
|
+
end if $DEBUG or $TESTING
|
156
|
+
|
157
|
+
##
|
158
|
+
# Returnes the bare bones structure of the sexp.
|
159
|
+
# s(:a, :b, s(:c, :d), :e) => s(:a, s(:c))
|
160
|
+
|
161
|
+
def structure
|
162
|
+
result = self.class.new
|
163
|
+
if Array === self.first then
|
164
|
+
result = self.first.structure
|
165
|
+
else
|
166
|
+
result << self.shift
|
167
|
+
self.grep(Array).each do |subexp|
|
168
|
+
result << subexp.structure
|
169
|
+
end
|
170
|
+
end
|
171
|
+
result
|
172
|
+
end
|
173
|
+
|
174
|
+
def sub(pattern, repl)
|
175
|
+
return repl.dup if pattern == self
|
176
|
+
|
177
|
+
done = false
|
178
|
+
|
179
|
+
new = self.map do |subset|
|
180
|
+
if done then
|
181
|
+
subset
|
182
|
+
else
|
183
|
+
case subset
|
184
|
+
when Sexp then
|
185
|
+
if pattern == subset then
|
186
|
+
done = true
|
187
|
+
repl.dup
|
188
|
+
elsif pattern === subset then
|
189
|
+
done = true
|
190
|
+
subset.sub pattern, repl
|
191
|
+
else
|
192
|
+
subset
|
193
|
+
end
|
194
|
+
else
|
195
|
+
subset
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
return Sexp.from_array(new)
|
201
|
+
end
|
202
|
+
|
203
|
+
def to_a # :nodoc:
|
204
|
+
self.map { |o| Sexp === o ? o.to_a : o }
|
205
|
+
end
|
206
|
+
|
207
|
+
def to_s # :nodoc:
|
208
|
+
inspect
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
class SexpMatchSpecial < Sexp; end
|
214
|
+
|
215
|
+
class SexpAny < SexpMatchSpecial
|
216
|
+
def ===(o)
|
217
|
+
return Sexp === o
|
218
|
+
end
|
219
|
+
|
220
|
+
def inspect
|
221
|
+
"ANY"
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
module SexpMatchSpecials
|
226
|
+
def ANY(); return SexpAny.new; end
|
227
|
+
end
|
228
|
+
|
229
|
+
##
|
230
|
+
# This is just a stupid shortcut to make indentation much cleaner.
|
231
|
+
|
232
|
+
def s(*args)
|
233
|
+
Sexp.new(*args)
|
234
|
+
end
|
235
|
+
|
@@ -0,0 +1,330 @@
|
|
1
|
+
|
2
|
+
$TESTING = false unless defined? $TESTING
|
3
|
+
|
4
|
+
require 'sexp'
|
5
|
+
|
6
|
+
class Object
|
7
|
+
|
8
|
+
##
|
9
|
+
# deep_clone is the usual Marshalling hack to make a deep copy.
|
10
|
+
# It is rather slow, so use it sparingly. Helps with debugging
|
11
|
+
# SexpProcessors since you usually shift off sexps.
|
12
|
+
|
13
|
+
def deep_clone
|
14
|
+
Marshal.load(Marshal.dump(self))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# SexpProcessor base exception class.
|
20
|
+
|
21
|
+
class SexpProcessorError < StandardError; end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Raised by SexpProcessor if it sees a node type listed in its
|
25
|
+
# unsupported list.
|
26
|
+
|
27
|
+
class UnsupportedNodeError < SexpProcessorError; end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Raised by SexpProcessor if it is in strict mode and sees a node for
|
31
|
+
# which there is no processor available.
|
32
|
+
|
33
|
+
class UnknownNodeError < SexpProcessorError; end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Raised by SexpProcessor if a processor did not process every node in
|
37
|
+
# a sexp and @require_empty is true.
|
38
|
+
|
39
|
+
class NotEmptyError < SexpProcessorError; end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Raised if assert_type encounters an unexpected sexp type.
|
43
|
+
|
44
|
+
class SexpTypeError < SexpProcessorError; end
|
45
|
+
|
46
|
+
##
|
47
|
+
# SexpProcessor provides a uniform interface to process Sexps.
|
48
|
+
#
|
49
|
+
# In order to create your own SexpProcessor subclass you'll need
|
50
|
+
# to call super in the initialize method, then set any of the
|
51
|
+
# Sexp flags you want to be different from the defaults.
|
52
|
+
#
|
53
|
+
# SexpProcessor uses a Sexp's type to determine which process method
|
54
|
+
# to call in the subclass. For Sexp <code>s(:lit, 1)</code>
|
55
|
+
# SexpProcessor will call #process_lit, if it is defined.
|
56
|
+
#
|
57
|
+
# You can also specify a default method to call for any Sexp types
|
58
|
+
# without a process_<type> method or use the default processor provided to
|
59
|
+
# skip over them.
|
60
|
+
#
|
61
|
+
# Here is a simple example:
|
62
|
+
#
|
63
|
+
# class MyProcessor < SexpProcessor
|
64
|
+
# def initialize
|
65
|
+
# super
|
66
|
+
# self.strict = false
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# def process_lit(exp)
|
70
|
+
# val = exp.shift
|
71
|
+
# return val
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
|
75
|
+
class SexpProcessor
|
76
|
+
|
77
|
+
##
|
78
|
+
# A default method to call if a process_<type> method is not found
|
79
|
+
# for the Sexp type.
|
80
|
+
|
81
|
+
attr_accessor :default_method
|
82
|
+
|
83
|
+
##
|
84
|
+
# Emit a warning when the method in #default_method is called.
|
85
|
+
|
86
|
+
attr_accessor :warn_on_default
|
87
|
+
|
88
|
+
##
|
89
|
+
# Automatically shifts off the Sexp type before handing the
|
90
|
+
# Sexp to process_<type>
|
91
|
+
|
92
|
+
attr_accessor :auto_shift_type
|
93
|
+
|
94
|
+
##
|
95
|
+
# An array that specifies node types that are unsupported by this
|
96
|
+
# processor. SexpProcessor will raise UnsupportedNodeError if you try
|
97
|
+
# to process one of those node types.
|
98
|
+
|
99
|
+
attr_accessor :unsupported
|
100
|
+
|
101
|
+
##
|
102
|
+
# Raise an exception if no process_<type> method is found for a Sexp.
|
103
|
+
|
104
|
+
attr_accessor :strict
|
105
|
+
|
106
|
+
##
|
107
|
+
# A Hash of Sexp types and Regexp.
|
108
|
+
#
|
109
|
+
# Print a debug message if the Sexp type matches the Hash key
|
110
|
+
# and the Sexp's #inspect output matches the Regexp.
|
111
|
+
|
112
|
+
attr_accessor :debug
|
113
|
+
|
114
|
+
##
|
115
|
+
# Expected result class
|
116
|
+
|
117
|
+
attr_accessor :expected
|
118
|
+
|
119
|
+
##
|
120
|
+
# Raise an exception if the Sexp is not empty after processing
|
121
|
+
|
122
|
+
attr_accessor :require_empty
|
123
|
+
|
124
|
+
##
|
125
|
+
# Creates a new SexpProcessor. Use super to invoke this
|
126
|
+
# initializer from SexpProcessor subclasses, then use the
|
127
|
+
# attributes above to customize the functionality of the
|
128
|
+
# SexpProcessor
|
129
|
+
|
130
|
+
def initialize
|
131
|
+
@default_method = nil
|
132
|
+
@warn_on_default = true
|
133
|
+
@auto_shift_type = false
|
134
|
+
@strict = false
|
135
|
+
@unsupported = [:alloca, :cfunc, :cref, :evstr, :ifunc, :last, :memo, :newline, :opt_n, :method] # internal nodes that you can't get to
|
136
|
+
@unsupported_checked = false
|
137
|
+
@debug = {}
|
138
|
+
@expected = Sexp
|
139
|
+
@require_empty = true
|
140
|
+
@exceptions = {}
|
141
|
+
@process_level = 0
|
142
|
+
|
143
|
+
# we do this on an instance basis so we can subclass it for
|
144
|
+
# different processors.
|
145
|
+
@processors = {}
|
146
|
+
@rewriters = {}
|
147
|
+
|
148
|
+
public_methods.each do |name|
|
149
|
+
case name
|
150
|
+
when /^process_(.*)/ then
|
151
|
+
@processors[$1.intern] = name.intern
|
152
|
+
when /^rewrite_(.*)/ then
|
153
|
+
@rewriters[$1.intern] = name.intern
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def assert_empty(meth, exp, exp_orig)
|
159
|
+
unless exp.empty? then
|
160
|
+
msg = "exp not empty after #{self.class}.#{meth} on #{exp.inspect}"
|
161
|
+
msg += " from #{exp_orig.inspect}" if $DEBUG
|
162
|
+
raise NotEmptyError, msg
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def rewrite(exp)
|
167
|
+
meth = @rewriters[exp.first]
|
168
|
+
if meth then
|
169
|
+
r = self.send(meth, exp)
|
170
|
+
self.assert_empty(meth, exp, nil) if @require_empty
|
171
|
+
r
|
172
|
+
else
|
173
|
+
result = exp.class.new
|
174
|
+
until exp.empty? do
|
175
|
+
sub_exp = exp.shift
|
176
|
+
sub_result = nil
|
177
|
+
|
178
|
+
if Array === sub_exp then
|
179
|
+
result << rewrite(sub_exp)
|
180
|
+
else
|
181
|
+
result << sub_exp
|
182
|
+
end
|
183
|
+
end
|
184
|
+
result
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
##
|
189
|
+
# Default Sexp processor. Invokes process_<type> methods matching
|
190
|
+
# the Sexp type given. Performs additional checks as specified by
|
191
|
+
# the initializer.
|
192
|
+
|
193
|
+
def process(exp)
|
194
|
+
return nil if exp.nil?
|
195
|
+
|
196
|
+
@process_level += 1
|
197
|
+
|
198
|
+
unless @unsupported_checked then
|
199
|
+
m = public_methods.grep(/^process_/) { |o| o.sub(/^process_/, '').intern }
|
200
|
+
supported = m - (m - @unsupported)
|
201
|
+
|
202
|
+
raise UnsupportedNodeError, "#{supported.inspect} shouldn't be in @unsupported" unless supported.empty?
|
203
|
+
|
204
|
+
@unsupported_checked = true
|
205
|
+
end
|
206
|
+
|
207
|
+
result = self.expected.new
|
208
|
+
|
209
|
+
type = exp.first
|
210
|
+
|
211
|
+
if @debug.has_key? type then
|
212
|
+
str = exp.inspect
|
213
|
+
puts "// DEBUG: #{str}" if str =~ @debug[type]
|
214
|
+
end
|
215
|
+
|
216
|
+
exp_orig = nil
|
217
|
+
exp_orig = exp.deep_clone if $DEBUG or
|
218
|
+
@debug.has_key? type or @exceptions.has_key?(type)
|
219
|
+
|
220
|
+
raise UnsupportedNodeError, "'#{type}' is not a supported node type" if @unsupported.include? type
|
221
|
+
|
222
|
+
exp = self.rewrite(exp) if @process_level == 1
|
223
|
+
|
224
|
+
# now do a pass with the real processor (or generic)
|
225
|
+
meth = @processors[type] || @default_method
|
226
|
+
if meth then
|
227
|
+
|
228
|
+
if @warn_on_default and meth == @default_method then
|
229
|
+
$stderr.puts "WARNING: Using default method #{meth} for #{type}"
|
230
|
+
end
|
231
|
+
|
232
|
+
exp.shift if @auto_shift_type and meth != @default_method
|
233
|
+
|
234
|
+
result = error_handler(type, exp_orig) do
|
235
|
+
self.send(meth, exp)
|
236
|
+
end
|
237
|
+
|
238
|
+
raise SexpTypeError, "Result must be a #{@expected}, was #{result.class}:#{result.inspect}" unless @expected === result
|
239
|
+
|
240
|
+
self.assert_empty(meth, exp, exp_orig) if @require_empty
|
241
|
+
else
|
242
|
+
unless @strict then
|
243
|
+
until exp.empty? do
|
244
|
+
sub_exp = exp.shift
|
245
|
+
sub_result = nil
|
246
|
+
if Array === sub_exp then
|
247
|
+
sub_result = error_handler(type, exp_orig) do
|
248
|
+
process(sub_exp)
|
249
|
+
end
|
250
|
+
raise "Result is a bad type" unless Array === sub_exp
|
251
|
+
raise "Result does not have a type in front: #{sub_exp.inspect}" unless Symbol === sub_exp.first unless sub_exp.empty?
|
252
|
+
else
|
253
|
+
sub_result = sub_exp
|
254
|
+
end
|
255
|
+
result << sub_result
|
256
|
+
end
|
257
|
+
|
258
|
+
# NOTE: this is costly, but we are in the generic processor
|
259
|
+
# so we shouldn't hit it too much with RubyToC stuff at least.
|
260
|
+
#if Sexp === exp and not exp.sexp_type.nil? then
|
261
|
+
begin
|
262
|
+
result.sexp_type = exp.sexp_type
|
263
|
+
rescue Exception
|
264
|
+
# nothing to do, on purpose
|
265
|
+
end
|
266
|
+
else
|
267
|
+
msg = "Bug! Unknown node-type #{type.inspect} to #{self.class}"
|
268
|
+
msg += " in #{exp_orig.inspect} from #{caller.inspect}" if $DEBUG
|
269
|
+
raise UnknownNodeError, msg
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
@process_level -= 1
|
274
|
+
|
275
|
+
result
|
276
|
+
end
|
277
|
+
|
278
|
+
def generate # :nodoc:
|
279
|
+
raise NotImplementedError, "not implemented yet"
|
280
|
+
end
|
281
|
+
|
282
|
+
##
|
283
|
+
# Raises unless the Sexp type for +list+ matches +typ+
|
284
|
+
|
285
|
+
def assert_type(list, typ)
|
286
|
+
raise SexpTypeError, "Expected type #{typ.inspect} in #{list.inspect}" if
|
287
|
+
list.first != typ
|
288
|
+
end
|
289
|
+
|
290
|
+
def error_handler(type, exp=nil) # :nodoc:
|
291
|
+
begin
|
292
|
+
return yield
|
293
|
+
rescue StandardError => err
|
294
|
+
if @exceptions.has_key? type then
|
295
|
+
return @exceptions[type].call(self, exp, err)
|
296
|
+
else
|
297
|
+
$stderr.puts "#{err.class} Exception thrown while processing #{type} for sexp #{exp.inspect} #{caller.inspect}" if $DEBUG
|
298
|
+
raise
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
private :error_handler
|
303
|
+
|
304
|
+
##
|
305
|
+
# Registers an error handler for +node+
|
306
|
+
|
307
|
+
def on_error_in(node_type, &block)
|
308
|
+
@exceptions[node_type] = block
|
309
|
+
end
|
310
|
+
|
311
|
+
##
|
312
|
+
# A fairly generic processor for a dummy node. Dummy nodes are used
|
313
|
+
# when your processor is doing a complicated rewrite that replaces
|
314
|
+
# the current sexp with multiple sexps.
|
315
|
+
#
|
316
|
+
# Bogus Example:
|
317
|
+
#
|
318
|
+
# def process_something(exp)
|
319
|
+
# return s(:dummy, process(exp), s(:extra, 42))
|
320
|
+
# end
|
321
|
+
|
322
|
+
def process_dummy(exp)
|
323
|
+
result = @expected.new(:dummy) rescue @expected.new
|
324
|
+
until exp.empty? do
|
325
|
+
result << self.process(exp.shift)
|
326
|
+
end
|
327
|
+
result
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|