summaryrefslogtreecommitdiff
path: root/contrib/utility/Example/Introspection/Traversal/Traversal.hpp
blob: 7ee84523fc009595dcca5c95c7b90c108202d07c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// file      : Example/Introspection/Traversal/Traversal.hpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#ifndef TRAVERSAL_HPP
#define TRAVERSAL_HPP

#include <map>
#include <iostream>

#include "Utility/Introspection/Introspection.hpp"

#include "SyntaxTree.hpp"

namespace Traversal
{
  class Traverser;

  //
  //
  //
  class Dispatcher
  {
  public:
    virtual
    ~Dispatcher ()
    {
    }

    virtual void
    dispatch (SyntaxTree::Node* n);

  protected:
    void
    map (Utility::Introspection::TypeId id, Traverser* t)
    {
      traversal_map_[id] = t;
    }

  private:
    typedef
    std::map<Utility::Introspection::TypeId, Traverser*>
    TraversalMap;

    TraversalMap traversal_map_;
  };


  //
  //
  //
  class Traverser : public virtual Dispatcher
  {
  public:
    virtual void
    traverse (SyntaxTree::Node* n) = 0;
  };

  //
  //
  //
  struct Node : Traverser
  {
    Node ()
    {
      map (typeid (SyntaxTree::Node), this);
    }

    virtual void
    traverse (SyntaxTree::Node*)
    {
      std::cerr << "node" << std::endl;
    }
  };


  //
  //
  //
  struct Declaration : Traverser
  {
    Declaration ()
    {
      map (typeid (SyntaxTree::Declaration), this);
    }

    virtual void
    traverse (SyntaxTree::Node*)
    {
      std::cerr << "declaration" << std::endl;
    }
  };

  //
  //
  //
  struct Scope : Traverser
  {
    Scope ()
    {
      map (typeid (SyntaxTree::Scope), this);
    }

    virtual void
    traverse (SyntaxTree::Node* n)
    {
      std::cerr << "scope" << std::endl;

      SyntaxTree::Scope* s = dynamic_cast<SyntaxTree::Scope*> (n);

      for (SyntaxTree::DeclarationList::iterator i = s->content_.begin ();
           i != s->content_.end ();
           i++)
      {
        dispatch (*i);
      }
    }
  };

  //
  //
  //
  struct InterfaceDecl : Traverser
  {
    InterfaceDecl ()
    {
      map (typeid (SyntaxTree::InterfaceDecl), this);
    }

    virtual void
    traverse (SyntaxTree::Node*)
    {
      std::cerr << "interface declaration" << std::endl;
    }
  };

  //
  //
  //
  struct InterfaceDef : Traverser
  {
    InterfaceDef ()
    {
      map (typeid (SyntaxTree::InterfaceDef), this);
    }

    virtual void
    traverse (SyntaxTree::Node*)
    {
      std::cerr << "interface definition" << std::endl;
    }
  };
}

#endif  // TRAVERSAL_HPP
//$Id$