1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/e2mmap.rb

197 lines
4.1 KiB
Ruby
Raw Normal View History

#
# e2mmap.rb - for ruby 1.1
# $Release Version: 2.0$
# $Revision: 1.10 $
# $Date: 1999/02/17 12:33:17 $
# by Keiju ISHITSUKA
#
# --
# Usage:
#
# U1)
# class Foo
# extend Exception2MassageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# ...
# end
#
# U2)
# module Error
# extend Exception2MassageMapper
# def_e2meggage ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# ...
# end
# class Foo
# include Exp
# ...
# end
#
# foo = Foo.new
# foo.Fail ....
#
# U3)
# module Error
# extend Exception2MassageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# ...
# end
# class Foo
# extend Exception2MessageMapper
# include Error
# ...
# end
#
# Foo.Fail NewExceptionClass, arg...
# Foo.Fail ExistingExceptionClass, arg...
#
#
fail "Use Ruby 1.1" if VERSION < "1.1"
module Exception2MessageMapper
@RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
E2MM = Exception2MessageMapper
def E2MM.extend_object(cl)
super
cl.bind(self) unless cl == E2MM
end
# <20><><EFBFBD><EFBFBD><EFBFBD>Ȥθߴ<CEB8><DFB4><EFBFBD><EFBFBD>Τ<EFBFBD><CEA4><EFBFBD><EFBFBD>˻Ĥ<CBBB><C4A4>Ƥ<EFBFBD><C6A4><EFBFBD>.
def E2MM.extend_to(b)
c = eval("self", b)
c.extend(self)
end
def bind(cl)
self.module_eval %[
def Raise(err = nil, *rest)
Exception2MessageMapper.Raise(self.type, err, *rest)
end
alias Fail Raise
def self.append_features(mod)
super
mod.extend Exception2MessageMapper
end
]
end
# Fail(err, *rest)
# err: <09>
# rest: <09><><EFBFBD>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϥ<EFBFBD><CFA4>ѥ<EFBFBD><D1A5><EFBFBD><E1A1BC>
#
def Raise(err = nil, *rest)
E2MM.Raise(self, err, *rest)
end
alias Fail Raise
# <20><><EFBFBD><EFBFBD><EFBFBD>θߴ<CEB8><DFB4><EFBFBD><EFBFBD>Τ<EFBFBD><CEA4><EFBFBD>
alias fail! fail
def fail(err = nil, *rest)
begin
E2MM.Fail(self, err, *rest)
rescue E2MM::ErrNotRegisteredException
super
end
end
class << self
public :fail
end
# def_e2message(c, m)
# c: exception
# m: message_form
# <09>㳰c<E3B3B0>Υ<EFBFBD><CEA5>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m<EFBFBD>Ȥ<EFBFBD><C8A4><EFBFBD>.
#
def def_e2message(c, m)
E2MM.def_e2message(self, c, m)
end
# def_exception(c, m)
# n: exception_name
# m: message_form
# s: <09><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>饹(<28>ǥե<C7A5><D5A5><EFBFBD><EFBFBD><EFBFBD>: StandardError)
# <09>㳰̾``c''<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD>Υ<EFBFBD><CEA5>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m<EFBFBD>Ȥ<EFBFBD><C8A4><EFBFBD>.
#
def def_exception(n, m, s = StandardError)
E2MM.def_exception(self, n, m, s)
end
#
# Private definitions.
#
# {[class, exp] => message, ...}
@MessageMap = {}
# E2MM.def_exception(k, e, m)
# k: <20><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
# e: exception
# m: message_form
# <09>㳰c<E3B3B0>Υ<EFBFBD><CEA5>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m<EFBFBD>Ȥ<EFBFBD><C8A4><EFBFBD>.
#
def E2MM.def_e2message(k, c, m)
E2MM.instance_eval{@MessageMap[[k, c]] = m}
c
end
# E2MM.def_exception(k, c, m)
# k: <20><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
# n: exception_name
# m: message_form
# s: <09><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>饹(<28>ǥե<C7A5><D5A5><EFBFBD><EFBFBD><EFBFBD>: StandardError)
# <09>㳰̾``c''<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD>Υ<EFBFBD><CEA5>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m<EFBFBD>Ȥ<EFBFBD><C8A4><EFBFBD>.
#
def E2MM.def_exception(k, n, m, s = StandardError)
n = n.id2name if n.kind_of?(Fixnum)
e = Class.new(s)
E2MM.instance_eval{@MessageMap[[k, e]] = m}
k.const_set(n, e)
end
# Fail(klass, err, *rest)
# klass: <20><EFBFBD><E3B3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4><EFBFBD>
# err: <09>
# rest: <09><><EFBFBD>å<EFBFBD><C3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϥ<EFBFBD><CFA4>ѥ<EFBFBD><D1A5><EFBFBD><E1A1BC>
#
def E2MM.Raise(klass = E2MM, err = nil, *rest)
if form = e2mm_message(klass, err)
$! = err.new(sprintf(form, *rest))
$@ = caller(1) if $@.nil?
#p $@
#p __FILE__
$@.shift if $@[0] =~ /^#{Regexp.quote(__FILE__)}:/
raise
else
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
end
end
class <<E2MM
alias Fail Raise
end
def E2MM.e2mm_message(klass, exp)
for c in klass.ancestors
if mes = @MessageMap[[c,exp]]
#p mes
m = klass.instance_eval('"' + mes + '"')
return m
end
end
nil
end
class <<self
alias message e2mm_message
end
E2MM.def_exception(E2MM,
:ErrNotRegisteredException,
"not registerd exception(%s)")
end