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

#ifndef CLI_CONTEXT_HXX
#define CLI_CONTEXT_HXX

#include <set>
#include <map>
#include <string>
#include <vector>
#include <ostream>
#include <cstddef> // std::size_t

#include <cutl/re.hxx>
#include <cutl/shared-ptr.hxx>
#include <cutl/fs/path.hxx>

#include <cli/options.hxx>
#include <cli/semantics.hxx>
#include <cli/traversal.hxx>

using std::endl;

class generation_failed {};

enum usage
{
  ut_none,
  ut_short,
  ut_long,
  ut_both
};

enum class_doc
{
  cd_default,
  cd_exclude,
  cd_exclude_base,
  cd_short,
  cd_long
};

class context
{
public:
  typedef std::size_t size_t;
  typedef std::string string;

  typedef cutl::fs::path path;
  typedef cutl::fs::invalid_path invalid_path;

  typedef ::options options_type;
  typedef ::usage usage_type;
  typedef ::class_doc class_doc_type;

  // Regex.
  //
  typedef cutl::re::regex regex;
  typedef cutl::re::regexsub regexsub;
  typedef cutl::re::format regex_format;

  typedef std::vector<regexsub> regex_mapping;

private:
  struct data;
  cutl::shared_ptr<data> data_;

public:
  std::ostream& os;
  semantics::cli_unit& unit;
  options_type const& options;

  // Documentation output type.
  //
  enum output_type
  {
    ot_plain,
    ot_html,
    ot_man
  };

  output_type ot;

  bool modifier;
  bool specifier;
  usage_type usage;

  string const& inl;
  string const& opt_prefix;
  string const& opt_sep;
  string const& cli;

  typedef std::map<string, string> reserved_name_map_type;
  reserved_name_map_type const& reserved_name_map;

  typedef std::set<string> keyword_set_type;
  keyword_set_type const& keyword_set;

  regex_mapping const& link_regex;

  typedef std::set<string> id_set_type;
  id_set_type& id_set;
  id_set_type& ref_set;

  // Map of heading ids to heading number (assigned during TOC generation)
  //
  typedef std::map<string, string> heading_map_type;
  heading_map_type& heading_map;

  // TOC phase.
  //
  // 0 - non-TOC
  // 1 - generating TOC after seeing expansion but before restart
  // 2 - generating TOC after restart
  // 0 - non-TOC after seeing expansion after restart
  //
  unsigned short& toc;

  struct toc_entry
  {
    toc_entry (char t = '\0', size_t c = 0): type (t), count (c) {}

    char type;    // Entry type (output type-specific, usually X from \hX).
    size_t count; // Number of sub-entries so far.
  };

  typedef std::vector<toc_entry> toc_stack;
  toc_stack& tocs;

private:
  struct data
  {
    string inl_;
    string cli_;
    keyword_set_type keyword_set_;
    regex_mapping link_regex_;
    id_set_type id_set_;
    id_set_type ref_set_;
    heading_map_type heading_map_;
    unsigned short toc_;
    toc_stack tocs_;
  };

public:
  // Escape C++ keywords, reserved names, and illegal characters.
  //
  string
  escape (string const&) const;

  string
  process_link_target (const string&);

  // Translate and format the documentation string. Translate converts
  // the <arg>-style constructs to \i{arg}. Format converts the string
  // to the output format.
  //
  static string
  translate_arg (string const&, std::set<string>&);

  static string
  translate (string const&, std::set<string> const&);

  // If para is true, start a new paragraph.
  //
  string
  format (semantics::scope&, string const&, bool para);

  void
  format_line (output_type, string&, const char*, size_t);

  // Called when we switch to the TOC mode and when we exit it,
  // respectively.
  //
  string
  start_toc ();

  string
  end_toc ();

  // Make sure each local fragment reference has the corresponding id. Issue
  // diagnostics and throw generation_failed if fails. Otherwise clear the
  // id and ref sets.
  //
  void
  verify_id_ref ();

  // Substitute doc variable expansions ($var$). Var must be a C identifier.
  // If the path is not NULL, then also recognize names that start with either
  // ./ or ../ and treat them as files relative to path. Such file expansions
  // are substituted with the file contents.
  //
  string
  substitute (const string&, const path* = 0);

  // Substitute doc variable expansions (\$var$). Note that it leaves escapes
  // (\\$) as is. Return true if any substitutions have been made, in which
  // case result will contain the expansion result.
  //
  bool
  substitute (semantics::scope&, const char* s, size_t n, string& result);

public:
  static string const&
  ename (semantics::nameable& n)
  {
    return n.context ().get<string> ("name");
  }

  static string const&
  especifier (semantics::nameable& n)
  {
    return n.context ().get<string> ("specifier");
  }

  static string const&
  emember (semantics::nameable& n)
  {
    return n.context ().get<string> ("member");
  }

  static string const&
  especifier_member (semantics::nameable& n)
  {
    return n.context ().get<string> ("specifier-member");
  }

public:
  // Return fully-qualified C++ or CLI name.
  //
  string
  fq_name (semantics::nameable& n, bool cxx_name = true);

  // Open/close namespace. If last is false, then the last name
  // component is not treated as a namespace. The open function
  // also returns the last name component.
  //
public:
  string
  ns_open (const string& name, bool last = true);

  void
  ns_close (const string& name, bool last = true);

public:
  class_doc_type
  class_doc (semantics::class_&);

public:
  string
  first_sentence (string const&);

public:
  context (std::ostream&,
           output_type,
           semantics::cli_unit&,
           options_type const&);

  context (context&);

private:
  context&
  operator= (context const&);
};

// Checks if scope Y names any of X.
//
template <typename X, typename Y>
bool
has (Y& y)
{
  for (semantics::scope::names_iterator i (y.names_begin ()),
         e (y.names_end ()); i != e; ++i)
    if (i->named (). template is_a<X> ())
      return true;

  return false;
}

// Standard namespace traverser.
//
struct namespace_: traversal::namespace_, context
{
  namespace_ (context& c) : context (c) {}

  virtual void
  pre (type&);

  virtual void
  post (type&);
};

#endif // CLI_CONTEXT_HXX