aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/eolian_mono/eolian/mono/documentation.hh
blob: ac072a42203b47d98db4bd269f07c0d5ceb2352e (plain) (blame)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#ifndef EOLIAN_MONO_DOCUMENTATION_HPP
#define EOLIAN_MONO_DOCUMENTATION_HPP

#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"
#include "grammar/html_escaped_string.hpp"
#include "using_decl.hh"
#include "name_helpers.hh"
#include "generation_contexts.hh"
#include "blacklist.hh"

#include <Eina.h>

namespace eolian_mono {

struct documentation_generator
{

   int scope_size = 0;

   documentation_generator(int scope_size)
       : scope_size(scope_size) {}


   // Returns the number of parameters (values + keys) that a property method requires
   // Specify if you want the Setter or the Getter method.
   static int property_num_parameters(const ::Eolian_Function *function, ::Eolian_Function_Type ftype)
   {
      Eina_Iterator *itr = ::eolian_property_keys_get(function, ftype);
      Eolian_Function_Parameter *pr;
      int n = 0;
      EINA_ITERATOR_FOREACH(itr, pr) { n++; }
      eina_iterator_free(itr);
      itr = ::eolian_property_values_get(function, ftype);
      EINA_ITERATOR_FOREACH(itr, pr) { n++; }
      eina_iterator_free(itr);
      return n;
   }

   // Gets the eolian ref for the given class, prepending I for interfaces.
   static std::string object_ref_conversion(const Eolian_Object *cls)
   {
      auto klass_type = ::eolian_class_type_get((const Eolian_Class *)cls);
      auto full_eolian_name = name_helpers::managed_namespace(::eolian_object_name_get(cls));
      if (klass_type == EOLIAN_CLASS_MIXIN || klass_type == EOLIAN_CLASS_INTERFACE)
        {
           size_t pos = full_eolian_name.rfind(".");
           if (pos == std::string::npos)
             pos = 0;
           else
             pos++;
           full_eolian_name.insert(pos, "I");
        }
      return full_eolian_name;
   }


   // Turns a function name from EO convention to EFL# convention.
   // The name_tail parameter is the last 4 chars of the original string, which
   // could be ".set" or ".get" and in this case they are ignored by Eolian.
   // We want them to know what the documentation intended to reference.
   static std::string function_conversion(const ::Eolian_Object *klass, const ::Eolian_Function *function, std::string name_tail)
   {
      ::Eolian_Function_Type ftype = ::eolian_function_type_get(function);
      const char* eo_name = ::eolian_function_name_get(function);
      std::string name = object_ref_conversion(klass);

      // Klass is needed to check the property naming rulles
      attributes::klass_def klass_d((const ::Eolian_Class *)klass, eolian_object_unit_get(klass));

      switch(ftype)
      {
         case ::EOLIAN_METHOD:
           if (blacklist::is_function_blacklisted(
                 ::eolian_function_full_c_name_get(function, ftype))) return "";
           name += ".";
           name += name_helpers::managed_method_name({function, ftype, NULL, eolian_object_unit_get(EOLIAN_OBJECT(function))});
           break;
         case ::EOLIAN_PROP_SET:
           name += ".Set";
           name += name_helpers::property_managed_name(klass_d, eo_name);
           break;
         case ::EOLIAN_PROP_GET:
           name += ".Get";
           name += name_helpers::property_managed_name(klass_d, eo_name);
           break;
         case ::EOLIAN_PROPERTY:
           {
             int getter_params = property_num_parameters(function, ::EOLIAN_PROP_GET);
             int setter_params = property_num_parameters(function, ::EOLIAN_PROP_SET);
             std::string short_name = name_helpers::property_managed_name(klass_d, eo_name);
             bool blacklisted = blacklist::is_property_blacklisted(name + "." + short_name);
             // EO properties with keys, with more than one value, or blacklisted, are not
             // converted into C# properties.
             // In these cases we refer to the getter method instead of the property.
             if ((getter_params > 1) || (setter_params > 1) || (blacklisted)) name += ".Get" + short_name;
             else if (name_tail == ".get") name += ".Get" + short_name;
             else if (name_tail == ".set") name += ".Set" + short_name;
             else name += "." + short_name;
           }
           break;
         default:
           break;
      }
      return name;
   }

   static std::string function_conversion(attributes::function_def const& func)
   {
      std::string name = name_helpers::klass_full_concrete_or_interface_name(func.klass);
      switch (func.type)
      {
          // managed_method_name takes care of reordering the function name so the get/set goes first
          // for properties
          case attributes::function_type::method:
          case attributes::function_type::prop_set:
          case attributes::function_type::prop_get:
            if (blacklist::is_function_blacklisted(func.c_name))return "";
            if (!name.empty()) name += ".";
            name += name_helpers::managed_method_name(func);
            break;
          default:
            // No need to deal with property as function_defs are converted to get/set when building a given klass_def.
            break;
      }

      return name;
   }

