summaryrefslogtreecommitdiff
path: root/lib/irb/slex.rb
blob: 1cf23255ad1bd1d3a6bfed9e5a4944a8a89edf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#
#   irb/slex.rb - symple lex analizer
#   	$Release Version: 0.9$
#   	$Revision$
#   	$Date$
#   	by Keiju ISHITSUKA(keiju@ishituska.com)
#
# --
#
#   
#

require "e2mmap"

class SLex
  @RCS_ID='-$Id$-'

  extend Exception2MessageMapper
  def_exception :ErrNodeNothing, "node nothing"
  def_exception :ErrNodeAlreadyExists, "node already exists"

  class << self
    attr_accessor :debug_level
    def debug?
      debug_level > 0
    end
  end
  @debug_level = 0

  def initialize
    @head = Node.new("")
  end
  
  def def_rule(token, preproc = nil, postproc = nil)
    #      print node.inspect, "\n" if SLex.debug?
    postproc = proc if iterator?
    node = create(token, preproc, postproc)
  end
  
  def def_rules(*tokens)
    if iterator?
      p = proc
    end
    for token in tokens
      def_rule(token, nil, p)
    end
  end
  
  def preporc(token, proc)
    node = search(token)
    node.preproc=proc
  end
  
  def postproc(token)
    node = search(token, proc)
    node.postproc=proc
  end
  
  def search(token)
    @head.search(token.split(//))
  end

  def create(token, preproc = nil, postproc = nil)
    @head.create_subnode(token.split(//), preproc, postproc)
  end
  
  def match(token)
    case token
    when Array
    when String
      return match(token.split(//))
    else
      return @head.match_io(token)
    end
    ret = @head.match(token)
    printf "match end: %s:%s", ret, token.inspect if SLex.debug?
    ret
  end
  
  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)
      else
	if opt
	  chrs.unshift ch
	  self.create_subnode(chrs)
	else
	  SLex.fail ErrNodeNothing
	end
      end
    end
    
    def create_subnode(chrs, preproc = nil, postproc = nil)
      if chrs.empty?
	if @postproc
	  p node
	  SLex.fail ErrNodeAlreadyExists
	else
	  print "Warn: change abstruct node to real node\n" if SLex.debug?
	  @preproc = preproc
	  @postproc = postproc
	end
	return self
      end
      
      ch = chrs.shift
      if node = @Tree[ch]
	if chrs.empty?
	  if node.postproc
	    p node
	    p self
	    p ch
	    p chrs
	    SLex.fail ErrNodeAlreadyExists
	  else
	    print "Warn: change abstruct node to real node\n" if SLex.debug?
	    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
    end

    #
    # 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 = "")
      print "match>: ", chrs, "op:", op, "\n" if SLex.debug?
      if chrs.empty?
	if @preproc.nil? || @preproc.call(op, chrs)
	  printf "op1: %s\n", op if SLex.debug?
	  @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)
	      printf "op2: %s\n", op.inspect if SLex.debug?
	      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)
	    printf "op3: %s\n", op if SLex.debug?
	    @postproc.call(op, chrs)
	    return ""
	  else
	    return nil
	  end
	end
      end
    end

    def match_io(io, op = "")
      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)
	  printf "op1: %s\n", op if SLex.debug?
	  @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)
	      printf "op2: %s\n", op.inspect if SLex.debug?
	      @postproc.call(op, io)
	    else
	      nil
	    end
	  end
	else
	  io.ungetc ch
	  if @postproc and @preproc.nil? || @preproc.call(op, io)
	    printf "op3: %s\n", op if SLex.debug?
	    @postproc.call(op, io)
	  else
	    nil
	  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"
    tr.def_rule("==", proc{false}) {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"
  end
  exit
end