2000-05-12 05:07:57 -04:00
|
|
|
|
#
|
2001-04-30 13:54:55 -04:00
|
|
|
|
# irb/slex.rb - symple lex analizer
|
2005-04-13 11:27:09 -04:00
|
|
|
|
# $Release Version: 0.9.5$
|
2000-05-12 05:07:57 -04:00
|
|
|
|
# $Revision$
|
|
|
|
|
# $Date$
|
2005-04-13 11:27:09 -04:00
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
#
|
|
|
|
|
# --
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
require "e2mmap"
|
2005-04-13 11:27:09 -04:00
|
|
|
|
require "irb/notifier"
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
module IRB
|
|
|
|
|
class SLex
|
|
|
|
|
@RCS_ID='-$Id$-'
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
extend Exception2MessageMapper
|
|
|
|
|
def_exception :ErrNodeNothing, "node nothing"
|
|
|
|
|
def_exception :ErrNodeAlreadyExists, "node already exists"
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
DOUT = Notifier::def_notifier("SLex::")
|
|
|
|
|
D_WARN = DOUT::def_notifier(1, "Warn: ")
|
|
|
|
|
D_DEBUG = DOUT::def_notifier(2, "Debug: ")
|
|
|
|
|
D_DETAIL = DOUT::def_notifier(4, "Detail: ")
|
|
|
|
|
|
|
|
|
|
DOUT.level = Notifier::D_NOMSG
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
@head = Node.new("")
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|
|
|
|
|
def def_rule(token, preproc = nil, postproc = nil, &block)
|
|
|
|
|
D_DETAIL.pp token
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
postproc = block if block_given?
|
|
|
|
|
node = create(token, preproc, postproc)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|
|
|
|
|
def def_rules(*tokens, &block)
|
|
|
|
|
if block_given?
|
|
|
|
|
p = block
|
|
|
|
|
end
|
|
|
|
|
for token in tokens
|
|
|
|
|
def_rule(token, nil, p)
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|
|
|
|
|
def preproc(token, proc)
|
|
|
|
|
node = search(token)
|
|
|
|
|
node.preproc=proc
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|
|
|
|
|
#$BMW%A%'%C%/(B?
|
|
|
|
|
def postproc(token)
|
|
|
|
|
node = search(token, proc)
|
|
|
|
|
node.postproc=proc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def search(token)
|
|
|
|
|
@head.search(token.split(//))
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
def create(token, preproc = nil, postproc = nil)
|
|
|
|
|
@head.create_subnode(token.split(//), preproc, postproc)
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
def match(token)
|
|
|
|
|
case token
|
|
|
|
|
when Array
|
|
|
|
|
when String
|
|
|
|
|
return match(token.split(//))
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
return @head.match_io(token)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
ret = @head.match(token)
|
|
|
|
|
D_DETAIL.exec_if{D_DEATIL.printf "match end: %s:%s\n", ret, token.inspect}
|
|
|
|
|
ret
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
def inspect
|
|
|
|
|
format("<SLex: @head = %s>", @head.inspect)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
#
|
|
|
|
|
# class Node -
|
|
|
|
|
#
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
class Node
|
|
|
|
|
# if postproc is nil, this node is an abstract node.
|
|
|
|
|
# if postproc is non-nil, this node is a real node.
|
|
|
|
|
def initialize(preproc = nil, postproc = nil)
|
|
|
|
|
@Tree = {}
|
|
|
|
|
@preproc = preproc
|
|
|
|
|
@postproc = postproc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
attr_accessor :preproc
|
|
|
|
|
attr_accessor :postproc
|
|
|
|
|
|
|
|
|
|
def search(chrs, opt = nil)
|
|
|
|
|
return self if chrs.empty?
|
|
|
|
|
ch = chrs.shift
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
node.search(chrs, opt)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
if opt
|
|
|
|
|
chrs.unshift ch
|
|
|
|
|
self.create_subnode(chrs)
|
|
|
|
|
else
|
|
|
|
|
SLex.fail ErrNodeNothing
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
def create_subnode(chrs, preproc = nil, postproc = nil)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
if chrs.empty?
|
2005-04-13 11:27:09 -04:00
|
|
|
|
if @postproc
|
|
|
|
|
D_DETAIL.pp node
|
2000-05-12 05:07:57 -04:00
|
|
|
|
SLex.fail ErrNodeAlreadyExists
|
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
D_DEBUG.puts "change abstract node to real node."
|
|
|
|
|
@preproc = preproc
|
|
|
|
|
@postproc = postproc
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
return self
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|
|
|
|
|
ch = chrs.shift
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
if chrs.empty?
|
|
|
|
|
if node.postproc
|
|
|
|
|
DebugLogger.pp node
|
|
|
|
|
DebugLogger.pp self
|
|
|
|
|
DebugLogger.pp ch
|
|
|
|
|
DebugLogger.pp chrs
|
|
|
|
|
SLex.fail ErrNodeAlreadyExists
|
|
|
|
|
else
|
|
|
|
|
D_WARN.puts "change abstract node to real node"
|
|
|
|
|
node.preproc = preproc
|
|
|
|
|
node.postproc = postproc
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
node.create_subnode(chrs, preproc, postproc)
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
if chrs.empty?
|
|
|
|
|
node = Node.new(preproc, postproc)
|
|
|
|
|
else
|
|
|
|
|
node = Node.new
|
|
|
|
|
node.create_subnode(chrs, preproc, postproc)
|
|
|
|
|
end
|
|
|
|
|
@Tree[ch] = node
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
node
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
#
|
|
|
|
|
# chrs: String
|
|
|
|
|
# character array
|
|
|
|
|
# io must have getc()/ungetc(); and ungetc() must be
|
|
|
|
|
# able to be called arbitrary number of times.
|
|
|
|
|
#
|
|
|
|
|
def match(chrs, op = "")
|
|
|
|
|
D_DETAIL.print "match>: ", chrs, "op:", op, "\n"
|
|
|
|
|
if chrs.empty?
|
|
|
|
|
if @preproc.nil? || @preproc.call(op, chrs)
|
|
|
|
|
DOUT.printf(D_DETAIL, "op1: %s\n", op)
|
|
|
|
|
@postproc.call(op, chrs)
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
ch = chrs.shift
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
if ret = node.match(chrs, op+ch)
|
|
|
|
|
return ret
|
|
|
|
|
else
|
|
|
|
|
chrs.unshift ch
|
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, chrs)
|
|
|
|
|
DOUT.printf(D_DETAIL, "op2: %s\n", op.inspect)
|
|
|
|
|
ret = @postproc.call(op, chrs)
|
|
|
|
|
return ret
|
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
|
|
|
|
chrs.unshift ch
|
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, chrs)
|
2005-04-13 11:27:09 -04:00
|
|
|
|
DOUT.printf(D_DETAIL, "op3: %s\n", op)
|
|
|
|
|
@postproc.call(op, chrs)
|
|
|
|
|
return ""
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
|
def match_io(io, op = "")
|
|
|
|
|
if op == ""
|
|
|
|
|
ch = io.getc
|
|
|
|
|
if ch == nil
|
|
|
|
|
return nil
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
|
ch = io.getc_of_rests
|
2000-05-12 05:07:57 -04:00
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
if ch.nil?
|
|
|
|
|
if @preproc.nil? || @preproc.call(op, io)
|
|
|
|
|
D_DETAIL.printf("op1: %s\n", op)
|
|
|
|
|
@postproc.call(op, io)
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
if ret = node.match_io(io, op+ch)
|
|
|
|
|
ret
|
|
|
|
|
else
|
|
|
|
|
io.ungetc ch
|
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, io)
|
2005-04-13 16:45:54 -04:00
|
|
|
|
DOUT.exec_if{D_DETAIL.printf "op2: %s\n", op.inspect}
|
2005-04-13 11:27:09 -04:00
|
|
|
|
@postproc.call(op, io)
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
else
|
|
|
|
|
io.ungetc ch
|
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, io)
|
2005-04-13 11:27:09 -04:00
|
|
|
|
D_DETAIL.printf("op3: %s\n", op)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
@postproc.call(op, io)
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
|
# Tracer.on
|
|
|
|
|
case $1
|
|
|
|
|
when "1"
|
|
|
|
|
tr = SLex.new
|
|
|
|
|
print "0: ", tr.inspect, "\n"
|
|
|
|
|
tr.def_rule("=") {print "=\n"}
|
|
|
|
|
print "1: ", tr.inspect, "\n"
|
|
|
|
|
tr.def_rule("==") {print "==\n"}
|
|
|
|
|
print "2: ", tr.inspect, "\n"
|
|
|
|
|
|
|
|
|
|
print "case 1:\n"
|
|
|
|
|
print tr.match("="), "\n"
|
|
|
|
|
print "case 2:\n"
|
|
|
|
|
print tr.match("=="), "\n"
|
|
|
|
|
print "case 3:\n"
|
|
|
|
|
print tr.match("=>"), "\n"
|
|
|
|
|
|
|
|
|
|
when "2"
|
|
|
|
|
tr = SLex.new
|
|
|
|
|
print "0: ", tr.inspect, "\n"
|
|
|
|
|
tr.def_rule("=") {print "=\n"}
|
|
|
|
|
print "1: ", tr.inspect, "\n"
|
2001-04-30 13:54:55 -04:00
|
|
|
|
tr.def_rule("==", proc{false}) {print "==\n"}
|
2000-05-12 05:07:57 -04:00
|
|
|
|
print "2: ", tr.inspect, "\n"
|
|
|
|
|
|
|
|
|
|
print "case 1:\n"
|
|
|
|
|
print tr.match("="), "\n"
|
|
|
|
|
print "case 2:\n"
|
|
|
|
|
print tr.match("=="), "\n"
|
|
|
|
|
print "case 3:\n"
|
|
|
|
|
print tr.match("=>"), "\n"
|
|
|
|
|
end
|
|
|
|
|
exit
|
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
|
|