   // Turns an Eolian reference like @Efl.Input.Pointer.tool into a <see> tag
   static std::string ref_conversion(const ::Eolian_Doc_Token *token, const Eolian_State *state, std::string name_tail)
   {
      const Eolian_Object *data, *data2;
      ::Eolian_Object_Type type =
        ::eolian_doc_token_ref_resolve(token, state, &data, &data2);
      std::string ref;
      switch(type)
      {
         case ::EOLIAN_OBJECT_STRUCT_FIELD:
           ref = name_helpers::managed_namespace(::eolian_object_name_get(data));
           ref += ".";
           ref += ::eolian_object_name_get(data2);
           if (blacklist::is_struct_blacklisted(ref)) return "";
           break;
         case ::EOLIAN_OBJECT_EVENT:
           ref = object_ref_conversion(data);
           ref += ".";
           ref += name_helpers::managed_event_name(::eolian_object_name_get(data2));
           break;
         case ::EOLIAN_OBJECT_ENUM_FIELD:
           ref = name_helpers::managed_namespace(::eolian_object_name_get(data));
           ref += ".";
           ref += name_helpers::enum_field_managed_name(::eolian_object_name_get(data2));
           break;
         case ::EOLIAN_OBJECT_FUNCTION:
           ref += function_conversion(data, (const ::Eolian_Function *)data2, name_tail);
           break;
         case ::EOLIAN_OBJECT_VARIABLE:
           if (::eolian_variable_type_get((::Eolian_Variable *)data) == ::EOLIAN_VAR_CONSTANT)
             {
                auto names = utils::split(name_helpers::managed_namespace(::eolian_object_name_get(data)), '.');
                names.pop_back(); // Remove var name
                ref = name_helpers::join_namespaces(names, '.');
                ref += "Constants.";
                ref += name_helpers::managed_name(::eolian_object_short_name_get(data));
             }
           // Otherwise, do nothing and no <see> tag will be generated. Because, who would
           // reference a global (non-constant) variable in the docs?
           break;
         case ::EOLIAN_OBJECT_UNKNOWN:
           // If the reference cannot be resolved, just return an empty string and
           // it won't be converted into a <see> tag.
           break;
         case ::EOLIAN_OBJECT_CLASS:
           ref = object_ref_conversion(data);
           break;
         default:
           ref = name_helpers::managed_namespace(::eolian_object_name_get(data));
           break;
      }
      return ref;
   }

   // Turns EO documentation syntax into C# triple-slash XML comment syntax
   static std::string syntax_conversion(std::string text, const Eolian_State *state)
   {
      std::string new_text, ref;
      ::Eolian_Doc_Token token;
      const char *text_ptr = text.c_str();
      ::eolian_doc_token_init(&token);
      ::Eolian_Doc_Token_Type previous_token_type = ::EOLIAN_DOC_TOKEN_UNKNOWN;
      while ((text_ptr = ::eolian_documentation_tokenize(text_ptr, &token)) != NULL)
        {
           std::string token_text, name_tail;
           char *token_text_cstr = ::eolian_doc_token_text_get(&token);
           if (token_text_cstr)
             {
                token_text = token_text_cstr;
                free(token_text_cstr);
                if (token_text.length() > 4)
                  name_tail = token_text.substr(token_text.length() - 4, 4);
             }
           ::Eolian_Doc_Token_Type token_type = ::eolian_doc_token_type_get(&token);
           switch(token_type)
           {
              case ::EOLIAN_DOC_TOKEN_TEXT:
                // If previous token was a reference and this text token starts with
                // parentheses, remove them, since the reference will be rendered
                // with the parentheses already.
                if ((previous_token_type == ::EOLIAN_DOC_TOKEN_REF) &&
                    (token_text.substr(0, 2)  == "()"))
                  token_text = token_text.substr(2, token_text.length() - 2);
                new_text += token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_REF:
                ref = ref_conversion(&token, state, name_tail);
                if (ref != "")
                  new_text += "<see cref=\"" + ref + "\"/>";
                else
                  // Unresolved references are passed through.
                  // They will appear in the docs as plain text, without link,
                  // but at least they won't be removed by DocFX.
                  new_text += token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_MARK_NOTE:
                new_text += "NOTE: " + token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_MARK_WARNING:
                new_text += "WARNING: " + token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_MARK_REMARK:
                new_text += "REMARK: " + token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_MARK_TODO:
                new_text += "TODO: " + token_text;
                break;
              case ::EOLIAN_DOC_TOKEN_MARKUP_MONOSPACE:
                new_text += "<c>" + token_text + "</c>";
                break;
              default:
                break;
           }
           previous_token_type = token_type;
        }
      return new_text;
   }

