summaryrefslogtreecommitdiff
path: root/modules/CIAO/CCF/CCF/CodeGenerationKit/IndentationIDL.hpp
blob: 24f9866eac45cbaf022c6df48d08b6e1e2059d94 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// file      : CCF/CodeGenerationKit/IndentationIDL.hpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#ifndef CCF_CODE_GENERATION_KIT_INDENTATION_IDL_HPP
#define CCF_CODE_GENERATION_KIT_INDENTATION_IDL_HPP

#include <deque>

#include "CCF/CodeGenerationKit/IndentationBuffer.hpp"

namespace Indentation
{
  template <typename C>
  class IDL : public Buffer<C>
  {
  public:
    typedef
    typename Buffer<C>::traits_type
    traits_type;

    typedef
    typename Buffer<C>::char_type
    char_type;

    typedef
    typename Buffer<C>::int_type
    int_type;

    typedef
    typename Buffer<C>::EndOfStream
    EndOfStream;

  public:
    IDL (Buffer<C>& out)
        : out_ (out),
          indentation_ (0),
          spaces_ (2),
          construct_ (OTHER)
    {
    }

    virtual
    ~IDL () throw () {}

  public:
    virtual int_type
    put (char_type c) throw (ExH::System::Exception)
    {
      int_type result = traits_type::to_int_type (c);

      try
      {
        bool defaulting = false;
        switch (c)
        {
        case '\n':
          {
            hold_.push_back (c);
            break;
          }
        case '{':
          {
            ensure_new_line ();
            output_indentation ();
            result = write (c);
            ensure_new_line ();
            indentation_++;
            break;
          }
        case '}':
          {
            if (indentation_ > 0) indentation_--;

            // Reduce multiple newlines to one.
            while (hold_.size () > 1)
            {
              typename Hold::reverse_iterator i = hold_.rbegin ();
              if (*i == '\n' && *(i + 1) == '\n') hold_.pop_back ();
              else break;
            }

            ensure_new_line ();
            output_indentation ();

            hold_.push_back (c);

            // result = write (c);

            //ensure_new_line ();

            // Add double newline after '}'.
            //
            hold_.push_back ('\n');
            hold_.push_back ('\n');


            break;
          }
        case ';':
          {
            // Handling '};' case.
            //

            bool brace (false);

            if (hold_.size () > 1 && hold_.back () == '\n')
            {
              bool pop_nl (false);

              for (typename Hold::reverse_iterator
                     i (hold_.rbegin ()), e (hold_.rend ()); i != e; ++i)
              {
                if (*i != '\n')
                {
                  if (*i == '}') brace = pop_nl = true;
                  break;
                }
              }

              if (pop_nl) while (hold_.back () == '\n') hold_.pop_back ();
            }

            output_indentation ();
            result = write (c);

            if (brace)
            {
              hold_.push_back ('\n');
              hold_.push_back ('\n');
            }

            if (construct_ != STRING_LITERAL && construct_ != CHAR_LITERAL)
            {
              ensure_new_line ();
            }
            break;
          }
        case '\\':
          {
            hold_.push_back (c);
            break;
          }
        case '\"':
          {
            if (hold_.empty () || hold_.back () != '\\')
            {
              // not escape sequence
              if (construct_ == STRING_LITERAL) construct_ = OTHER;
              else construct_ = STRING_LITERAL;
            }

            defaulting = true;
            break;
          }
        case '\'':
          {
            if (hold_.empty () || hold_.back () != '\\')
            {
              // not escape sequence
              if (construct_ == CHAR_LITERAL) construct_ = OTHER;
              else construct_ = CHAR_LITERAL;
            }

            defaulting = true;
            break;
          }
        default:
          {
            defaulting = true;
            break;
          }
        }

        if (defaulting)
        {
          output_indentation ();
          result = write (c);
        }
      }
      catch (Full const&)
      {
        result = traits_type::eof ();
      }

      return result;
    }

    virtual void
    unbuffer () throw (EndOfStream, ExH::System::Exception)
    {
      int_type result;

      while (!hold_.empty ())
      {
        result = out_.put (hold_.front ());

        //@@ failed
        if (result == traits_type::eof ())
        {
          throw EndOfStream ("unable to flush buffer");
        }

        hold_.pop_front ();
      }
    }

  private:
    class Full {};

    void
    ensure_new_line ()
    {
      if (hold_.empty () || hold_.back () != '\n')
      {
        hold_.push_back ('\n');
      }
    }


    void
    output_indentation () throw (Full)
    {
      if (!hold_.empty () && hold_.back () == '\n')
      {
        for (unsigned long i = 0; i < indentation_ * spaces_; i++)
        {
          write (' ');
        }
      }
    }

    int_type
    write (char_type c) throw (Full)
    {
      hold_.push_back (c);

      int_type result (traits_type::eof ());

      while (!hold_.empty ())
      {
        result = out_.put (hold_.front ());

        if (result == traits_type::eof ()) throw Full ();

        hold_.pop_front ();
      }

      return result;
    }


  private:
    Buffer<C>& out_;
    unsigned long indentation_;
    unsigned long spaces_;

    bool suppress_nl_;

    enum Construct
    {
      OTHER,
      STRING_LITERAL,
      CHAR_LITERAL
    };

    Construct construct_;

    typedef
    std::deque<int_type>
    Hold;

    Hold hold_;
  };
}

#endif // CCF_CODE_GENERATION_KIT_INDENTATION_IDL_HPP