summary refs log tree commit diff stats
path: root/lib/token.h
blob: a18fe42b2a48e443ebfba2b5ac973b4d5dec65c9 (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
#ifndef TOKEN_H_AD62C505
#define TOKEN_H_AD62C505

#include <ostream>
#include <string>
#include <list>
#include <set>
#include <variant>
#include <hkutil/recptr.h>
#include "enums.h"
#include "word.h"
#include "part.h"

namespace verbly {

  class token {
    public:
      enum class type {
        word,
        literal,
        part,
        fillin,
        utterance,
        transform
      };

      // Accessors

      type getType() const
      {
        return type_;
      }

      bool isComplete() const;

      std::string compile() const;

      bool isEmpty() const
      {
        return (type_ == type::utterance &&
          std::get<utterance_type>(variant_).empty());
      }

      // Word

      token(word arg, inflection category = inflection::base);

      const word& getWord() const;

      token inflect(inflection category) const;

      // Literal

      token(std::string arg);
      token(const char* arg);

      const std::string& getLiteral() const;

      // Part

      token(part arg);

      const part& getPart() const;

      // Fillin

      token(std::set<std::string> synrestrs);

      const std::set<std::string>& getSynrestrs() const;

      bool hasSynrestr(std::string synrestr) const;

      void addSynrestr(std::string synrestr);

      // Utterance

      using iterator = std::list<token>::iterator;
      using const_iterator = std::list<token>::const_iterator;

      token();
      token(std::vector<part> parts);
      token(std::initializer_list<token> pieces);

      iterator begin();
      const_iterator begin() const;

      iterator end();
      const_iterator end() const;

      token& operator<<(token arg);

      // Transform

      enum class casing {
        normal,
        capitalize,
        all_caps,
        title_case
      };

      static token separator(std::string param, token inner);
      static token punctuation(std::string param, token inner);
      static token indefiniteArticle(token inner);
      static token capitalize(casing param, token inner);
      static token quote(std::string open, std::string close, token inner);

      token& getInnerToken();
      const token& getInnerToken() const;

    private:

      std::string compileHelper(
        std::string separator,
        bool indefiniteArticle,
        casing capitalization) const;

      enum class transform_mode {
        separator,
        punctuation,
        indefinite_article,
        capitalize,
        quote
      };

      token(
        transform_mode type,
        std::string param1,
        std::string param2,
        token inner);

      token(
        transform_mode type,
        casing param,
        token inner);

      struct word_type {
        word value;
        inflection category;
      };

      using literal_type = std::string;

      using fillin_type = std::set<std::string>;

      using utterance_type = std::list<token>;

      struct transform_type {
        transform_mode type;
        std::string strParam;
        std::string strParam2;
        casing casingParam;
        hatkirby::recptr<token> inner;
      };

      using variant_type =
        std::variant<
          word_type,
          literal_type,
          part,
          fillin_type,
          utterance_type,
          transform_type>;

      type type_;
      variant_type variant_;
  };

  std::ostream& operator<<(std::ostream& os, token::type type);

};

#endif /* end of include guard: TOKEN_H_AD62C505 */