On Saturday, November 23, 2019 at 4:46:08 AM UTC-6, Edward K. Ream wrote:

It's just a bit tricky to look ahead in a generator. So instead of the code 
> shown above in do_If, the visitor call self.peek() and self.advance(),  
> defined something like this (completely untested):
>

> # Use lists for debugging, generators for production.
>
> peek_list = [] # Must be a list.
>
> def peek(self):
>     if instance(self.token_gen, list):
>         return self.token_gen[self.token_gen_index]
>     if not self.peek_list:
>         self.peek_list = [self.token_gen.next] # or is it next()?
>     return self.peek_list[-1]
>     
> def advance(self):
>     if instance(self.token_gen, list):
>         self.token_gen_index += 1
>     self.peek_list = []
>

Heh.  The code I wrote at 3 in the morning looks better than any of the stack 
overflow answers <https://stackoverflow.com/questions/2425270>.

True, the code above works only for a single generator. The methods will be 
renamed to *if_gen_peek* and *if_gen_next*.

Similarly, the tog.sync method must peek arbitrarily many times into the 
full token generator, so it will use the *all_tokens_peek* and 
*all_tokens_next* methods.  Likewise, if the string_tokens and 
significant_tokens generators require lookahead I'll define corresponding 
peek and next methods.

In short, a general solution is not needed.  A per-generator solution seems 
simpler and better.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/b6d03153-cdf1-444e-9e7a-bc3c7ecd9fb9%40googlegroups.com.

Reply via email to