rubylexer 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ require "set" #; #; #; def assert ( expr , msg
2
+ = "assertion failed" ) #; $DEBUG and ( expr or
3
+ raise msg ) #; end #; #; @@printed = Set .
4
+ new #; def fixme ( s ) #; @@printed .
5
+ include? ( s ) and return #; $DEBUG and
6
+ STDERR . print "FIXME: #{s}\n" #; @@printed . add s
7
+ #; end #;
@@ -0,0 +1 @@
1
+ require 'foo'
data/testdata/w.rb ADDED
@@ -0,0 +1,22 @@
1
+ p %r{\/$}
2
+ p %r~<!include:([\/\w\.\-]+)>~m
3
+
4
+ p [].push *[1,2,3]
5
+ p /\n/
6
+
7
+ $a=1
8
+ @b=2
9
+ @@c=3
10
+ p(/\#$a \#@b \#@@c \#{$a+@b+@@c}/)
11
+
12
+
13
+ class Foo
14
+ attr :foo,true
15
+ end
16
+ f=Foo.new
17
+ p f.foo
18
+ p f.foo=9
19
+ p f.foo =19
20
+ p f.foo= 29
21
+ p f.foo = 39
22
+ p f.foo
@@ -0,0 +1,499 @@
1
+ # SOAP4R - SOAP WSDL driver
2
+ # Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
+
4
+ # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
+ # redistribute it and/or modify it under the same terms of Ruby's license;
6
+ # either the dual license version in 2003, or any later version.
7
+
8
+
9
+ require 'wsdl/parser'
10
+ require 'wsdl/importer'
11
+ require 'xsd/qname'
12
+ require 'soap/element'
13
+ require 'soap/baseData'
14
+ require 'soap/streamHandler'
15
+ require 'soap/mapping'
16
+ require 'soap/mapping/wsdlRegistry'
17
+ require 'soap/rpc/rpc'
18
+ require 'soap/rpc/element'
19
+ require 'soap/processor'
20
+ require 'logger'
21
+
22
+
23
+ module SOAP
24
+
25
+
26
+ class WSDLDriverFactory
27
+ class FactoryError < StandardError; end
28
+
29
+ attr_reader :wsdl
30
+
31
+ def initialize(wsdl, logdev = nil)
32
+ @logdev = logdev
33
+ @wsdl = import(wsdl)
34
+ end
35
+
36
+ def inspect
37
+ "#<#{self.class}:#{@wsdl.name}>"
38
+ end
39
+
40
+ def create_driver(servicename = nil, portname = nil)
41
+ service = if servicename
42
+ @wsdl.service(XSD::QName.new(@wsdl.targetnamespace, servicename))
43
+ else
44
+ @wsdl.services[0]
45
+ end
46
+ if service.nil?
47
+ raise FactoryError.new("Service #{ servicename } not found in WSDL.")
48
+ end
49
+ port = if portname
50
+ service.ports[XSD::QName.new(@wsdl.targetnamespace, portname)]
51
+ else
52
+ service.ports[0]
53
+ end
54
+ if port.nil?
55
+ raise FactoryError.new("Port #{ portname } not found in WSDL.")
56
+ end
57
+ if port.soap_address.nil?
58
+ raise FactoryError.new("soap:address element not found in WSDL.")
59
+ end
60
+ WSDLDriver.new(@wsdl, port, @logdev)
61
+ end
62
+
63
+ # Backward compatibility.
64
+ alias createDriver create_driver
65
+
66
+ private
67
+
68
+ def import(location)
69
+ WSDL::Importer.import(location)
70
+ end
71
+ end
72
+
73
+
74
+ class WSDLDriver
75
+ class << self
76
+ def __attr_proxy(symbol, assignable = false)
77
+ name = symbol.to_s
78
+ module_eval <<-EOD
79
+ def #{name}
80
+ @servant.#{name}
81
+ end
82
+ EOD
83
+ if assignable
84
+ module_eval <<-EOD
85
+ def #{name}=(rhs)
86
+ @servant.#{name} = rhs
87
+ end
88
+ EOD
89
+ end
90
+ end
91
+ end
92
+
93
+ __attr_proxy :options
94
+ __attr_proxy :endpoint_url, true
95
+ __attr_proxy :mapping_registry, true # for RPC unmarshal
96
+ __attr_proxy :wsdl_mapping_registry, true # for RPC marshal
97
+ __attr_proxy :default_encodingstyle, true
98
+ __attr_proxy :allow_unqualified_element, true
99
+ __attr_proxy :generate_explicit_type, true
100
+
101
+ def httpproxy
102
+ @servant.options["protocol.http.proxy"]
103
+ end
104
+
105
+ def httpproxy=(httpproxy)
106
+ @servant.options["protocol.http.proxy"] = httpproxy
107
+ end
108
+
109
+ def wiredump_dev
110
+ @servant.options["protocol.http.wiredump_dev"]
111
+ end
112
+
113
+ def wiredump_dev=(wiredump_dev)
114
+ @servant.options["protocol.http.wiredump_dev"] = wiredump_dev
115
+ end
116
+
117
+ def mandatorycharset
118
+ @servant.options["protocol.mandatorycharset"]
119
+ end
120
+
121
+ def mandatorycharset=(mandatorycharset)
122
+ @servant.options["protocol.mandatorycharset"] = mandatorycharset
123
+ end
124
+
125
+ def wiredump_file_base
126
+ @servant.options["protocol.wiredump_file_base"]
127
+ end
128
+
129
+ def wiredump_file_base=(wiredump_file_base)
130
+ @servant.options["protocol.wiredump_file_base"] = wiredump_file_base
131
+ end
132
+
133
+ def initialize(wsdl, port, logdev)
134
+ @servant = Servant__.new(self, wsdl, port, logdev)
135
+ end
136
+
137
+ def inspect
138
+ "#<#{self.class}:#{@servant.port.name}>"
139
+ end
140
+
141
+ def reset_stream
142
+ @servant.streamhandler.reset
143
+ end
144
+
145
+ # Backward compatibility.
146
+ alias generateEncodeType= generate_explicit_type=
147
+
148
+ class Servant__
149
+ include Logger::Severity
150
+ include SOAP
151
+
152
+ attr_reader :options
153
+ attr_reader :streamhandler
154
+ attr_reader :port
155
+
156
+ attr_accessor :mapping_registry
157
+ attr_accessor :wsdl_mapping_registry
158
+ attr_accessor :default_encodingstyle
159
+ attr_accessor :allow_unqualified_element
160
+ attr_accessor :generate_explicit_type
161
+
162
+ def initialize(host, wsdl, port, logdev)
163
+ @host = host
164
+ @wsdl = wsdl
165
+ @port = port
166
+ @logdev = logdev
167
+
168
+ @options = setup_options
169
+ @mapping_registry = nil # for rpc unmarshal
170
+ @wsdl_mapping_registry = nil # for rpc marshal
171
+ @default_encodingstyle = EncodingNamespace
172
+ @allow_unqualified_element = true
173
+ @generate_explicit_type = false
174
+ @wiredump_file_base = nil
175
+ @mandatorycharset = nil
176
+
177
+ @wsdl_elements = @wsdl.collect_elements
178
+ @wsdl_types = @wsdl.collect_complextypes
179
+ @rpc_decode_typemap = @wsdl_types +
180
+ @wsdl.soap_rpc_complextypes(port.find_binding)
181
+ @wsdl_mapping_registry = Mapping::WSDLRegistry.new(@rpc_decode_typemap)
182
+ @doc_mapper = Mapper.new(@wsdl_elements, @wsdl_types)
183
+ endpoint_url = @port.soap_address.location
184
+ @streamhandler = HTTPPostStreamHandler.new(endpoint_url,
185
+ @options["protocol.http"] ||= Property.new)
186
+ # Convert a map which key is QName, to a Hash which key is String.
187
+ @operations = {}
188
+ @port.inputoperation_map.each do |op_name, op_info|
189
+ @operations[op_name.name] = op_info
190
+ add_method_interface(op_info)
191
+ end
192
+ end
193
+
194
+ def endpoint_url
195
+ @streamhandler.endpoint_url
196
+ end
197
+
198
+ def endpoint_url=(endpoint_url)
199
+ @streamhandler.endpoint_url = endpoint_url
200
+ @streamhandler.reset
201
+ end
202
+
203
+ def rpc_send(method_name, *params)
204
+ log(INFO) { "call: calling method '#{ method_name }'." }
205
+ log(DEBUG) { "call: parameters '#{ params.inspect }'." }
206
+
207
+ op_info = @operations[method_name]
208
+ parts_names = op_info.bodyparts.collect { |part| part.name }
209
+ obj = create_method_obj(parts_names, params)
210
+ method = Mapping.obj2soap(obj, @wsdl_mapping_registry, op_info.optype_name)
211
+ method.elename = op_info.op_name
212
+ method.type = XSD::QName.new # Request should not be typed.
213
+ req_header = nil
214
+ req_body = SOAPBody.new(method)
215
+
216
+ if @wiredump_file_base
217
+ @streamhandler.wiredump_file_base =
218
+ @wiredump_file_base + '_' << method_name
219
+ end
220
+
221
+ begin
222
+ opt = create_options
223
+ opt[:decode_typemap] = @rpc_decode_typemap
224
+ res_header, res_body = invoke(req_header, req_body, op_info, opt)
225
+ if res_body.fault
226
+ raise SOAP::FaultError.new(res_body.fault)
227
+ end
228
+ rescue SOAP::FaultError => e
229
+ Mapping.fault2exception(e)
230
+ end
231
+
232
+ #breakpoint
233
+ ret = res_body.response ?
234
+ Mapping.soap2obj(res_body.response, @mapping_registry) : nil
235
+
236
+ if res_body.outparams
237
+ outparams = res_body.outparams.collect { |outparam|
238
+ Mapping.soap2obj(outparam)
239
+ }
240
+ return [ret].concat(outparams)
241
+ else
242
+ return ret
243
+ end
244
+ end
245
+
246
+ # req_header: [[element, mustunderstand, encodingstyle(QName/String)], ...]
247
+ # req_body: SOAPBasetype/SOAPCompoundtype
248
+ def document_send(name, header_obj, body_obj)
249
+ log(INFO) { "document_send: sending document '#{ name }'." }
250
+ op_info = @operations[name]
251
+ req_header = header_from_obj(header_obj, op_info)
252
+ req_body = body_from_obj(body_obj, op_info)
253
+ opt = create_options
254
+ res_header, res_body = invoke(req_header, req_body, op_info, opt)
255
+ if res_body.fault
256
+ raise SOAP::FaultError.new(res_body.fault)
257
+ end
258
+ res_body_obj = res_body.response ?
259
+ Mapping.soap2obj(res_body.response, @mapping_registry) : nil
260
+ return res_header, res_body_obj
261
+ end
262
+
263
+ private
264
+
265
+ def create_method_obj(names, params)
266
+ o = Object.new
267
+ for idx in 0 ... params.length
268
+ o.instance_eval("@#{ names[idx] } = params[idx]")
269
+ end
270
+ o
271
+ end
272
+
273
+ def invoke(req_header, req_body, op_info, opt)
274
+ send_string = Processor.marshal(req_header, req_body, opt)
275
+ log(DEBUG) { "invoke: sending string #{ send_string }" }
276
+ data = @streamhandler.send(send_string, op_info.soapaction)
277
+ log(DEBUG) { "invoke: received string #{ data.receive_string }" }
278
+ if data.receive_string.empty?
279
+ return nil, nil
280
+ end
281
+ opt[:charset] = @mandatorycharset ||
282
+ StreamHandler.parse_media_type(data.receive_contenttype)
283
+ res_header, res_body = Processor.unmarshal(data.receive_string, opt)
284
+ return res_header, res_body
285
+ end
286
+
287
+ def header_from_obj(obj, op_info)
288
+ if obj.is_a?(SOAPHeader)
289
+ obj
290
+ elsif op_info.headerparts.empty?
291
+ if obj.nil?
292
+ nil
293
+ else
294
+ raise RuntimeError.new("No header definition in schema.")
295
+ end
296
+ elsif op_info.headerparts.size == 1
297
+ part = op_info.headerparts[0]
298
+ header = SOAPHeader.new()
299
+ header.add(headeritem_from_obj(obj, part.element || part.eletype))
300
+ header
301
+ else
302
+ header = SOAPHeader.new()
303
+ op_info.headerparts.each do |part|
304
+ child = obj[part.elename.name]
305
+ ele = headeritem_from_obj(child, part.element || part.eletype)
306
+ header.add(ele)
307
+ end
308
+ header
309
+ end
310
+ end
311
+
312
+ def headeritem_from_obj(obj, name)
313
+ if obj.nil?
314
+ SOAPElement.new(name)
315
+ elsif obj.is_a?(SOAPHeaderItem)
316
+ obj
317
+ else
318
+ @doc_mapper.obj2ele(obj, name)
319
+ end
320
+ end
321
+
322
+ def body_from_obj(obj, op_info)
323
+ if obj.is_a?(SOAPBody)
324
+ obj
325
+ elsif op_info.bodyparts.empty?
326
+ if obj.nil?
327
+ nil
328
+ else
329
+ raise RuntimeError.new("No body found in schema.")
330
+ end
331
+ elsif op_info.bodyparts.size == 1
332
+ part = op_info.bodyparts[0]
333
+ ele = bodyitem_from_obj(obj, part.element || part.type)
334
+ SOAPBody.new(ele)
335
+ else
336
+ body = SOAPBody.new
337
+ op_info.bodyparts.each do |part|
338
+ child = obj[part.elename.name]
339
+ ele = bodyitem_from_obj(child, part.element || part.type)
340
+ body.add(ele.elename.name, ele)
341
+ end
342
+ body
343
+ end
344
+ end
345
+
346
+ def bodyitem_from_obj(obj, name)
347
+ if obj.nil?
348
+ SOAPElement.new(name)
349
+ elsif obj.is_a?(SOAPElement)
350
+ obj
351
+ else
352
+ @doc_mapper.obj2ele(obj, name)
353
+ end
354
+ end
355
+
356
+ def add_method_interface(op_info)
357
+ case op_info.style
358
+ when :document
359
+ add_document_method_interface(op_info.op_name.name)
360
+ when :rpc
361
+ parts_names = op_info.bodyparts.collect { |part| part.name }
362
+ add_rpc_method_interface(op_info.op_name.name, parts_names)
363
+ else
364
+ raise RuntimeError.new("Unknown style: #{op_info.style}")
365
+ end
366
+ end
367
+
368
+ def add_document_method_interface(name)
369
+ @host.instance_eval <<-EOS
370
+ def #{ name }(headers, body)
371
+ @servant.document_send(#{ name.dump }, headers, body)
372
+ end
373
+ EOS
374
+ end
375
+
376
+ def add_rpc_method_interface(name, parts_names)
377
+ i = 0
378
+ param_names = parts_names.collect { |orgname| i += 1; "arg#{ i }" }
379
+ callparam = (param_names.collect { |pname| ", " + pname }).join
380
+ @host.instance_eval <<-EOS
381
+ def #{ name }(#{ param_names.join(", ") })
382
+ @servant.rpc_send(#{ name.dump }#{ callparam })
383
+ end
384
+ EOS
385
+ end
386
+
387
+ def create_options
388
+ opt = {}
389
+ opt[:default_encodingstyle] = @default_encodingstyle
390
+ opt[:allow_unqualified_element] = @allow_unqualified_element
391
+ opt[:generate_explicit_type] = @generate_explicit_type
392
+ opt
393
+ end
394
+
395
+ def log(sev)
396
+ @logdev.add(sev, nil, self.class) { yield } if @logdev
397
+ end
398
+
399
+ def setup_options
400
+ if opt = Property.loadproperty(::SOAP::PropertyName)
401
+ opt = opt["client"]
402
+ end
403
+ opt ||= Property.new
404
+ opt.add_hook("protocol.mandatorycharset") do |key, value|
405
+ @mandatorycharset = value
406
+ end
407
+ opt.add_hook("protocol.wiredump_file_base") do |key, value|
408
+ @wiredump_file_base = value
409
+ end
410
+ opt["protocol.http.charset"] ||= XSD::Charset.encoding_label
411
+ opt["protocol.http.proxy"] ||= Env::HTTP_PROXY
412
+ opt["protocol.http.no_proxy"] ||= Env::NO_PROXY
413
+ opt
414
+ end
415
+
416
+ class Mapper
417
+ def initialize(elements, types)
418
+ @elements = elements
419
+ @types = types
420
+ end
421
+
422
+ def obj2ele(obj, name)
423
+ if ele = @elements[name]
424
+ _obj2ele(obj, ele)
425
+ elsif type = @types[name]
426
+ obj2type(obj, type)
427
+ else
428
+ raise RuntimeError.new("Cannot find name #{name} in schema.")
429
+ end
430
+ end
431
+
432
+ def ele2obj(ele, *arg)
433
+ raise NotImplementedError.new
434
+ end
435
+
436
+ private
437
+
438
+ def _obj2ele(obj, ele)
439
+ o = nil
440
+ if ele.type
441
+ if type = @types[ele.type]
442
+ o = obj2type(obj, type)
443
+ elsif type = TypeMap[ele.type]
444
+ o = base2soap(obj, type)
445
+ else
446
+ raise RuntimeError.new("Cannot find type #{ele.type}.")
447
+ end
448
+ o.elename = ele.name
449
+ elsif ele.local_complextype
450
+ o = SOAPElement.new(ele.name)
451
+ ele.local_complextype.each_element do |child_name, child_ele|
452
+ o.add(_obj2ele(find_attribute(obj, child_name.name), child_ele))
453
+ end
454
+ else
455
+ raise RuntimeError.new("Illegal schema?")
456
+ end
457
+ o
458
+ end
459
+
460
+ def obj2type(obj, type)
461
+ o = SOAPElement.new(type.name)
462
+ type.each_element do |child_name, child_ele|
463
+ o.add(_obj2ele(find_attribute(obj, child_name.name), child_ele))
464
+ end
465
+ o
466
+ end
467
+
468
+ def _ele2obj(ele)
469
+ raise NotImplementedError.new
470
+ end
471
+
472
+ def base2soap(obj, type)
473
+ soap_obj = nil
474
+ if type <= XSD::XSDString
475
+ soap_obj = type.new(XSD::Charset.is_ces(obj, $KCODE) ?
476
+ XSD::Charset.encoding_conv(obj, $KCODE, XSD::Charset.encoding) : obj)
477
+ else
478
+ soap_obj = type.new(obj)
479
+ end
480
+ soap_obj
481
+ end
482
+
483
+ def find_attribute(obj, attr_name)
484
+ if obj.respond_to?(attr_name)
485
+ obj.__send__(attr_name)
486
+ elsif obj.is_a?(Hash)
487
+ obj[attr_name] || obj[attr_name.intern]
488
+ else
489
+ obj.instance_eval("@#{ attr_name }")
490
+ end
491
+ end
492
+ end
493
+ end
494
+ end
495
+
496
+
497
+ end
498
+
499
+