summaryrefslogtreecommitdiff
path: root/cli/header.cxx
blob: 3a47c4d763245d8c256e26198fe15ca6b48e1766 (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
// file      : cli/header.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include "header.hxx"

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 << "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 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)
    {
      string name (escape (c.name ()));

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

      // c-tors
      //
      string um (cli + "::unknown_mode");

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

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

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

      os << name << " (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 << name << " (" << cli << "::scanner&," << endl
         << um << " option = " << um << "::fail," << endl
         << um << " argument = " << um << "::stop);"
         << endl;

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

      names (c, names_option_);

      // Usage.
      //
      if (usage)
      {
        os << "// Print usage information." << endl
           << "//" << endl
           << "static void" << endl
           << "print_usage (::std::ostream&);"
           << endl;
      }

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

      // _parse()
      //
      os << "private:" << endl
         << "void" << 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, context
  {
    includes (context& c) : context (c) {}

    virtual void
    traverse (semantics::cxx_includes& i)
    {
      os << "#include " << i.file () << endl
         << endl;
    }
  };
}

void
generate_header (context& ctx)
{
  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);
}