Synopsis - Synopsis/PTree/Display.hh

File: Synopsis/PTree/Display.hh

      1//
      2// Copyright (C) 2004 Stefan Seefeld
      3// All rights reserved.
      4// Licensed to the public under the terms of the GNU LGPL (>= 2),
      5// see the file COPYING for details.
      6//
      7#ifndef Synopsis_PTree_Display_hh_
      8#define Synopsis_PTree_Display_hh_
      9
     10#include <Synopsis/PTree.hh>
     11
     12namespace Synopsis
     13{
     14namespace PTree
     15{
     16
     17//. The Display class provides an annotated view of the ptree,
     18//. for debugging purposes
     19class Display : private Visitor
     20{
     21public:
     22  Display(std::ostream &os, bool encoded);
     23
     24  void display(Node const *);
     25
     26  virtual void visit(Atom *);
     27  virtual void visit(List *);
     28  // atoms...
     29  virtual void visit(DupAtom *);
     30  // ...lists...
     31  virtual void visit(Brace *);
     32  virtual void visit(Block *b) { visit(static_cast<Brace *>(b));}
     33  virtual void visit(ClassBody *b) { visit(static_cast<Brace *>(b));}
     34  virtual void visit(Declarator *l) { print_encoded(l);}
     35  virtual void visit(Name *l) { print_encoded(l);}
     36  virtual void visit(FstyleCastExpr *l) { print_encoded(l);}
     37private:
     38  void newline();
     39  bool too_deep();
     40  void print_encoded(List *);
     41
     42  std::ostream &my_os;
     43  size_t        my_indent;
     44  bool          my_encoded;
     45};
     46
     47class RTTIDisplay : private Visitor
     48{
     49public:
     50  RTTIDisplay(std::ostream &os, bool encoded);
     51
     52  void display(Node const *);
     53
     54  virtual void visit(Atom *);
     55  virtual void visit(List *);
     56  virtual void visit(DupAtom *);
     57private:
     58  void newline();
     59
     60  std::ostream &my_os;
     61  size_t        my_indent;
     62  bool          my_encoded;
     63};
     64
     65class DotFileGenerator : public PTree::Visitor
     66{
     67public:
     68  DotFileGenerator(std::ostream &);
     69  void write(PTree::Node const *ptree);
     70private:
     71  virtual void visit(PTree::Atom *a);
     72  virtual void visit(PTree::List *l);
     73
     74  std::ostream &my_os;
     75};
     76
     77//. Display the given parse tree segment on the given output stream.
     78//. If 'encoded' is set to 'true', print encoded names / types
     79//. on appropriate nodes. If 'typeinfo' is set to 'true', print
     80//. the class names of the nodes.
     81inline void display(Node const *node, std::ostream &os,
     82		    bool encoded = false, bool typeinfo = false)
     83{
     84  if (typeinfo)
     85  {
     86    RTTIDisplay d(os, encoded);
     87    d.display(node);
     88  }
     89  else
     90  {
     91    Display d(os, encoded);
     92    d.display(node);
     93  }
     94}
     95
     96//. Generate a dot file for the given parse tree segment.
     97inline void generate_dot_file(Node const *node, std::ostream &os)
     98{
     99  DotFileGenerator generator(os);
    100  generator.write(node);
    101}
    102
    103}
    104}
    105
    106#endif

Generated on Thu Apr 16 16:28:02 2009 by
synopsis (version devel)