From: Eric Hodel Date: 2006-11-29T10:17:02+09:00 Subject: Re: "yield" and "old-way iteration" On Nov 28, 2006, at 1420 , Bjarke Walling wrote: > I am writing a lexer and parser for a small language I have created. > The first part splitting some input into tokens was easy to write > using > "yield" (20 lines or so). I was actually a little overwhelmed how easy > it was. The next part is to examine these tokens and parse them into > language structures. I want to create a class with "current" and > "next" > methods to get the current token and fetch the next (advance the > pointer). I have solved it by using the first lexer yielding tokens > and > collecting them in an array. Afterwards I can fetch tokens from the > array. But could it be done in a smarter way? > > My code is like this: > > class FirstParse include Enumerable > def initialize > ... > end > def each > ... yield tokens ... > end > end > > class SecondParse > def initialize def initialize(tokens) > @index = 0 @tokens = tokens > end > def current > @tokens[@index] > end > def next > @index++ > self.current > end > def read_structure1 > ... read structures ... > end > def read_structure2 > ... read structures ... > end > end parser = SecondParse.new FirstParse.new.to_a Passing in an Array of tokens makes it easier to test, too. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net I LIT YOUR GEM ON FIRE!