31 : name_(cfg[
"name"].str())
32 , min_(cfg[
"min"].to_int())
33 , max_(cfg[
"max"].str() ==
"infinite" ? -1 : cfg[
"max"].to_int(1))
34 , min_children_(cfg[
"min_tags"].to_int())
35 , max_children_(cfg[
"max_tags"].str() ==
"infinite" ? -1 : cfg[
"max_tags"].to_int(-1))
42 , fuzzy_(name_.find_first_of(
"*?+") != std::string::npos)
43 , any_tag_(cfg[
"any_tag"].to_bool())
53 super_ = cfg[
"super"].str();
67 std::string link_name = link[
"name"].str();
87 std::istringstream
i(
s);
95 std::istringstream
i(
s);
103 std::string::size_type pos_last = link.rfind(
'/');
105 std::string name_link = link.substr(pos_last + 1, link.length());
106 links_.emplace(name_link, link);
111 auto visited = std::vector<const wml_tag*>();
112 return find_key(name, match, ignore_super, visited);
118 if(std::find(visited.begin(), visited.end(),
this) != visited.end()) {
122 visited.push_back(
this);
127 if(cond.matches(match)) {
129 if(
auto key = cond.find_key(name, match,
true)) {
135 const auto it_keys =
keys_.find(name);
136 if(it_keys !=
keys_.end()) {
137 return &(it_keys->second);
140 key_map::const_iterator it_fuzzy = std::find_if(
keys_.begin(),
keys_.end(), [&name](
const key_map::value_type& key){
141 if(!key.second.is_fuzzy()) {
146 if(it_fuzzy != keys_.end()) {
147 return &(it_fuzzy->second);
151 for(
auto& cond : conditions_) {
152 if(cond.matches(match)) {
154 if(
auto key = cond.find_key(name, match,
false, visited)) {
159 for(
auto& [
_, super_tag] : super_refs_) {
160 if(
const wml_key* found_key = super_tag->find_key(name, match,
false, visited)) {
169 const std::string* wml_tag::find_link(
const std::string& name)
const
171 const auto it_links = links_.find(name);
172 if(it_links != links_.end()) {
173 return &(it_links->second);
179 const wml_tag* wml_tag::find_tag(
const std::string& fullpath,
const wml_tag& root,
const config& match,
bool ignore_super)
const
181 auto visited = std::vector<const wml_tag*>();
182 return find_tag(fullpath, root, match, ignore_super, visited);
185 const wml_tag* wml_tag::find_tag(
const std::string& fullpath,
const wml_tag& root,
const config& match,
bool ignore_super, std::vector<const wml_tag*>& visited)
const
188 if(std::find(visited.begin(), visited.end(),
this) != visited.end()) {
192 visited.push_back(
this);
194 if(fullpath.empty()) {
198 std::string::size_type pos = fullpath.find(
'/');
200 std::string next_path;
202 if(pos != std::string::npos) {
203 name = fullpath.substr(0, pos);
204 next_path = fullpath.substr(pos + 1, fullpath.length());
211 for(
auto& cond : conditions_) {
212 if(cond.matches(match)) {
214 if(
auto tag = cond.find_tag(fullpath, root, match,
true)) {
220 const auto it_tags = tags_.find(name);
221 if(it_tags != tags_.end()) {
222 if(next_path.empty()) {
223 return &(it_tags->second);
225 return it_tags->second.
find_tag(next_path, root, match,
false, visited);
229 const auto it_links = links_.find(name);
230 if(it_links != links_.end()) {
232 return root.
find_tag(it_links->second +
"/" + next_path, root, match,
false);
235 const auto it_fuzzy = std::find_if(tags_.begin(), tags_.end(), [&name](
const tag_map::value_type& tag) {
236 if(!tag.second.fuzzy_) {
241 if(it_fuzzy != tags_.end()) {
242 if(next_path.empty()) {
243 return &(it_fuzzy->second);
245 return it_tags->second.
find_tag(next_path, root, match,
false, visited);
250 for(
auto& cond : conditions_) {
251 if(cond.matches(match)) {
253 if(
auto tag = cond.find_tag(fullpath, root, match,
false, visited)) {
258 for(
auto& [
_, super_tag] : super_refs_) {
259 if(
const wml_tag* found_tag = super_tag->find_tag(fullpath, root, match,
false, visited)) {
274 for(
auto& tag : tags_) {
276 tag.second.expand_all(root);
278 for(
auto& cond : conditions_) {
280 cond.expand_all(root);
284 void wml_tag::remove_keys_by_type(
const std::string&
type)
286 auto i = keys_.begin();
287 while(
i != keys_.end()) {
288 if(
i->second.get_type() ==
type) {
295 for(
auto& tag : tags_) {
296 tag.second.remove_keys_by_type(
type);
300 void wml_tag::printl(std::ostream& os,
int level,
int step)
303 for(
int j = 0; j <
level; j++) {
308 <<
s <<
" name=\"" << name_ <<
"\"\n"
309 <<
s <<
" min=\"" << min_ <<
"\"\n"
310 <<
s <<
" max=\"" << max_ <<
"\"\n";
312 if(!super_.empty()) {
313 os <<
s <<
" super=\"" << super_ <<
"\"\n";
316 for(
auto& tag : tags_) {
317 tag.second.printl(os,
level + step, step);
320 for(
auto& link : links_) {
324 <<
" name=\"" << link.second <<
"\"\n"
329 for(
auto& key : keys_) {
330 key.second.print(os,
level + step);
335 os <<
s <<
"[/tag]\n";
341 auto it = tags_.find(tag.
name_);
343 if(it == tags_.end()) {
344 tags_.emplace(tag.
name_, tag);
346 it->second.set_min(tag.
min_);
347 it->second.set_max(tag.
max_);
348 it->second.add_tags(tag.
tags_);
349 it->second.add_keys(tag.
keys_);
350 it->second.add_links(tag.
links_);
358 std::string::size_type pos =
path.find(
'/');
359 std::string name =
path.substr(0, pos);
360 std::string next_path =
path.substr(pos + 1,
path.length());
362 auto it_links = links_.find(name);
363 if(it_links != links_.end()) {
364 root.
add_tag(it_links->second +
"/" + next_path, tag, root);
367 auto it_tags = tags_.find(name);
368 if(it_tags == tags_.end()) {
371 subtag.
add_tag(next_path, tag, root);
372 tags_.emplace(name, subtag);
376 it_tags->second.add_tag(next_path, tag, root);
381 conditions_.insert(conditions_.end(), list.begin(), list.end());
389 if(super_tag !=
this) {
390 super_refs_.emplace(super, super_tag);
396 void wml_tag::add_switch(
const config& switch_cfg)
399 const std::string key = switch_cfg[
"key"];
400 bool allow_missing =
false;
401 for(
const auto& case_cfg : switch_cfg.
child_range(
"case")) {
402 if(case_cfg.has_attribute(
"value")) {
405 for(
const auto& value : values) {
413 default_cfg.
add_child(
"not")[key] = value;
415 if(!allow_missing && case_cfg[
"trigger_if_missing"].to_bool()) {
417 missing_filter[
"glob_on_" + key] =
"*";
418 allow_missing =
true;
420 conditions_.emplace_back(case_cfg, filter);
423 conditions_.emplace_back(case_cfg,
config());
425 const std::string name =
formatter() << get_name() <<
'[' << key <<
'=' << case_cfg[
"value"] <<
']';
426 conditions_.back().set_name(name);
433 default_cfg.
add_child(
"and")[
"glob_on_" + key] =
"*";
435 conditions_.emplace_back(switch_cfg.
mandatory_child(
"else"), default_cfg);
436 const std::string name =
formatter() << get_name() <<
"[else]";
437 conditions_.back().set_name(name);
441 void wml_tag::add_filter(
const config& cond_cfg)
443 config filter = cond_cfg, else_filter;
447 else_filter.add_child(
"not", filter);
450 const std::string name =
formatter() << get_name() <<
"[then]";
451 conditions_.back().set_name(name);
454 for(
auto elseif_cfg : cond_cfg.
child_range(
"elseif")) {
455 config elseif_filter = elseif_cfg, old_else_filter = else_filter;
457 else_filter.add_child(
"not", elseif_filter);
460 conditions_.emplace_back(elseif_cfg.child_or_empty(
"then"), elseif_filter);
461 const std::string name =
formatter() << get_name() <<
"[elseif " <<
i++ <<
"]";
462 conditions_.back().set_name(name);
465 conditions_.emplace_back(cond_cfg.
mandatory_child(
"else"), else_filter);
466 const std::string name =
formatter() << get_name() <<
"[else]";
467 conditions_.back().set_name(name);
471 bool wml_condition::matches(
const config& cfg)
const
486 current = base_tag.
tags_.begin();
487 condition_queue.push(&base_tag);
491 void wml_tag::tag_iterator::ensure_valid_or_end() {
492 while(current == condition_queue.front()->tags_.end()) {
493 condition_queue.pop();
494 if(condition_queue.empty()) {
497 const wml_tag& new_base = *condition_queue.front();
498 current= new_base.
tags_.begin();
499 push_new_tag_conditions(new_base);
506 current = base_tag.
keys_.begin();
507 condition_queue.push(&base_tag);
511 void wml_tag::key_iterator::ensure_valid_or_end() {
512 while(current == condition_queue.front()->keys_.end()) {
513 condition_queue.pop();
514 if(condition_queue.empty()) {
517 const wml_tag& new_base = *condition_queue.front();
518 current = new_base.keys_.begin();
519 push_new_tag_conditions(new_base);
526 current = base_tag.super_refs_.begin();
527 condition_queue.push(&base_tag);
531 void wml_tag::super_iterator::ensure_valid_or_end()
533 while(current == condition_queue.front()->super_refs_.end()) {
534 condition_queue.pop();
535 if(condition_queue.empty()) {
538 const wml_tag& new_base = *condition_queue.front();
539 current = new_base.super_refs_.begin();
540 push_new_tag_conditions(new_base);
544 void wml_tag::push_new_tag_conditions(std::queue<const wml_tag*>& q,
const config& match,
const wml_tag& tag)
547 if(condition.matches(match)) {
A config object defines a single node in a WML file, with access to child nodes.
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
bool matches(const config &filter) const
void clear_children(T... keys)
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
bool has_attribute(config_key_type key) const
child_itors child_range(config_key_type key)
void append_children(const config &cfg)
Adds children from cfg.
config & add_child(config_key_type key)
wml_key is used to save the information about one key.
Stores information about tag.
void add_link(const std::string &link)
void expand(wml_tag &root)
Expands all "super", storing direct references for easier access.
const std::string & get_name() const
std::string super_
name of tag to extend "super-tag" Extension is smth like inheritance and is used in case when you nee...
const wml_tag * find_tag(const std::string &fullpath, const wml_tag &root, const config &match, bool ignore_super=false) const
Returns pointer to tag using full path to it.
void set_name(const std::string &name)
void add_filter(const config &cond_cfg)
void add_tag(const wml_tag &new_tag)
void print(std::ostream &os)
Prints information about tag to outputstream, recursively is used to print tag info the format is nex...
link_map links_
links to possible children.
std::string name_
name of tag.
int min_
minimum number of occurrences.
void add_switch(const config &switch_cfg)
condition_list conditions_
conditional partial matches
std::vector< wml_condition > condition_list
const wml_key * find_key(const std::string &name, const config &match, bool ignore_super=false) const
Returns pointer to child key.
tag_map tags_
children tags
void add_key(const wml_key &new_key)
int max_
maximum number of occurrences.
void printl(std::ostream &os, int level, int step=4)
the same as wml_tag::print(std::ostream&) but indents different levels with step space.
int max_children_
maximum number of children.
static std::string _(const char *str)
wml_tag any_tag("", 0, -1, "", true)
struct utils::detail::formula_initer init
@ STRIP_SPACES
REMOVE_EMPTY: remove empty elements.
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), '+' as one or more characters,...
std::vector< std::string > split(const config_attribute_value &val)
This file contains object "tag", which is used to store information about tags while annotation parsi...
static map_location::DIRECTION sw
static map_location::DIRECTION s