summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/string.hpp
blob: fbbf1ecaab553e1977d791f2d8cc2deab5de5114 (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
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EOLIAN_CXX_STRING_HH
#define EOLIAN_CXX_STRING_HH

#include <cstdlib>
#include <cstring>

#include "grammar/generator.hpp"
#include "grammar/attributes.hpp"
#include "grammar/case.hpp"

namespace efl { namespace eolian { namespace grammar {

// literal
struct literal_generator
{
   literal_generator(const char* string)
     : string(string) {}

   template <typename OutputIterator, typename Attribute, typename Context>
   bool generate(OutputIterator sink, Attribute const&, Context const&) const
   {
      std::copy(string, string + std::strlen(string), sink);
      return true;
   }

   const char* string;
};

inline literal_generator as_generator(char const* literal) { return literal; }

struct {
  literal_generator operator()(const char* literal) const
  {
    return literal_generator(literal);
  }
} const lit = {};

// string
struct string_generator
{
   template <typename OutputIterator, typename Attribute, typename Context>
   bool generate(OutputIterator sink, Attribute const& attribute, Context const&) const
   {
      if(tag_check<upper_case_tag, Context>::value)
        {
          std::transform(std::begin(attribute), std::end(attribute), sink, &::toupper);
        }
      else if(tag_check<lower_case_tag, Context>::value)
        {
          std::transform(std::begin(attribute), std::end(attribute), sink, &::tolower);
        }
      else
        {
          std::copy(std::begin(attribute), std::end(attribute), sink);
        }
      return true;
   }
};

struct specific_string_generator
{
   specific_string_generator(std::string string) : string(string) {}
  
   template <typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::unused_type, Context const&) const
   {
      if(tag_check<upper_case_tag, Context>::value)
        {
          std::transform(std::begin(string), std::end(string), sink, &::toupper);
        }
      else if(tag_check<lower_case_tag, Context>::value)
        {
          std::transform(std::begin(string), std::end(string), sink, &::tolower);
        }
      else
        {
          std::copy(std::begin(string), std::end(string), sink);
        }
      return true;
   }

   std::string string;
};

struct string_replace_generator
{
   template <typename OutputIterator, typename Attribute, typename Context>
   bool generate(OutputIterator sink, Attribute const& string, Context const&) const
   {
      if(tag_check<upper_case_tag, Context>::value)
        {
          std::transform(std::begin(string), std::end(string), sink,
                         [&] (char c) -> char
                         {
                           if(c == from)
                             return to;
                           else
                             return ::toupper(c);
                         }
                         );
        }
      else if(tag_check<lower_case_tag, Context>::value)
        {
          std::transform(std::begin(string), std::end(string), sink,
                         [&] (char c) -> char
                         {
                           if(c == from)
                             return to;
                           else
                             return ::tolower(c);
                         });
        }
      else
        {
          std::transform(std::begin(string), std::end(string), sink
                         , [&] (char c) { return c == from ? to : c; });
        }
      return true;
   }

  char from, to;
};
      
struct string_generator_terminal
{
  specific_string_generator operator[](std::string string) const
  {
    return specific_string_generator{string};
  }
} const string = {};

struct string_replace_terminal
{
  string_replace_generator operator()(char from, char to) const
  {
    return string_replace_generator{from, to};
  }
} const string_replace = {};
      
template <>
struct is_eager_generator<literal_generator> : std::true_type {};
template <>
struct is_generator<literal_generator> : std::true_type {};
template <>
struct is_generator<const char*> : std::true_type {};

template <int N>
struct is_generator<const char[N]> : std::true_type {};
template <>
struct is_eager_generator<string_generator> : std::true_type {};
template <>
struct is_eager_generator<specific_string_generator> : std::true_type {};
template <>
struct is_eager_generator<string_replace_generator> : std::true_type {};
template <>
struct is_generator<string_generator> : std::true_type {};
template <>
struct is_generator<specific_string_generator> : std::true_type {};
template <>
struct is_generator<string_replace_generator> : std::true_type {};

template <>
struct is_generator<string_generator_terminal> : std::true_type {};
template <>
struct is_generator<std::string> : std::true_type {};

inline string_generator as_generator(string_generator_terminal)
{
  return string_generator{};
}
namespace type_traits {
template <>
struct attributes_needed<string_generator> : std::integral_constant<int, 1> {};  
template <>
struct attributes_needed<string_generator_terminal> : std::integral_constant<int, 1> {};  
template <>
struct attributes_needed<string_replace_generator> : std::integral_constant<int, 1> {};  
}      
      
} } }

namespace std {

inline ::efl::eolian::grammar::specific_string_generator as_generator(std::string string)
{
  return ::efl::eolian::grammar::specific_string_generator{string};
}
  
}

#endif