| Class | IRB::SLex::Node |
| In: |
lib/irb/slex.rb
|
| Parent: | Object |
| postproc | [RW] | |
| preproc | [RW] |
if postproc is nil, this node is an abstract node. if postproc is non-nil, this node is a real node.
# File lib/irb/slex.rb, line 95
95: def initialize(preproc = nil, postproc = nil)
96: @Tree = {}
97: @preproc = preproc
98: @postproc = postproc
99: end
# File lib/irb/slex.rb, line 119
119: def create_subnode(chrs, preproc = nil, postproc = nil)
120: if chrs.empty?
121: if @postproc
122: D_DETAIL.pp node
123: SLex.fail ErrNodeAlreadyExists
124: else
125: D_DEBUG.puts "change abstract node to real node."
126: @preproc = preproc
127: @postproc = postproc
128: end
129: return self
130: end
131:
132: ch = chrs.shift
133: if node = @Tree[ch]
134: if chrs.empty?
135: if node.postproc
136: DebugLogger.pp node
137: DebugLogger.pp self
138: DebugLogger.pp ch
139: DebugLogger.pp chrs
140: SLex.fail ErrNodeAlreadyExists
141: else
142: D_WARN.puts "change abstract node to real node"
143: node.preproc = preproc
144: node.postproc = postproc
145: end
146: else
147: node.create_subnode(chrs, preproc, postproc)
148: end
149: else
150: if chrs.empty?
151: node = Node.new(preproc, postproc)
152: else
153: node = Node.new
154: node.create_subnode(chrs, preproc, postproc)
155: end
156: @Tree[ch] = node
157: end
158: node
159: end
chrs: String
character array
io must have getc()/ungetc(); and ungetc() must be
able to be called arbitrary number of times.
# File lib/irb/slex.rb, line 167
167: def match(chrs, op = "")
168: D_DETAIL.print "match>: ", chrs, "op:", op, "\n"
169: if chrs.empty?
170: if @preproc.nil? || @preproc.call(op, chrs)
171: DOUT.printf(D_DETAIL, "op1: %s\n", op)
172: @postproc.call(op, chrs)
173: else
174: nil
175: end
176: else
177: ch = chrs.shift
178: if node = @Tree[ch]
179: if ret = node.match(chrs, op+ch)
180: return ret
181: else
182: chrs.unshift ch
183: if @postproc and @preproc.nil? || @preproc.call(op, chrs)
184: DOUT.printf(D_DETAIL, "op2: %s\n", op.inspect)
185: ret = @postproc.call(op, chrs)
186: return ret
187: else
188: return nil
189: end
190: end
191: else
192: chrs.unshift ch
193: if @postproc and @preproc.nil? || @preproc.call(op, chrs)
194: DOUT.printf(D_DETAIL, "op3: %s\n", op)
195: @postproc.call(op, chrs)
196: return ""
197: else
198: return nil
199: end
200: end
201: end
202: end
# File lib/irb/slex.rb, line 204
204: def match_io(io, op = "")
205: if op == ""
206: ch = io.getc
207: if ch == nil
208: return nil
209: end
210: else
211: ch = io.getc_of_rests
212: end
213: if ch.nil?
214: if @preproc.nil? || @preproc.call(op, io)
215: D_DETAIL.printf("op1: %s\n", op)
216: @postproc.call(op, io)
217: else
218: nil
219: end
220: else
221: if node = @Tree[ch]
222: if ret = node.match_io(io, op+ch)
223: ret
224: else
225: io.ungetc ch
226: if @postproc and @preproc.nil? || @preproc.call(op, io)
227: DOUT.exec_if{D_DETAIL.printf "op2: %s\n", op.inspect}
228: @postproc.call(op, io)
229: else
230: nil
231: end
232: end
233: else
234: io.ungetc ch
235: if @postproc and @preproc.nil? || @preproc.call(op, io)
236: D_DETAIL.printf("op3: %s\n", op)
237: @postproc.call(op, io)
238: else
239: nil
240: end
241: end
242: end
243: end
# File lib/irb/slex.rb, line 104
104: def search(chrs, opt = nil)
105: return self if chrs.empty?
106: ch = chrs.shift
107: if node = @Tree[ch]
108: node.search(chrs, opt)
109: else
110: if opt
111: chrs.unshift ch
112: self.create_subnode(chrs)
113: else
114: SLex.fail ErrNodeNothing
115: end
116: end
117: end