-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
tag.hpp
517 lines (430 loc) · 10.7 KB
/
tag.hpp
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
/*
Copyright (C) 2011 - 2018 by Sytyi Nick <nsytyi@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* This file contains objects "tag" and "key", which are used to store
* information about tags and keys while annotation parsing.
*/
#pragma once
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
class config;
namespace schema_validation
{
/**
* Stores information about a schema type.
* This is an abstract base class for several variants of schema type.
*/
class class_type {
protected:
std::string name_;
public:
class_type() = delete;
class_type(const std::string& name) : name_(name) {}
using map = std::map<std::string, std::shared_ptr<class_type>>;
virtual bool matches(const std::string& value, const map& type_map) const = 0;
static std::shared_ptr<class_type> from_config(const config& cfg);
};
/**
* Stores information about a schema type.
* This type represents a simple pattern match.
*/
class class_type_simple : public class_type {
boost::regex pattern_;
public:
class_type_simple(const std::string& name, const std::string& pattern) : class_type(name), pattern_(pattern) {}
bool matches(const std::string& value, const map& type_map) const override;
};
/**
* Stores information about a schema type.
* This type represents a name alias for another type.
*/
class class_type_alias : public class_type {
mutable std::shared_ptr<class_type> cached_;
std::string link_;
public:
class_type_alias(const std::string& name, const std::string& link) : class_type(name), link_(link) {}
bool matches(const std::string& value, const map& type_map) const override;
};
/**
* Stores information about a schema type.
* This is an abstract base class for composite types.
*/
class class_type_composite : public class_type {
protected:
std::vector<std::shared_ptr<class_type>> subtypes_;
public:
class_type_composite(const std::string& name) : class_type(name) {}
void add_type(std::shared_ptr<class_type> type)
{
subtypes_.push_back(type);
}
};
/**
* Stores information about a schema type.
* Represents a union type, which matches if any of its subtypes match.
*/
class class_type_union : public class_type_composite {
public:
class_type_union(const std::string& name) : class_type_composite(name) {}
bool matches(const std::string& value, const map& type_map) const override;
};
/**
* Stores information about a schema type.
* Represents an intersection type, which matches if all of its subtypes match.
*/
class class_type_intersection : public class_type_composite {
public:
class_type_intersection(const std::string& name) : class_type_composite(name) {}
bool matches(const std::string& value, const map& type_map) const override;
};
/**
* Stores information about a schema type.
* Represents a list type, where each list element is itself a union.
*/
class class_type_list : public class_type_union {
boost::regex split_;
int min_ = 0, max_ = -1;
public:
class_type_list(const std::string& name, const std::string& pattern, int min, int max)
: class_type_union(name)
, split_(pattern)
, min_(min)
, max_(max)
{}
bool matches(const std::string& value, const map& type_map) const override;
};
/**
* class_key is used to save the information about one key.
* Key has next info: name, type, default value or key is mandatory.
*/
class class_key
{
public:
class_key()
: name_("")
, type_("")
, default_("\"\"")
, mandatory_(false)
, fuzzy_(false)
{
}
class_key(const std::string& name, const std::string& type, const std::string& def = "\"\"")
: name_(name)
, type_(type)
, default_(def)
, mandatory_(def.empty())
, fuzzy_(name.find_first_of("*?") != std::string::npos)
{
}
class_key(const config&);
const std::string& get_name() const
{
return name_;
}
const std::string& get_type() const
{
return type_;
}
const std::string& get_default() const
{
return default_;
}
bool is_mandatory() const
{
return mandatory_;
}
bool is_fuzzy() const {
return fuzzy_;
}
void set_name(const std::string& name)
{
name_ = name;
}
void set_type(const std::string& type)
{
type_ = type;
}
void set_default(const std::string& def)
{
default_ = def;
if(def.empty()) {
mandatory_ = true;
}
}
void set_mandatory(bool mandatory)
{
mandatory_ = mandatory;
}
void set_fuzzy(bool f)
{
fuzzy_ = f;
}
/** is used to print key info
* the format is next
* [key]
* name="name"
* type="type"
* default="default"
* mandatory="true/false"
* [/key]
*/
void print(std::ostream& os, int level) const;
/** Compares keys by name. Used in std::sort, i.e. */
bool operator<(const class_key& k) const
{
return (get_name() < k.get_name());
}
private:
/** Name of key. */
std::string name_;
/** Type of key. */
std::string type_;
/** Default value. */
std::string default_;
/** Shows, if key is a mandatory key. */
bool mandatory_;
/** Whether the key is a fuzzy match. */
bool fuzzy_;
};
/**
* Stores information about tag.
* Each tags is an element of great tag tree. This tree is close to filesystem:
* you can use links and special include directory global/
* Normally root is not mentioned in path.
* Each tag has name, minimum and maximum occasions number,
* and lists of subtags, keys and links.
*/
class class_tag
{
public:
using tag_map = std::map<std::string, class_tag>;
using key_map = std::map<std::string, class_key>;
using link_map = std::map<std::string, std::string>;
class_tag()
: name_("")
, min_(0)
, max_(0)
, super_("")
, tags_()
, keys_()
, links_()
, fuzzy_(false)
, any_tag_(false)
{
}
class_tag(const std::string& name, int min, int max, const std::string& super = "", bool any = false)
: name_(name)
, min_(min)
, max_(max)
, super_(super)
, tags_()
, keys_()
, links_()
, fuzzy_(name.find_first_of("*?") != std::string::npos)
, any_tag_(any)
{
}
class_tag(const config&);
~class_tag()
{
}
/** Prints information about tag to outputstream, recursively
* is used to print tag info
* the format is next
* [tag]
* subtags
* keys
* name="name"
* min="min"
* max="max"
* [/tag]
*/
void print(std::ostream& os);
const std::string& get_name() const
{
return name_;
}
int get_min() const
{
return min_;
}
int get_max() const
{
return max_;
}
const std::string& get_super() const
{
return super_;
}
bool is_extension() const
{
return !super_.empty();
}
bool is_fuzzy() const {
return fuzzy_;
}
bool accepts_any_tag() const {
return any_tag_;
}
void set_name(const std::string& name)
{
name_ = name;
}
void set_min(int o)
{
min_ = o;
}
void set_max(int o)
{
max_ = o;
}
void set_min(const std::string& s)
{
std::istringstream i(s);
if(!(i >> min_)) {
min_ = 0;
}
}
void set_max(const std::string& s)
{
std::istringstream i(s);
if(!(i >> max_)) {
max_ = 0;
}
}
void set_super(const std::string& s)
{
super_ = s;
}
void set_fuzzy(bool f) {
fuzzy_ = f;
}
void set_any_tag(bool any) {
any_tag_ = any;
}
void add_key(const class_key& new_key)
{
keys_.emplace(new_key.get_name(), new_key);
}
void add_tag(const class_tag& new_tag)
{
tags_.emplace(new_tag.name_, new_tag);
}
void add_link(const std::string& link);
/**
* Tags are usually organized in tree.
* This function helps to add a tag to his exact place in tree
* @param path - path in subtree to exact place of tag
* @param tag - tag to add
* @param root - root of schema tree - use to support of adding to link.
* Path is getting shotter and shoter with each call.
* Path should look like tag1/tag2/parent/ Slash at end is mandatory.
*/
void add_tag(const std::string& path, const class_tag& tag, class_tag& root);
bool operator<(const class_tag& t) const
{
return name_ < t.name_;
}
bool operator==(const class_tag& other) const
{
return name_ == other.name_;
}
/** Returns pointer to child key. */
const class_key* find_key(const std::string& name) const;
/** Returns pointer to child link. */
const std::string* find_link(const std::string& name) const;
/**
* Returns pointer to tag using full path to it.
* Also work with links
*/
const class_tag* find_tag(const std::string& fullpath, const class_tag& root) const;
/** Calls the expansion on each child. */
void expand_all(class_tag& root);
const tag_map& tags() const
{
return tags_;
}
const key_map& keys() const
{
return keys_;
}
const link_map& links() const
{
return links_;
}
void remove_key_by_name(const std::string& name)
{
keys_.erase(name);
}
/** Removes all keys with this type. Works recursively */
void remove_keys_by_type(const std::string& type);
private:
/** name of tag. */
std::string name_;
/** number of minimum occasions. */
int min_;
/** number of maximum occasions. */
int max_;
/**
* name of tag to extend "super-tag"
* Extension is smth like inheritance and is used in case
* when you need to use another tag with all his
* keys, children, etc. But you also want to allow extra subtags of that tags,
* so just linking that tag wouldn't help at all.
*/
std::string super_;
/** children tags*/
tag_map tags_;
/** keys*/
key_map keys_;
/** links to possible children. */
link_map links_;
/** whether this is a "fuzzy" tag. */
bool fuzzy_;
/** whether this tag allows arbitrary subtags. */
bool any_tag_;
/**
* the same as class_tag::print(std::ostream&)
* but indents different levels with step space.
* @param os stream to print
* @param level current level of indentation
* @param step step to next indent
*/
void printl(std::ostream& os, int level, int step = 4);
class_tag* find_tag(const std::string & fullpath, class_tag & root)
{
return const_cast<class_tag*>(const_cast<const class_tag*>(this)->find_tag(fullpath, root));
}
void add_tags(const tag_map& list)
{
tags_.insert(list.begin(), list.end());
}
void add_keys(const key_map& list)
{
keys_.insert(list.begin(), list.end());
}
void add_links(const link_map& list)
{
links_.insert(list.begin(), list.end());
}
/** Copies tags, keys and links of tag to this. */
void append_super(const class_tag& tag, const std::string& super);
/** Expands all "super" copying their data to this. */
void expand(class_tag& root);
};
}