summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/name_helpers.hh
blob: c0b206aa03b57de5ba752b265a765609fc85570a (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
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
#ifndef EOLIAN_MONO_NAME_HELPERS_HH
#define EOLIAN_MONO_NAME_HELPERS_HH

#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include "utils.hh"

#include "grammar/integral.hpp"
#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"
#include "grammar/list.hpp"
#include "grammar/string.hpp"
#include "grammar/integral.hpp"

using efl::eolian::grammar::as_generator;
using efl::eolian::grammar::string;
using efl::eolian::grammar::lit;
using efl::eolian::grammar::operator*;

namespace eolian_mono {

/* Utility functions for naming things. Compared to the utils.hh, this header has higher level
 * functions, dealing with the knowledge of how to convert the items to the C# style we are using, for
 * example, while being too short to be implemented as full-fledged generators.
 */
namespace name_helpers {

namespace attributes = efl::eolian::grammar::attributes;

namespace detail {
inline bool is_equal(std::string const& lhs, std::string const& rhs)
{
  return lhs == rhs;
}
}

inline std::string identity(std::string const& str)
{
  return str;
}

inline std::string escape_keyword(std::string const& name)
{
  using detail::is_equal;
  if(is_equal(name, "delete")
     || is_equal(name, "do")
     || is_equal(name, "lock")
     || is_equal(name, "event")
     || is_equal(name, "in")
     || is_equal(name, "object")
     || is_equal(name, "interface")
     || is_equal(name, "string")
     || is_equal(name, "internal")
     || is_equal(name, "fixed")
     || is_equal(name, "base"))
    return "kw_" + name;

  if (is_equal(name, "Finalize"))
    return name + "Add"; // Eo's Finalize is actually the end of efl_add.
  return name;
}

typedef std::function<std::string(std::string const&)> string_transform_func;

inline std::string join_namespaces(std::vector<std::string> const& namespaces, char separator,
                                   string_transform_func func=identity)
{
   std::stringstream s;
   for (auto&& n : namespaces)
     s << func(n) << separator;

   return s.str();
}

static const std::vector<std::string> verbs =
  {
    "add",
    "get",
    "is",
    "del",
    "thaw",
    "freeze",
    "save",
    "wait",
    "eject",
    "raise",
    "lower",
    "load",
    "dup",
    "reset",
    "unload",
    "close",
    "set",
    "interpolate",
    "has",
    "grab",
    "check",
    "find",
    "ungrab",
    "unset",
    "clear",
    "pop",
    "new",
    "peek",
    "push",
    "update",
    "show",
    "move",
    "hide",
    "calculate",
    "resize",
    "attach",
    "pack",
    "unpack",
    "emit",
    "call",
    "append"
  };

const std::vector<std::string> not_verbs =
  {
    "below",
    "above",
    "name",
    "unfreezable",
    "value",
    "r",
    "g",
    "b",
    "a",
    "finalize",
    "destructor",
    "to",
    "circle",
    "rect",
    "path",
    "commands",
    "type",
    "colorspace"
    "op",
    "type",
    "properties",
    "status",
    "status",
    "relative",
    "ptr",
    "pair",
    "pos",
    "end"
  };

void reorder_verb(std::vector<std::string> &names)
{
  if (names.size() <= 1)
    return;

  std::string verb = names.back();

  if (std::find(verbs.begin(), verbs.end(), verb) != verbs.end())
    {
       names.pop_back();
       names.insert(names.begin(), verb);
    }
}

inline std::string managed_namespace(std::string const& ns)
{
  return escape_keyword(utils::remove_all(ns, '_'));
}

inline std::string managed_method_name(std::string const& klass, std::string const& name)
{
  std::vector<std::string> names = utils::split(name, '_');

  name_helpers::reorder_verb(names);

  std::string candidate = escape_keyword(utils::to_pascal_case(names));

  // Some eolian methods have the same name as their parent class
  if (candidate == klass)
      candidate = "Do" + candidate;

  return candidate;
}

inline std::string managed_method_name(attributes::function_def const& f)
{
  return managed_method_name(f.klass.eolian_name, f.name);
}

inline std::string alias_full_eolian_name(attributes::alias_def const& alias)
{

   std::string eolian_name = utils::remove_all(alias.eolian_name, '_');
   return join_namespaces(alias.namespaces, '.') + eolian_name;
}

inline std::string managed_async_method_name(attributes::function_def const& f)
{
  return managed_method_name(f) + "Async";
}
inline std::string function_ptr_full_eolian_name(attributes::function_def const& func)
{
   return join_namespaces(func.namespaces, '.') + func.name;
}

inline std::string type_full_eolian_name(attributes::regular_type_def const& type)
{
   return join_namespaces(type.namespaces, '.') + type.base_type;
}

inline std::string type_full_managed_name(attributes::regular_type_def const& type)
{
   return join_namespaces(type.namespaces, '.', managed_namespace) + utils::remove_all(type.base_type, '_');
}

inline std::string struct_full_eolian_name(attributes::struct_def const& struct_)
{
   return join_namespaces(struct_.namespaces, '.') + struct_.cxx_name;
}

template<typename T>
inline std::string typedecl_managed_name(T const& item)
{
   return utils::remove_all(item.cxx_name, '_');
}

inline std::string typedecl_managed_name(attributes::function_def const& func)
{
   return utils::remove_all(func.name, '_');
}


inline std::string enum_field_managed_name(std::string name)
{
   std::vector<std::string> names = utils::split(name, '_');
   return utils::to_pascal_case(names);
}

inline std::string to_field_name(std::string const& in)
{
  return utils::capitalize(in);
}

inline std::string property_managed_name(const std::string name)
{
  auto names = utils::split(name, '_');
  // No need to escape keyword here as it will be capitalized and already
  // namespaced inside the owner class.
  return utils::to_pascal_case(names);
}

inline std::string property_managed_name(attributes::property_def const& property)
{
  return property_managed_name(property.name);
}

inline std::string managed_part_name(attributes::part_def const& part)
{
  std::vector<std::string> names = utils::split(part.name, '_');
  return utils::to_pascal_case(names);
}

// Class name translation (interface/concrete/inherit/etc)
struct klass_interface_name_generator
{

  template <typename T>
  std::string operator()(T const& klass) const
  {
    return utils::remove_all(klass.eolian_name, '_');
  }

  template <typename OutputIterator, typename Attr, typename Context>
  bool generate(OutputIterator sink, Attr const& attribute, Context const& context) const
  {
    return as_generator((*this).operator()<Attr>(attribute)).generate(sink, attributes::unused, context);
  }
} const klass_interface_name;

struct klass_full_interface_name_generator
{
  template <typename T>
  std::string operator()(T const& klass) const
  {
    return join_namespaces(klass.namespaces, '.', managed_namespace) + klass_interface_name(klass);
  }

  template <typename OutputIterator, typename Attr, typename Context>
  bool generate(OutputIterator sink, Attr const& attribute, Context const& context) const
  {
    return as_generator((*this).operator()<Attr>(attribute)).generate(sink, attributes::unused, context);
  }
} const klass_full_interface_name;

template<typename T>
inline std::string klass_concrete_name(T const& klass)
{
  std::string name = utils::remove_all(klass.eolian_name, '_');
  if (klass.type == attributes::class_type::regular || klass.type == attributes::class_type::abstract_)
    return name;
  return name + "Concrete";
}

struct klass_full_concrete_name_generator
{
  template <typename T>
  std::string operator()(T const& klass) const
  {
    return join_namespaces(klass.namespaces, '.', managed_namespace) + klass_concrete_name(klass);
  }

  template <typename OutputIterator, typename Attr, typename Context>
  bool generate(OutputIterator sink, Attr const& attribute, Context const& context) const
  {
    return as_generator((*this).operator()<Attr>(attribute)).generate(sink, attributes::unused, context);
  }
} const klass_full_concrete_name;

struct klass_full_concrete_or_interface_name_generator
{
  template <typename T>
  std::string operator()(T const& klass) const
  {
    switch(klass.type)
    {
    case attributes::class_type::abstract_:
    case attributes::class_type::regular:
      return klass_full_concrete_name(klass);
    default:
      return klass_full_interface_name(klass);
    }
  }

  template <typename OutputIterator, typename Context>
  bool generate(OutputIterator, attributes::unused_type, Context const&) const
  {
    return true;
  }

  template <typename OutputIterator, typename Attr, typename Context>
  bool generate(OutputIterator sink, Attr const& attribute, Context const& context) const
  {
    return as_generator((*this).operator()<Attr>(attribute)).generate(sink, attributes::unused, context);
  }
} const klass_full_concrete_or_interface_name;

template<typename T>
inline std::string klass_inherit_name(T const& klass)
{
  return klass_concrete_name(klass);
}

template<typename T>
inline std::string klass_native_inherit_name(T const& klass)
{
  switch(klass.type)
  {
  case attributes::class_type::abstract_:
  case attributes::class_type::regular:
    return klass_concrete_name(klass) + "NativeInherit";
  default:
    return klass_interface_name(klass) + "NativeInherit";
  }
}

template<typename T>
inline std::string klass_full_native_inherit_name(T const& klass)
{
  switch(klass.type)
  {
  case attributes::class_type::abstract_:
  case attributes::class_type::regular:
    return klass_full_concrete_name(klass) + "NativeInherit";
  default:
    return klass_full_interface_name(klass) + "NativeInherit";
  }
}

template<typename T>
inline std::string klass_get_name(T const& clsname)
{
  return clsname.klass_get_name;
}

template<typename T>
inline std::string klass_get_full_name(T const& clsname)
{
  return klass_full_concrete_name(clsname) + "." + klass_get_name(clsname);
}

// Events
inline std::string managed_event_name(std::string const& name)
{
   return utils::to_pascal_case(utils::split(name, ','), "") + "Evt";
}

inline std::string managed_event_args_short_name(attributes::event_def const& evt)
{
   std::string ret;
     ret = klass_interface_name(evt.klass);
   return ret + name_helpers::managed_event_name(evt.name) + "_Args";
}

inline std::string managed_event_args_name(attributes::event_def evt)
{
   return join_namespaces(evt.klass.namespaces, '.', managed_namespace) +
          managed_event_args_short_name(evt);
}

inline std::string translate_inherited_event_name(const attributes::event_def &evt, const attributes::klass_def &klass)
{
   return join_namespaces(klass.namespaces, '_') + klass_interface_name(klass) + "_" + managed_event_name(evt.name);
}

// Open/close namespaces
template<typename OutputIterator, typename Context>
bool open_namespaces(OutputIterator sink, std::vector<std::string> namespaces, Context context)
{
  std::transform(namespaces.begin(), namespaces.end(), namespaces.begin(), managed_namespace);

  auto open_namespace = *("namespace " << string << " { ") << "\n";
  return as_generator(open_namespace).generate(sink, namespaces, context);
}

template<typename OutputIterator, typename Context>
bool close_namespaces(OutputIterator sink, std::vector<std::string> const& namespaces, Context context)
{
     auto close_namespace = *(lit("} ")) << "\n";
     return as_generator(close_namespace).generate(sink, namespaces, context);
}



} // namespace name_helpers

} // namespace eolian_mono


namespace efl { namespace eolian { namespace grammar {

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

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

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

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

namespace type_traits {
template <>
struct attributes_needed<struct ::eolian_mono::name_helpers::klass_full_concrete_or_interface_name_generator> : std::integral_constant<int, 1> {};
}
      
} } }
      
#endif