Re: [RBMetacode] Need some code design help
Status: Planning
Brought to you by:
jstrout
From: Joe S. <jo...@st...> - 2008-03-14 15:12:00
|
On Mar 14, 2008, at 8:38 AM, Thomas Tempelmann wrote: > Now I like to add Find & Replace functionality and I am looking for > suggestions on how to do this best. > > To search, I need to iterate over all items. Currently, not every > text item > of interest has its own node in the tree. Instead, as explained > above, the > name/label is a property of any node's base class, and some classes > have > accessor methods for reading and settings source, declaration and > notes. > > To iterate over these, I'd currently have to iterate over the > nodes, then > add special handling for the special properties. But how would the > iterator > remember where it's at? If I code it so that it uses a callback > (delegate) > function to handle the finding, it would implicitly remember where > it's at, > but what if the callback function decides to alter the tree so that > the > iteration loop is now working on obsolete items? > > Therefore, I believe it's better to have a "smarter" iterator that > one calls > from one's own loop, getting the next found item, then process this > item, > then proceed in the loop to get the next item from the iterator. > But then, > the iterator has to know where it's at. See the problem? If I understand you properly, I think the find/replace functionality should be a visitor (see SyntaxVisitor and its subclasses in ParserProto for an example). And to avoid the problem of the tree changing while you're traversing it, do it in two stages: first, gather a list of all the matches; and second (if the user wants to replace all without seeing them first), do the replacements. HTH, - Joe |