2000-05-12 13:07:57 +04:00
|
|
|
|
#
|
2008-06-04 13:37:38 +04:00
|
|
|
|
# irb/slex.rb - simple lex analyzer
|
2009-07-07 15:36:20 +04:00
|
|
|
|
# $Release Version: 0.9.6$
|
2000-05-12 13:07:57 +04:00
|
|
|
|
# $Revision$
|
2005-04-13 19:27:09 +04:00
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
#
|
|
|
|
|
# --
|
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
|
#
|
2000-05-12 13:07:57 +04:00
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
require "e2mmap"
|
2005-04-13 19:27:09 +04:00
|
|
|
|
require "irb/notifier"
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
2012-12-13 09:22:30 +04:00
|
|
|
|
# :stopdoc:
|
2005-04-13 19:27:09 +04:00
|
|
|
|
module IRB
|
|
|
|
|
class SLex
|
|
|
|
|
@RCS_ID='-$Id$-'
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
extend Exception2MessageMapper
|
|
|
|
|
def_exception :ErrNodeNothing, "node nothing"
|
|
|
|
|
def_exception :ErrNodeAlreadyExists, "node already exists"
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
2005-04-13 19: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: ")
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
DOUT.level = Notifier::D_NOMSG
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
@head = Node.new("")
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def def_rule(token, preproc = nil, postproc = nil, &block)
|
|
|
|
|
D_DETAIL.pp token
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
postproc = block if block_given?
|
2010-07-11 20:59:10 +04:00
|
|
|
|
create(token, preproc, postproc)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19: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 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def preproc(token, proc)
|
|
|
|
|
node = search(token)
|
|
|
|
|
node.preproc=proc
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
|
|
|
|
#$BMW%A%'%C%/(B?
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def postproc(token)
|
|
|
|
|
node = search(token, proc)
|
|
|
|
|
node.postproc=proc
|
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def search(token)
|
|
|
|
|
@head.search(token.split(//))
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def create(token, preproc = nil, postproc = nil)
|
|
|
|
|
@head.create_subnode(token.split(//), preproc, postproc)
|
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def match(token)
|
|
|
|
|
case token
|
|
|
|
|
when Array
|
|
|
|
|
when String
|
|
|
|
|
return match(token.split(//))
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
2005-04-13 19:27:09 +04:00
|
|
|
|
return @head.match_io(token)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2005-04-13 19:27:09 +04:00
|
|
|
|
ret = @head.match(token)
|
2012-12-21 21:36:14 +04:00
|
|
|
|
D_DETAIL.exec_if{D_DETAIL.printf "match end: %s:%s\n", ret, token.inspect}
|
2005-04-13 19:27:09 +04:00
|
|
|
|
ret
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def inspect
|
|
|
|
|
format("<SLex: @head = %s>", @head.inspect)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
|
# class Node -
|
2005-04-13 19:27:09 +04:00
|
|
|
|
#
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
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
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def search(chrs, opt = nil)
|
|
|
|
|
return self if chrs.empty?
|
|
|
|
|
ch = chrs.shift
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
node.search(chrs, opt)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
2005-04-13 19:27:09 +04:00
|
|
|
|
if opt
|
|
|
|
|
chrs.unshift ch
|
|
|
|
|
self.create_subnode(chrs)
|
|
|
|
|
else
|
|
|
|
|
SLex.fail ErrNodeNothing
|
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def create_subnode(chrs, preproc = nil, postproc = nil)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
if chrs.empty?
|
2005-04-13 19:27:09 +04:00
|
|
|
|
if @postproc
|
|
|
|
|
D_DETAIL.pp node
|
2000-05-12 13:07:57 +04:00
|
|
|
|
SLex.fail ErrNodeAlreadyExists
|
|
|
|
|
else
|
2005-04-13 19:27:09 +04:00
|
|
|
|
D_DEBUG.puts "change abstract node to real node."
|
|
|
|
|
@preproc = preproc
|
|
|
|
|
@postproc = postproc
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2005-04-13 19:27:09 +04:00
|
|
|
|
return self
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2005-04-13 19: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 13:07:57 +04:00
|
|
|
|
else
|
2005-04-13 19: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 13:07:57 +04:00
|
|
|
|
end
|
2005-04-13 19:27:09 +04:00
|
|
|
|
node
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
#
|
|
|
|
|
# chrs: String
|
|
|
|
|
# character array
|
|
|
|
|
# io must have getc()/ungetc(); and ungetc() must be
|
2009-03-06 06:56:38 +03:00
|
|
|
|
# able to be called arbitrary number of times.
|
2005-04-13 19:27:09 +04:00
|
|
|
|
#
|
|
|
|
|
def match(chrs, op = "")
|
|
|
|
|
D_DETAIL.print "match>: ", chrs, "op:", op, "\n"
|
|
|
|
|
if chrs.empty?
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @preproc.nil? || @preproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
DOUT.printf(D_DETAIL, "op1: %s\n", op)
|
2006-08-04 14:06:17 +04:00
|
|
|
|
@postproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
2005-04-13 19:27:09 +04:00
|
|
|
|
ch = chrs.shift
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
if ret = node.match(chrs, op+ch)
|
|
|
|
|
return ret
|
|
|
|
|
else
|
|
|
|
|
chrs.unshift ch
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
DOUT.printf(D_DETAIL, "op2: %s\n", op.inspect)
|
2006-08-04 14:06:17 +04:00
|
|
|
|
ret = @postproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
return ret
|
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
|
|
|
|
chrs.unshift ch
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
DOUT.printf(D_DETAIL, "op3: %s\n", op)
|
2006-08-04 14:06:17 +04:00
|
|
|
|
@postproc.call(op, chrs)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
return ""
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2005-04-13 19:27:09 +04:00
|
|
|
|
def match_io(io, op = "")
|
|
|
|
|
if op == ""
|
|
|
|
|
ch = io.getc
|
|
|
|
|
if ch == nil
|
|
|
|
|
return nil
|
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
2005-04-13 19:27:09 +04:00
|
|
|
|
ch = io.getc_of_rests
|
2000-05-12 13:07:57 +04:00
|
|
|
|
end
|
2005-04-13 19:27:09 +04:00
|
|
|
|
if ch.nil?
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @preproc.nil? || @preproc.call(op, io)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
D_DETAIL.printf("op1: %s\n", op)
|
2006-08-04 14:06:17 +04:00
|
|
|
|
@postproc.call(op, io)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if node = @Tree[ch]
|
|
|
|
|
if ret = node.match_io(io, op+ch)
|
|
|
|
|
ret
|
|
|
|
|
else
|
|
|
|
|
io.ungetc ch
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, io)
|
2005-04-14 00:45:54 +04:00
|
|
|
|
DOUT.exec_if{D_DETAIL.printf "op2: %s\n", op.inspect}
|
2006-08-04 14:06:17 +04:00
|
|
|
|
@postproc.call(op, io)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
|
|
|
|
io.ungetc ch
|
2006-08-04 14:06:17 +04:00
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, io)
|
2005-04-13 19:27:09 +04:00
|
|
|
|
D_DETAIL.printf("op3: %s\n", op)
|
2006-08-04 14:06:17 +04:00
|
|
|
|
@postproc.call(op, io)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-12-13 09:22:30 +04:00
|
|
|
|
# :startdoc:
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
|
|
|
|
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"
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
|
print "case 1:\n"
|
|
|
|
|
print tr.match("="), "\n"
|
|
|
|
|
print "case 2:\n"
|
|
|
|
|
print tr.match("=="), "\n"
|
|
|
|
|
print "case 3:\n"
|
|
|
|
|
print tr.match("=>"), "\n"
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
|
when "2"
|
|
|
|
|
tr = SLex.new
|
|
|
|
|
print "0: ", tr.inspect, "\n"
|
|
|
|
|
tr.def_rule("=") {print "=\n"}
|
|
|
|
|
print "1: ", tr.inspect, "\n"
|
2001-04-30 21:54:55 +04:00
|
|
|
|
tr.def_rule("==", proc{false}) {print "==\n"}
|
2000-05-12 13:07:57 +04:00
|
|
|
|
print "2: ", tr.inspect, "\n"
|
2009-03-06 06:56:38 +03:00
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
|
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 19:27:09 +04:00
|
|
|
|
|