summaryrefslogtreecommitdiff
path: root/CIAO/CCF/CCF/IDL2/SemanticGraph/Name.cpp
blob: 374446661df0cc9e9e407aab882f4f93a428b78f (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// file      : CCF/IDL2/SemanticGraph/Name.cpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#include "CCF/IDL2/SemanticGraph/Name.hpp"

#include <ostream>

/*
#include <iostream>

using std::cerr;
using std::endl;
*/

namespace CCF
{
  namespace IDL2
  {
    namespace SemanticGraph
    {

      // SimpleName
      //
      //

      SimpleName::
      SimpleName (char const* name) throw (InvalidName)
          : escaped_ (name[0] == '_'),
            name_ (escaped_ ? name + 1 : name)
      {
        if (name_.find (':') != std::string::npos)
          throw InvalidName ();
      }

      SimpleName::
      SimpleName (std::string const& name) throw (InvalidName)
          : escaped_ (name[0] == '_'),
            name_ (name, escaped_ ? 1 : 0)
      {
        if (name_.find (':') != std::string::npos)
          throw InvalidName ();
      }

      SimpleName
      operator+ (SimpleName const& name, std::string const& str)
      {
        return SimpleName (name.str () + str);
      }

      SimpleName
      operator+ (std::string const& str, SimpleName const& name)
      {
        return SimpleName (str + name.str ());
      }

      // Name
      //
      //
      Name::
      Name (SimpleName const& name)
          : name_cache_ (name.unescaped_str ())
      {
        name_.push_back (name);
      }

      Name::
      Name (char const* name) throw (InvalidName)
      {
        init (name);
      }

      Name::
      Name (std::string const& name) throw (InvalidName)
      {
        init (name);
      }

      void Name::
      init (std::string const& name) throw (InvalidName)
      {
        // cerr << "parsing name \'" << name_cache_ << "\' {"<< endl;

        for (std::string::size_type pos (0), next (name.find ("::", pos));;
             next = name.find ("::", pos))
        {
          std::string simple_name (
            name,
            pos,
            next == std::string::npos ? next : next - pos);

          // cerr << "\t\'" << simple_name << '\'' << endl;

          if (simple_name.empty () && !name_.empty ())
          {
            // Empty name (file-scope) is only valid when
            // it is first.
            //
            throw InvalidName ();
          }

          name_.push_back (SimpleName (simple_name));
          name_cache_ += (pos != 0 ? "::" : "") +
            name_.back ().unescaped_str ();

          if (next == std::string::npos)
            break;

          pos = next + 2;
        }

        // cerr << "parsing name }" << name_cache_ << endl;

        if (name_.empty ())
          throw InvalidName ();
      }


      Name::
      Name (Iterator begin, Iterator end) throw (InvalidName)
      {
        for (Iterator i (begin); i != end; ++i)
        {
          name_cache_ += (i != begin ? "::" : "") + i->unescaped_str ();
          name_.push_back (*i);
        }

        if (name_.empty ())
          throw InvalidName ();
      }

      bool Name::
      scoped () const
      {
        return name_[0] == SimpleName ("");
      }

      bool Name::
      simple () const
      {
        return name_.size () == 1;
      }

      // ScopedName
      //
      //

      ScopedName::
      ScopedName (char const* name) throw (InvalidName)
          : Name (name)
      {
        if (!scoped ()) throw InvalidName ();
      }

      ScopedName::
      ScopedName (std::string const& name) throw (InvalidName)
          : Name (name)
      {
        if (!scoped ()) throw InvalidName ();
      }

      ScopedName::
      ScopedName (Iterator begin, Iterator end) throw (InvalidName)
          : Name (begin, end)
      {
        if (!scoped ()) throw InvalidName ();
      }

      ScopedName::
      ScopedName (Name const& name) throw (InvalidName)
          : Name (name)
      {
        if (!scoped ()) throw InvalidName ();
      }

      ScopedName::
      ScopedName (ScopedName const& scope, Name const& name)
        throw (InvalidName)
          : Name (scope.str () + "::" + name.str ())
      {
        if (!scoped ()) throw InvalidName ();
      }

      SimpleName ScopedName::
      simple_name () const
      {
        return *(end () - 1);
      }

      ScopedName ScopedName::
      scope_name () const throw (FileScope)
      {
        Iterator end (this->end () - 1);

        if (begin () == end) throw FileScope ();

        return ScopedName (begin (), end);
      }


      // NamePrinter
      //
      int const name_printer_index = std::ios_base::xalloc ();

      NamePrinter::~NamePrinter ()
      {
      }

      void NamePrinter::
      print (std::ostream& os, Name const& n)
      {
        for (Name::Iterator b (n.begin ()), i (b); i != n.end (); ++i)
        {
          if (i != b)
            os << "::";

          print (os, *i);
        }
      }
    }
  }
}

std::ostream&
operator << (std::ostream& o,
             CCF::IDL2::SemanticGraph::SimpleName const& name)
{
  using namespace CCF::IDL2::SemanticGraph;

  if (void* tmp = o.pword (name_printer_index))
  {
    NamePrinter* p (reinterpret_cast<NamePrinter*> (tmp));
    p->print (o, name);
    return o;
  }
  else
    return o << name.str ();
}

std::ostream&
operator << (std::ostream& o, CCF::IDL2::SemanticGraph::Name const& name)
{
  using namespace CCF::IDL2::SemanticGraph;

  if (void* tmp = o.pword (name_printer_index))
  {
    NamePrinter* p (reinterpret_cast<NamePrinter*> (tmp));
    p->print (o, name);
    return o;
  }
  else
    return o << name.str ();
}