2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2008-06-04 05:37:38 -04:00
|
|
|
# irb/slex.rb - simple lex analyzer
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
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
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
# :stopdoc:
|
2005-04-13 11:27:09 -04:00
|
|
|
module IRB
|
|
|
|
class SLex
|
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: ")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
DOUT.level = Notifier::D_NOMSG
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@head = Node.new("")
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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?
|
2010-07-11 12:59:10 -04:00
|
|
|
create(token, preproc, postproc)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def def_rules(*tokens, &block)
|
|
|
|
if block_given?
|
2014-08-08 21:36:49 -04:00
|
|
|
p = block
|
2005-04-13 11:27:09 -04:00
|
|
|
end
|
|
|
|
for token in tokens
|
2014-08-08 21:36:49 -04:00
|
|
|
def_rule(token, nil, p)
|
2005-04-13 11:27:09 -04:00
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2017-01-08 21:45:28 -05:00
|
|
|
# need a check?
|
2005-04-13 11:27:09 -04:00
|
|
|
def postproc(token)
|
|
|
|
node = search(token, proc)
|
|
|
|
node.postproc=proc
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def match(token)
|
|
|
|
case token
|
|
|
|
when Array
|
|
|
|
when String
|
2014-08-08 21:36:49 -04:00
|
|
|
return match(token.split(//))
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -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)
|
2012-12-21 12:36:14 -05:00
|
|
|
D_DETAIL.exec_if{D_DETAIL.printf "match end: %s:%s\n", ret, token.inspect}
|
2005-04-13 11:27:09 -04:00
|
|
|
ret
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def inspect
|
|
|
|
format("<SLex: @head = %s>", @head.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# class Node -
|
2005-04-13 11: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)
|
2014-08-08 21:36:49 -04:00
|
|
|
@Tree = {}
|
|
|
|
@preproc = preproc
|
|
|
|
@postproc = postproc
|
2005-04-13 11:27:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :preproc
|
|
|
|
attr_accessor :postproc
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def search(chrs, opt = nil)
|
2014-08-08 21:36:49 -04:00
|
|
|
return self if chrs.empty?
|
|
|
|
ch = chrs.shift
|
|
|
|
if node = @Tree[ch]
|
|
|
|
node.search(chrs, opt)
|
|
|
|
else
|
|
|
|
if opt
|
|
|
|
chrs.unshift ch
|
|
|
|
self.create_subnode(chrs)
|
|
|
|
else
|
|
|
|
SLex.fail ErrNodeNothing
|
|
|
|
end
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def create_subnode(chrs, preproc = nil, postproc = nil)
|
2014-08-08 21:36:49 -04:00
|
|
|
if chrs.empty?
|
|
|
|
if @postproc
|
|
|
|
D_DETAIL.pp node
|
|
|
|
SLex.fail ErrNodeAlreadyExists
|
|
|
|
else
|
|
|
|
D_DEBUG.puts "change abstract node to real node."
|
|
|
|
@preproc = preproc
|
|
|
|
@postproc = postproc
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-08-08 21:36:49 -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
|
|
|
|
else
|
|
|
|
if chrs.empty?
|
|
|
|
node = Node.new(preproc, postproc)
|
|
|
|
else
|
|
|
|
node = Node.new
|
|
|
|
node.create_subnode(chrs, preproc, postproc)
|
|
|
|
end
|
|
|
|
@Tree[ch] = node
|
|
|
|
end
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
# able to be called arbitrary number of times.
|
2005-04-13 11:27:09 -04:00
|
|
|
#
|
|
|
|
def match(chrs, op = "")
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
|
|
|
else
|
|
|
|
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
|
|
|
|
else
|
|
|
|
chrs.unshift ch
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, chrs)
|
|
|
|
DOUT.printf(D_DETAIL, "op3: %s\n", op)
|
|
|
|
@postproc.call(op, chrs)
|
|
|
|
return ""
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def match_io(io, op = "")
|
2014-08-08 21:36:49 -04:00
|
|
|
if op == ""
|
|
|
|
ch = io.getc
|
|
|
|
if ch == nil
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
ch = io.getc_of_rests
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
DOUT.exec_if{D_DETAIL.printf "op2: %s\n", op.inspect}
|
|
|
|
@postproc.call(op, io)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
io.ungetc ch
|
|
|
|
if @postproc and @preproc.nil? || @preproc.call(op, io)
|
|
|
|
D_DETAIL.printf("op3: %s\n", op)
|
|
|
|
@postproc.call(op, io)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-13 00:22:30 -05:00
|
|
|
# :startdoc:
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
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-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05: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-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05: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 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"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05: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
|