summaryrefslogtreecommitdiff
path: root/libxsd-frontend/xsd-frontend/transformations/simplifier.cxx
blob: 9372a4ae0c054fb25f4ec4083b2fddda3263a836 (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
158
159
160
161
162
// file      : xsd-frontend/transformations/simplifier.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsd-frontend/semantic-graph.hxx>
#include <xsd-frontend/traversal.hxx>

#include <xsd-frontend/transformations/simplifier.hxx>

namespace XSDFrontend
{
  namespace
  {
    struct Compositor: Traversal::All,
                       Traversal::Choice,
                       Traversal::Sequence
    {
      Compositor (SemanticGraph::Schema& root)
          : root_ (root)
      {
      }

      virtual void
      traverse (SemanticGraph::All& a)
      {
        // The all compositor cannot contain compositors.
        //
        if (a.contains_begin () == a.contains_end ())
          remove (a);
      }

      virtual void
      traverse (SemanticGraph::Choice& c)
      {
        // Do the depth-first traversal so that we take into account
        // the potential removal of nested compositors.
        //
        using SemanticGraph::Compositor;

        for (Compositor::ContainsIterator i (c.contains_begin ());
             i != c.contains_end ();)
        {
          edge_traverser ().dispatch (*i++);
        }

        Choice::contains (c);

        if (c.contains_begin () == c.contains_end ())
          remove (c);
      }

      virtual void
      traverse (SemanticGraph::Sequence& s)
      {
        // Do the depth-first traversal so that we take into account
        // the potential removal of nested compositors.
        //
        using SemanticGraph::Compositor;

        for (Compositor::ContainsIterator i (s.contains_begin ());
             i != s.contains_end ();)
        {
          edge_traverser ().dispatch (*i++);
        }

        if (s.contains_begin () == s.contains_end ())
          remove (s);
      }

    private:
      virtual void
      remove (SemanticGraph::Compositor& c)
      {
        using SemanticGraph::Node;
        using SemanticGraph::Choice;
        using SemanticGraph::Complex;
        using SemanticGraph::Compositor;

        if (c.contained_particle_p ())
        {
          Compositor& com (c.contained_particle ().compositor ());

          // Empty compositors in choice are important.
          //
          if (!com.is_a<Choice> ())
            root_.delete_edge (com, c, c.contained_particle ());
        }
        else
        {
          Complex& con (
            dynamic_cast<Complex&> (c.contained_compositor ().container ()));
          root_.delete_edge (con, c, c.contained_compositor ());
        }
      }

    private:
      SemanticGraph::Schema& root_;
    };

    //
    //
    struct Type: Traversal::Complex
    {
      virtual void
      traverse (SemanticGraph::Complex& c)
      {
        if (c.contains_compositor_p ())
          Complex::contains_compositor (c);
      }
    };

    // Go into implied/included/imported schemas while making sure
    // we don't process the same stuff more than once.
    //
    struct Uses: Traversal::Uses
    {
      virtual void
      traverse (Type& u)
      {
        SemanticGraph::Schema& s (u.schema ());

        if (!s.context ().count ("xsd-frontend-simplifier-seen"))
        {
          s.context ().set ("xsd-frontend-simplifier-seen", true);
          Traversal::Uses::traverse (u);
        }
      }
    };
  }

  namespace Transformations
  {
    void Simplifier::
    transform (SemanticGraph::Schema& s, SemanticGraph::Path const&)
    {
      Traversal::Schema schema;
      Uses uses;

      schema >> uses >> schema;

      Traversal::Names schema_names;
      Traversal::Namespace ns;
      Traversal::Names ns_names;
      Type type;

      schema >> schema_names >> ns >> ns_names >> type;

      Compositor compositor (s);
      Traversal::ContainsCompositor contains_compositor;
      Traversal::ContainsParticle contains_particle;

      type >> contains_compositor >> compositor;
      compositor >> contains_particle >> compositor;

      // Some twisted schemas do recusive inclusions.
      //
      s.context ().set ("xsd-frontend-simplifier-seen", true);

      schema.dispatch (s);
    }
  }
}