summaryrefslogtreecommitdiff
path: root/cli/header.cxx
blob: dc0ea9e04035222654497623ac6482cb1773aa68 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// file      : cli/header.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <cli/header.hxx>

using namespace std;

namespace
{
  //
  //
  struct option: traversal::option, context
  {
    option (context& c) : context (c) {}

    virtual void
    traverse (type& o)
    {
      string name (ename (o));
      string type (o.type ().name ());

      os << "const " << type << "&" << endl
         << name << " () const;"
         << endl;

      if (modifier)
      {
        os << type << "&" << endl
           << name << " ();"
           << endl;

        os << "void" << endl
           << name << " (const " << type << "&);"
           << endl;
      }

      if (specifier && type != "bool")
      {
        string spec (especifier (o));

        os << "bool" << endl
           << spec << " () const;"
           << endl;

        if (modifier)
          os << "void" << endl
             << spec << " (bool);"
             << endl;
      }
    }
  };

  //
  //
  struct option_data: traversal::option, context
  {
    option_data (context& c) : context (c) {}

    virtual void
    traverse (type& o)
    {
      string member (emember (o));
      string type (o.type ().name ());

      os << type << " " << member << ";";

      if (specifier && type != "bool")
        os << "bool " << especifier_member (o) << ";";
    }
  };

  //
  //
  struct base: traversal::class_, context
  {
    base (context& c): context (c), first_ (true) {}

    virtual void
    traverse (type& c)
    {
      if (first_)
      {
        os << ": ";
        first_ = false;
      }
      else
        os << "," << endl
           << "  ";

      os << "public " << fq_name (c);
    }

  private:
    bool first_;
  };

  //
  //
  struct class_: traversal::class_, context
  {
    class_ (context& c)
        : context (c),
          option_ (c),
          option_data_ (c)
    {
      names_option_ >> option_;
      names_option_data_ >> option_data_;
    }

    virtual void
    traverse (type& c)
    {
      bool abst (c.abstract ());
      string name (escape (c.name ()));
      string um (cli + "::unknown_mode");

      os << "class " << name;

      {
        base b (*this);
        traversal::inherits i (b);
        inherits (c, i);
      }

      os << "{"
         << "public:" << endl;

      // c-tors
      //
      if (!abst)
      {
        os << name << " ();"
           << endl;

        // Are we generating parsing constructors or parse() functions?
        //
        string n;
        if (options.generate_parse ())
        {
          os << "// Return true if anything has been parsed." << endl
             << "//" << endl;

          n = string ("bool\n") + (name != "parse" ? "parse" : "parse_");
        }
        else
          n = name;

        os << n << " (int& argc," << endl
           << "char** argv," << endl
           << "bool erase = false," << endl
           << um << " option = " << um << "::fail," << endl
           << um << " argument = " << um << "::stop);"
           << endl;

        os << n << " (int start," << endl
           << "int& argc," << endl
           << "char** argv," << endl
           << "bool erase = false," << endl
           << um << " option = " << um << "::fail," << endl
           << um << " argument = " << um << "::stop);"
           << endl;

        os << n << " (int& argc," << endl
           << "char** argv," << endl
           << "int& end," << endl
           << "bool erase = false," << endl
           << um << " option = " << um << "::fail," << endl
           << um << " argument = " << um << "::stop);"
           << endl;

        os << n << " (int start," << endl
           << "int& argc," << endl
           << "char** argv," << endl
           << "int& end," << endl
           << "bool erase = false," << endl
           << um << " option = " << um << "::fail," << endl
           << um << " argument = " << um << "::stop);"
           << endl;

        os << n << " (" << cli << "::scanner&," << endl
           << um << " option = " << um << "::fail," << endl
           << um << " argument = " << um << "::stop);"
           << endl;
      }

      //
      //
      os << "// Option accessors" << (modifier ? " and modifiers." : ".") << endl
         << "//" << endl;

      names (c, names_option_);

      // Usage.
      //
      if (usage != ut_none)
      {
        string up (cli + "::usage_para");
        string const& ost (options.ostream_type ());

        os << "// Print usage information." << endl
           << "//" << endl;

        os << "static " << up << endl
           << "print_usage (" << ost << "&," << endl
           << up << " = " << up << "::none);"
           << endl;

        if (usage == ut_both)
          os << "static " << up << endl
             << "print_long_usage (" << ost << "&," << endl
             << up << " = " << up << "::none);"
             << endl;
      }

      // Description.
      //
      if (options.generate_description ())
      {
        os << "// Option description." << endl
           << "//" << endl
           << "static const " << cli << "::options&" << endl
           << "description ();"
           << endl;
      }

      os << "// Implementation details." << endl
         << "//" << endl
         << "protected:" << endl;

      // default c-tor (abstract)
      //
      if (abst)
        os << name << " ();"
           << endl;

      // fill ()
      //
      if (options.generate_description ())
        os << "friend struct _cli_" + name + "_desc_type;"
           << endl
           << "static void" << endl
           << "fill (" << cli << "::options&);"
           << endl;

      // _parse ()
      //
      os << "bool" << endl
         << "_parse (const char*, " << cli << "::scanner&);"
         << endl;

      // _parse ()
      //
      if (!abst)
        os << "private:" << endl
           << "bool" << endl
           << "_parse (" << cli << "::scanner&," << endl
           << um << " option," << endl
           << um << " argument);"
           << endl;

      // Data members.
      //
      os << "public:" << endl; //@@ tmp

      names (c, names_option_data_);

      os << "};";
    }

  private:
    option option_;
    traversal::names names_option_;

    option_data option_data_;
    traversal::names names_option_data_;
  };

  //
  //
  struct includes_: traversal::cxx_includes,
                    traversal::cli_includes,
                    context
  {
    includes_ (context& c) : context (c) {}

    virtual void
    traverse (semantics::cxx_includes& i)
    {
      generate (i.kind (), i.file ().string ());
    }

    virtual void
    traverse (semantics::cli_includes& i)
    {
      generate (i.kind (),
                (options.output_prefix () +
                 i.file ().base ().string () +
                 options.output_suffix () +
                 options.hxx_suffix ()));
    }

    void
    generate (semantics::includes::kind_type k, string const& f)
    {
      char b, e;
      if (k == semantics::includes::quote)
        b = e = '"';
      else
      {
        b = '<';
        e = '>';
      }

      os << "#include " << b << f << e << endl
         << endl;
    }
  };
}

void
generate_header (context& ctx)
{
  ostream& os (ctx.os);

  traversal::cli_unit unit;
  includes_ includes (ctx);
  traversal::names unit_names;
  namespace_ ns (ctx);
  class_ cl (ctx);

  unit >> includes;
  unit >> unit_names >> ns;
  unit_names >> cl;

  traversal::names ns_names;

  ns >> ns_names >> ns;
  ns_names >> cl;

  unit.dispatch (ctx.unit);

  // Entire page usage.
  //
  if (ctx.usage != ut_none && ctx.options.page_usage_specified ())
  {
    os << "// Print page usage information." << endl
       << "//" << endl;

    const string& qn (ctx.options.page_usage ());
    string n (ctx.escape (ctx.substitute (ctx.ns_open (qn, false))));

    string up (ctx.cli + "::usage_para");
    string const& ost (ctx.options.ostream_type ());

    os << up << endl
       << n << "usage (" << ost << "&," << endl
       << up << " = " << up << "::none);"
       << endl;

    if (ctx.usage == ut_both)
      os << up << endl
         << n << "long_usage (" << ost << "&," << endl
         << up << " = " << up << "::none);"
         << endl;

    ctx.ns_close (qn, false);
  }
}