summaryrefslogtreecommitdiff
path: root/contrib/utility/Example/Introspection/Traversal/driver.cpp
blob: 35891ef58cc7cae58098911150f3f8ed71c79adf (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
// file      : Example/Introspection/Traversal/driver.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#include <iostream>

#include "SyntaxTree.hpp"
#include "Traversal.hpp"

int
main ()
{
  using namespace SyntaxTree;

  /*
    Create a syntax tree that looks something like this:

    scope
    {
      interface declaration;

      scope
      {
        interface definition
        {
          decalartion;
        };
      };
    };

  */

  Scope s1;

  InterfaceDecl i1;
  s1.content_.push_back (&i1);

  Scope s2;
  s1.content_.push_back (&s2);

  InterfaceDef i2;
  s2.content_.push_back (&i2);

  Declaration d1;
  i2.content_.push_back (&d1);

  SyntaxTree::Node* root = &s1;

  // Now different ways of traversing this tree:

  {
    std::cout << "test #1" << std::endl;

    struct Generator : Traversal::Declaration, Traversal::Scope
    {
    };

    Generator g;
    g.dispatch (root);

    std::cout << std::endl;
  }

  {
    std::cout << "test #2" << std::endl;

    struct Generator : Traversal::Scope, Traversal::InterfaceDecl
    {
    };

    Generator g;
    g.dispatch (root);

    std::cout << std::endl;
  }

  {
    std::cout << "test #3" << std::endl;

    struct Generator : Traversal::Scope, Traversal::InterfaceDef
    {
    };

    Generator g;
    g.dispatch (root);

    std::cout << std::endl;
  }
}
//$Id$