From: Stefan R. <sr...@ma...> - 2003-04-23 14:12:19
|
On Mon, Apr 14, 2003 at 04:32:57AM -0700, James Michael DuPont wrote: > --- Stefan Reuther <sr...@ma...> wrote: > > Another possibility would be to put that knowledge into the > > Walker interface. Here, we have such a thing for > > PtreeDeclaration. Depending upon whether a PtreeDeclaration is a > > class declaration ("class foo;"), a name declaration ("int x;") > > or a function implementation ("void x() { }"), it nicely > > dissects the tree node and calls different functions. "Class" > > and "ClassWalker" already seem to do some other parts of this. > > The walker has only one problem. > It does not only walk, but also has to interpret things. > It has to know how to extract the data out of the parser > > How do you propose to hide the details of the parser from the walker? I would not hide the details. Someone has to know about the Ptree structure, right? My point was making Walker the one who knows it. Like this: // users can override this method virtual Ptree* TranslateNamespaceCooked(Ptree* keyword, Ptree* name, Ptree* content); // this method is called by PtreeNamespace::Translate virtual Ptree* TranslateNamespaceRaw(PtreeNamespace* p) { return TranslateNamespaceCooked(p->Car() /* "namespace" */, p->Second() /* the namespace name */, p->Third() /* the contents */); } I'm not exactly sure, but maybe that's not too flexible (one can't inspect a given Ptree except by making a custom Walker and calling it). The other solution, class PtreeNamespace : public Ptree { public: Ptree* GetKeyword() { return Car(); } Ptree* GetName() { return Second(); } Ptree* GetContent() { return Third(); } }; puts that knowledge into the Ptree itself, which I like, but I feel there are performance problems for things like "GetName" in a PtreeDeclarator (it has to skip over a number of "*", "&", "class::*" and cv-qualifiers to find the name or sub-declarator). But maybe that's not at all a problem (caching?). Using handle classes (Grzegorz' way) would definitely work nice, but it's a lot of work to implement it. Stefan |