   /// Tag generator helpers
   template<typename OutputIterator, typename Context>
   bool generate_opening_tag(OutputIterator sink, std::string const& tag, Context const& context, std::string tag_params = "") const
   {
      return as_generator("<" << tag << tag_params << ">").generate(sink, attributes::unused, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_closing_tag(OutputIterator sink, std::string const& tag, Context const& context) const
   {
      return as_generator("</" << tag << ">").generate(sink, attributes::unused, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_escaped_content(OutputIterator sink, std::string const &text, Context const& context) const
   {
      std::string new_text;
      if (!as_generator(html_escaped_string).generate(std::back_inserter(new_text), text, context))
        return false;
      new_text = syntax_conversion( new_text, context_find_tag<eolian_state_context>(context).state );

      std::string tabs;
      as_generator(scope_tab(scope_size) << "/// ").generate (std::back_inserter(tabs), attributes::unused, context);

      std::istringstream ss(new_text);
      std::string para;
      std::string final_text;
      bool first = true;
      while (std::getline(ss, para)) {
        if (first) final_text += para;
        else final_text += "\n" + tabs + para;
        first = false;
      }
      return as_generator(final_text).generate(sink, attributes::unused, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag(OutputIterator sink, std::string const& tag, std::string const &text, Context const& context, std::string tag_params = "") const
   {
      if (text == "")
        return true;

      if (!as_generator(scope_tab(scope_size) << "/// ").generate(sink, attributes::unused, context)) return false;
      if (!generate_opening_tag(sink, tag, context, tag_params)) return false;
      if (!generate_escaped_content(sink, text, context)) return false;
      if (!generate_closing_tag(sink, tag, context)) return false;
      return as_generator("\n").generate(sink, attributes::unused, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag_summary(OutputIterator sink, std::string const& text, Context const& context) const
   {
      return generate_tag(sink, "summary", text, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag_param(OutputIterator sink, std::string const& name, std::string const& text, Context const& context) const
   {
      return generate_tag(sink, "param", text, context, " name=\"" + name + "\"");
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag_return(OutputIterator sink, std::string const& text, Context const& context) const
   {
      return generate_tag(sink, "returns", text, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag_value(OutputIterator sink, std::string const& text, Context const& context) const
   {
      return generate_tag(sink, "value", text, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_tag_example(OutputIterator sink, std::string const& full_object_name, Context const& context) const
   {
      auto options = efl::eolian::grammar::context_find_tag<options_context>(context);
      // Example embedding not requested
      if (options.examples_dir.empty()) return true;
      std::string file_name = options.examples_dir + full_object_name + ".cs";
      std::ifstream exfile(file_name);
      // There is no example file for this class or method, just return
      if (!exfile.good()) return true;
      std::stringstream example_buff;
      // Start with a newline so the first line renders with same indentation as the rest
      example_buff << std::endl << exfile.rdbuf();

      if (!as_generator(scope_tab(scope_size) << "/// ").generate(sink, attributes::unused, context)) return false;
      if (!generate_opening_tag(sink, "example", context)) return false;
      if (!generate_opening_tag(sink, "code", context)) return false;
      if (!generate_escaped_content(sink, example_buff.str(), context)) return false;
      if (!generate_closing_tag(sink, "code", context)) return false;
      if (!generate_closing_tag(sink, "example", context)) return false;
      return as_generator("\n").generate(sink, attributes::unused, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_all_tag_examples(OutputIterator sink, std::string const & full_class_name, std::string const& object_name, Context const& context) const
   {
      // Take example from derived class
      auto derived_klass = efl::eolian::grammar::context_find_tag<class_context>(context);
      std::string derived_full_name =
        derived_klass.name.empty() ? object_name : derived_klass.name + "." + object_name;
      std::string base_full_name =
        full_class_name.empty() ? object_name : full_class_name + "." + object_name;
      if (!derived_klass.name.empty())
        {
           if (!generate_tag_example(sink, derived_full_name, context)) return false;
        }
      if (derived_full_name.compare(base_full_name) == 0) return true;
      // Take example from base class
      return generate_tag_example(sink, base_full_name, context);
   }

   // Actual exported generators
   template<typename OutputIterator, typename Attribute, typename Context>
   bool generate(OutputIterator sink, Attribute const& attr, Context const& context) const
   {
       return generate(sink, attr.documentation, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::klass_def const& klass, Context const& context) const
   {
       if (!generate(sink, klass.documentation, context)) return false;

       std::string klass_name = name_helpers::klass_full_concrete_or_interface_name(klass);
       return generate_tag_example(sink, klass_name, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::property_def const& prop, Context const& context) const
   {
       if (!generate(sink, prop.documentation, context))
         return false;

       std::string text;
       if (prop.setter.is_engaged())
         text = prop.setter->parameters[0].documentation.full_text;
       else if (prop.getter.is_engaged())
         text = prop.getter->return_documentation.full_text;
       // If there are no docs at all, do not generate <value> tag
       if (!text.empty())
         if (!generate_tag_value(sink, text, context)) return false;

       return generate_all_tag_examples(sink,
                                        name_helpers::klass_full_concrete_or_interface_name(prop.klass),
                                        name_helpers::property_managed_name(prop),
                                        context);
   }

   template<typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::function_def const& func, Context const& context) const
   {
       if (func.type == attributes::function_type::prop_get || func.type == attributes::function_type::prop_set)
         return generate_property(sink, func, context);
       else
         return generate_function(sink, func, context);
       return true;
   }

   template<typename OutputIterator, typename Context>
   bool generate_property(OutputIterator sink, attributes::function_def const& func, Context const& context) const
   {

       // First, try the get/set specific documentation
       if (!func.documentation.summary.empty())
         {
            if (!generate(sink, func.documentation, context))
              return false;
         }
       else // fallback to common property documentation
         {
            if (!generate(sink, func.property_documentation, context))
              return false;
         }

       for (auto&& param : func.parameters)
         if (!generate_parameter(sink, param, context))
           return false;

       if (!generate_tag_return(sink, func.return_documentation.full_text, context))
         return false;

       return generate_all_tag_examples(sink,
                                        name_helpers::klass_full_concrete_or_interface_name(func.klass),
                                        name_helpers::managed_method_name(func),
                                        context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_function(OutputIterator sink, attributes::function_def const& func, Context const& context) const
   {
       if (!generate(sink, func.documentation, context))
         return false;

       for (auto&& param : func.parameters)
         if (!generate_parameter(sink, param, context))
           return false;

       if (!generate_tag_return(sink, func.return_documentation.full_text, context))
         return false;

       return generate_all_tag_examples(sink,
                                        name_helpers::klass_full_concrete_or_interface_name(func.klass),
                                        name_helpers::managed_method_name(func),
                                        context);
   }

   template<typename OutputIterator, typename Context>
   bool generate_parameter(OutputIterator sink, attributes::parameter_def const& param, Context const& context) const
   {
      return generate_tag_param(sink, name_helpers::escape_keyword(param.param_name), param.documentation.full_text, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::documentation_def const& doc, Context const& context) const
   {
      std::string str = doc.full_text;
      if (!doc.since.empty())
        str += "\n(Since EFL " + doc.since + ")";
      return generate_tag_summary(sink, str, context);
   }

   template<typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::constructor_def const& ctor, Context const& context) const
   {
      // Not sure if this is the best way to generate a reference outside the full doc generator.
      auto unit = (const Eolian_Unit*) context_find_tag<eolian_state_context>(context).state;
      auto func = ctor.function;
      auto eolian_klass = get_klass(func.klass, unit);
      attributes::klass_def klass(eolian_klass, unit);
      std::string summary;

      if (func.type == attributes::function_type::prop_set)
          summary = func.property_documentation.summary;
      else
          summary = func.documentation.summary;

      for (auto &&param : ctor.function.parameters)
        {
          if (!as_generator(
                      scope_tab << "/// <param name=\"" << constructor_parameter_name(ctor) << "\">" << summary << " See <see cref=\"" << function_conversion(func) << "\"/></param>\n"
                      ).generate(sink, param, context))
            return false;
        }
      return true;
   }
};

struct documentation_terminal
{
  documentation_generator operator()(int n) const
  {
      return documentation_generator(n);
  }
} const documentation = {};

documentation_generator as_generator(documentation_terminal)
{
    return documentation_generator(0);
}

} // namespace eolian_mono


namespace efl { namespace eolian { namespace grammar {

template<>
struct is_eager_generator<::eolian_mono::documentation_generator> : std::true_type {};
template<>
struct is_generator<::eolian_mono::documentation_generator> : std::true_type {};

template<>
struct is_generator<::eolian_mono::documentation_terminal> : std::true_type {};

namespace type_traits {
template<>
struct attributes_needed<struct ::eolian_mono::documentation_generator> : std::integral_constant<int, 1> {};
template<>
struct attributes_needed<struct ::eolian_mono::documentation_terminal> : std::integral_constant<int, 1> {};
}
} } }

#endif