diff options
Diffstat (limited to 'verbly/token.h')
| -rw-r--r-- | verbly/token.h | 336 | 
1 files changed, 336 insertions, 0 deletions
| diff --git a/verbly/token.h b/verbly/token.h new file mode 100644 index 0000000..bbe7c2d --- /dev/null +++ b/verbly/token.h | |||
| @@ -0,0 +1,336 @@ | |||
| 1 | #ifndef TOKEN_H_AD62C505 | ||
| 2 | #define TOKEN_H_AD62C505 | ||
| 3 | |||
| 4 | #include <string> | ||
| 5 | #include <list> | ||
| 6 | #include <sstream> | ||
| 7 | #include "verb.h" | ||
| 8 | |||
| 9 | namespace verbly { | ||
| 10 | |||
| 11 | enum class type { | ||
| 12 | verb, | ||
| 13 | fillin, | ||
| 14 | string, | ||
| 15 | utterance | ||
| 16 | }; | ||
| 17 | |||
| 18 | class selrestr { | ||
| 19 | }; | ||
| 20 | |||
| 21 | class synrestr { | ||
| 22 | }; | ||
| 23 | |||
| 24 | enum class fillin_type { | ||
| 25 | noun_phrase, | ||
| 26 | participle_phrase, | ||
| 27 | adjective | ||
| 28 | }; | ||
| 29 | |||
| 30 | class token { | ||
| 31 | protected: | ||
| 32 | // General | ||
| 33 | type type; | ||
| 34 | |||
| 35 | token(enum type type) : type(type) | ||
| 36 | { | ||
| 37 | |||
| 38 | } | ||
| 39 | |||
| 40 | public: | ||
| 41 | enum type token_type() const | ||
| 42 | { | ||
| 43 | return type; | ||
| 44 | } | ||
| 45 | |||
| 46 | virtual bool complete() const = 0; | ||
| 47 | virtual std::string compile() const = 0; | ||
| 48 | virtual token* copy() const = 0; | ||
| 49 | }; | ||
| 50 | |||
| 51 | class verb_token : public token { | ||
| 52 | private: | ||
| 53 | // Verb | ||
| 54 | const verb* m_verb; | ||
| 55 | conjugation verb_infl = conjugation::infinitive; | ||
| 56 | |||
| 57 | public: | ||
| 58 | verb_token(const class verb& verb) : token(type::verb), m_verb(&verb) | ||
| 59 | { | ||
| 60 | |||
| 61 | } | ||
| 62 | |||
| 63 | const class verb& verb() const | ||
| 64 | { | ||
| 65 | return *m_verb; | ||
| 66 | } | ||
| 67 | |||
| 68 | verb_token& conjugate(conjugation infl) | ||
| 69 | { | ||
| 70 | verb_infl = infl; | ||
| 71 | return *this; | ||
| 72 | } | ||
| 73 | |||
| 74 | bool complete() const | ||
| 75 | { | ||
| 76 | return true; | ||
| 77 | } | ||
| 78 | |||
| 79 | std::string compile() const | ||
| 80 | { | ||
| 81 | return m_verb->conjugate(verb_infl); | ||
| 82 | } | ||
| 83 | |||
| 84 | token* copy() const | ||
| 85 | { | ||
| 86 | return new verb_token(*this); | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | class utterance_token : public token { | ||
| 91 | private: | ||
| 92 | // Utterance | ||
| 93 | std::list<std::unique_ptr<token>> utterance; | ||
| 94 | |||
| 95 | public: | ||
| 96 | typedef std::list<std::unique_ptr<token>>::iterator iterator; | ||
| 97 | /*class iterator { | ||
| 98 | private: | ||
| 99 | friend class utterance_token; | ||
| 100 | |||
| 101 | std::list<std::unique_ptr<token>>::iterator it; | ||
| 102 | |||
| 103 | public: | ||
| 104 | iterator(std::list<std::unique_ptr<token>>::iterator it) : it(it) | ||
| 105 | { | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | iterator& operator++() | ||
| 110 | { | ||
| 111 | ++it; | ||
| 112 | return *this; | ||
| 113 | } | ||
| 114 | |||
| 115 | iterator& operator--() | ||
| 116 | { | ||
| 117 | --it; | ||
| 118 | return *this; | ||
| 119 | } | ||
| 120 | |||
| 121 | bool operator==(const iterator& other) const | ||
| 122 | { | ||
| 123 | return it == other.it; | ||
| 124 | } | ||
| 125 | |||
| 126 | bool operator!=(const iterator& other) const | ||
| 127 | { | ||
| 128 | return it != other.it; | ||
| 129 | } | ||
| 130 | |||
| 131 | token* operator*() | ||
| 132 | { | ||
| 133 | return *it->get(); | ||
| 134 | } | ||
| 135 | |||
| 136 | token* operator->() | ||
| 137 | { | ||
| 138 | return *it->get(); | ||
| 139 | } | ||
| 140 | };*/ | ||
| 141 | |||
| 142 | utterance_token(std::initializer_list<token*> tkns) : token(type::utterance) | ||
| 143 | { | ||
| 144 | for (auto tkn : tkns) | ||
| 145 | { | ||
| 146 | utterance.push_back(std::unique_ptr<token>(tkn)); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | utterance_token(const utterance_token& other) : token(type::utterance) | ||
| 151 | { | ||
| 152 | for (auto& tkn : other.utterance) | ||
| 153 | { | ||
| 154 | utterance.push_back(std::unique_ptr<token>(tkn->copy())); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | utterance_token(utterance_token&& other) : token(type::utterance), utterance(std::move(other.utterance)) | ||
| 159 | { | ||
| 160 | |||
| 161 | } | ||
| 162 | |||
| 163 | utterance_token& operator=(const utterance_token& other) | ||
| 164 | { | ||
| 165 | utterance.clear(); | ||
| 166 | |||
| 167 | for (auto& tkn : other.utterance) | ||
| 168 | { | ||
| 169 | utterance.push_back(std::unique_ptr<token>(tkn->copy())); | ||
| 170 | } | ||
| 171 | |||
| 172 | return *this; | ||
| 173 | } | ||
| 174 | |||
| 175 | utterance_token& operator=(utterance_token&& other) | ||
| 176 | { | ||
| 177 | utterance = std::move(other.utterance); | ||
| 178 | |||
| 179 | return *this; | ||
| 180 | } | ||
| 181 | |||
| 182 | iterator begin() | ||
| 183 | { | ||
| 184 | return std::begin(utterance); | ||
| 185 | } | ||
| 186 | |||
| 187 | iterator end() | ||
| 188 | { | ||
| 189 | return std::end(utterance); | ||
| 190 | } | ||
| 191 | |||
| 192 | const iterator begin() const | ||
| 193 | { | ||
| 194 | return std::begin(utterance); | ||
| 195 | } | ||
| 196 | |||
| 197 | const iterator end() const | ||
| 198 | { | ||
| 199 | return std::end(utterance); | ||
| 200 | } | ||
| 201 | |||
| 202 | void erase(iterator it) | ||
| 203 | { | ||
| 204 | utterance.erase(it); | ||
| 205 | } | ||
| 206 | |||
| 207 | bool complete() const | ||
| 208 | { | ||
| 209 | return std::all_of(std::begin(utterance), std::end(utterance), [] (const std::unique_ptr<token>& tkn) { | ||
| 210 | return tkn->complete(); | ||
| 211 | }); | ||
| 212 | } | ||
| 213 | |||
| 214 | std::string compile() const | ||
| 215 | { | ||
| 216 | std::stringstream result; | ||
| 217 | for (auto& t : utterance) | ||
| 218 | { | ||
| 219 | if (t->complete()) | ||
| 220 | { | ||
| 221 | result << t->compile() << " "; | ||
| 222 | } else { | ||
| 223 | return "Could not compile!"; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | std::string output = result.str(); | ||
| 228 | if (output != "") | ||
| 229 | { | ||
| 230 | output.pop_back(); | ||
| 231 | } | ||
| 232 | |||
| 233 | return output; | ||
| 234 | } | ||
| 235 | |||
| 236 | token* copy() const | ||
| 237 | { | ||
| 238 | return new utterance_token(*this); | ||
| 239 | } | ||
| 240 | }; | ||
| 241 | |||
| 242 | class fillin_token : public token { | ||
| 243 | private: | ||
| 244 | // Fillin | ||
| 245 | std::string m_theme; | ||
| 246 | fillin_type m_fillin_type; | ||
| 247 | |||
| 248 | public: | ||
| 249 | fillin_token(fillin_type ft) : token(type::fillin), m_fillin_type(ft) | ||
| 250 | { | ||
| 251 | |||
| 252 | } | ||
| 253 | |||
| 254 | /* void synrestrs(std::initializer_list<synrestr> ins) | ||
| 255 | { | ||
| 256 | m_synrestrs = std::set<synrestr>(ins); | ||
| 257 | } | ||
| 258 | |||
| 259 | std::set<synrestr>& synrestrs() | ||
| 260 | { | ||
| 261 | return m_synrestrs; | ||
| 262 | } | ||
| 263 | |||
| 264 | void selrestrs(std::initializer_list<selrestr> ins) | ||
| 265 | { | ||
| 266 | m_selrestrs = std::set<selrestr>(ins); | ||
| 267 | } | ||
| 268 | |||
| 269 | std::set<selrestr>& selrestrs() | ||
| 270 | { | ||
| 271 | return m_selrestrs; | ||
| 272 | }*/ | ||
| 273 | |||
| 274 | fillin_token theme(std::string theme) | ||
| 275 | { | ||
| 276 | m_theme = theme; | ||
| 277 | |||
| 278 | return *this; | ||
| 279 | } | ||
| 280 | |||
| 281 | std::string& theme() | ||
| 282 | { | ||
| 283 | return m_theme; | ||
| 284 | } | ||
| 285 | |||
| 286 | fillin_type fillin_type() const | ||
| 287 | { | ||
| 288 | return m_fillin_type; | ||
| 289 | } | ||
| 290 | |||
| 291 | bool complete() const | ||
| 292 | { | ||
| 293 | return false; | ||
| 294 | } | ||
| 295 | |||
| 296 | std::string compile() const | ||
| 297 | { | ||
| 298 | return ""; | ||
| 299 | } | ||
| 300 | |||
| 301 | token* copy() const | ||
| 302 | { | ||
| 303 | return new fillin_token(*this); | ||
| 304 | } | ||
| 305 | }; | ||
| 306 | |||
| 307 | class string_token : public token { | ||
| 308 | private: | ||
| 309 | // String | ||
| 310 | std::string str; | ||
| 311 | |||
| 312 | public: | ||
| 313 | string_token(std::string str) : token(type::string), str(str) | ||
| 314 | { | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | bool complete() const | ||
| 319 | { | ||
| 320 | return true; | ||
| 321 | } | ||
| 322 | |||
| 323 | std::string compile() const | ||
| 324 | { | ||
| 325 | return str; | ||
| 326 | } | ||
| 327 | |||
| 328 | token* copy() const | ||
| 329 | { | ||
| 330 | return new string_token(*this); | ||
| 331 | } | ||
| 332 | }; | ||
| 333 | |||
| 334 | }; | ||
| 335 | |||
| 336 | #endif /* end of include guard: TOKEN_H_AD62C505 */ | ||
