Class: IRB::SLex::Node

Inherits:
Object show all
Defined in:
lib/irb/slex.rb

Overview


class Node -

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(preproc = nil, postproc = nil) ⇒ Node

if postproc is nil, this node is an abstract node. if postproc is non-nil, this node is a real node.



95
96
97
98
99
# File 'lib/irb/slex.rb', line 95

def initialize(preproc = nil, postproc = nil)
	@Tree = {}
	@preproc = preproc
	@postproc = postproc
end

Instance Attribute Details

#postprocObject

Returns the value of attribute postproc



102
103
104
# File 'lib/irb/slex.rb', line 102

def postproc
  @postproc
end

#preprocObject

Returns the value of attribute preproc



101
102
103
# File 'lib/irb/slex.rb', line 101

def preproc
  @preproc
end

Instance Method Details

#create_subnode(chrs, preproc = nil, postproc = nil) ⇒ Object



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
# File 'lib/irb/slex.rb', line 119

def create_subnode(chrs, preproc = nil, postproc = nil)
	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

	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
end

#match(chrs, op = "") ⇒ Object

chrs: String

character array
io must have getc()/ungetc(); and ungetc() must be
able to be called arbitrary number of times.


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
# File 'lib/irb/slex.rb', line 167

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
	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
end

#match_io(io, op = "") ⇒ Object



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
# File 'lib/irb/slex.rb', line 204

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)
	    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
end

#search(chrs, opt = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/irb/slex.rb', line 104

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