31 wml_tag::wml_tag(
const std::string& name,
int min,
int max,
const std::string& super,
bool any)
36 , fuzzy_(name.find_first_of(
"*?") != std::string::npos)
42 : name_(
cfg[
"name"].str())
43 , min_(
cfg[
"min"].to_int())
44 , max_(
cfg[
"max"].str() ==
"infinite" ? -1 :
cfg[
"max"].to_int(1))
45 , min_children_(
cfg[
"min_tags"].to_int())
46 , max_children_(
cfg[
"max_tags"].str() ==
"infinite" ? -1 :
cfg[
"max_tags"].to_int(-1))
53 , fuzzy_(name_.find_first_of(
"*?+") != std::string::npos)
54 , any_tag_(
cfg[
"any_tag"].to_bool())
57 max_ = std::numeric_limits<int>::max();
78 std::string link_name = link[
"name"].str();
98 std::istringstream
i(
s);
106 std::istringstream
i(
s);
114 std::string::size_type pos_last = link.rfind(
'/');
116 std::string name_link = link.substr(pos_last + 1, link.length());
117 links_.emplace(name_link, link);
122 auto visited = std::vector<const wml_tag*>();
123 return find_key(name, match, ignore_super, visited);
133 visited.push_back(
this);
138 if(cond.matches(match)) {
140 if(
auto key = cond.find_key(name, match,
true)) {
146 const auto it_keys =
keys_.find(name);
147 if(it_keys !=
keys_.end()) {
148 return &(it_keys->second);
151 key_map::const_iterator it_fuzzy = std::find_if(
keys_.begin(),
keys_.end(), [&name](
const key_map::value_type& key){
152 if(!key.second.is_fuzzy()) {
157 if(it_fuzzy != keys_.end()) {
158 return &(it_fuzzy->second);
162 for(
auto& cond : conditions_) {
163 if(cond.matches(match)) {
165 if(
auto key = cond.find_key(name, match,
false, visited)) {
170 for(
auto& [
_, super_tag] : super_refs_) {
171 if(
const wml_key* found_key = super_tag->find_key(name, match,
false, visited)) {
180 const std::string* wml_tag::find_link(
const std::string& name)
const
182 const auto it_links = links_.find(name);
183 if(it_links != links_.end()) {
184 return &(it_links->second);
190 const wml_tag* wml_tag::find_tag(
const std::string& fullpath,
const wml_tag& root,
const config& match,
bool ignore_super)
const
192 auto visited = std::vector<const wml_tag*>();
193 return find_tag(fullpath, root, match, ignore_super, visited);
196 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
203 visited.push_back(
this);
205 if(fullpath.empty()) {
209 std::string::size_type pos = fullpath.find(
'/');
211 std::string next_path;
213 if(pos != std::string::npos) {
214 name = fullpath.substr(0, pos);
215 next_path = fullpath.substr(pos + 1, fullpath.length());
222 for(
auto& cond : conditions_) {
223 if(cond.matches(match)) {
225 if(
auto tag = cond.find_tag(fullpath, root, match,
true)) {
231 const auto it_tags = tags_.find(name);
232 if(it_tags != tags_.end()) {
233 if(next_path.empty()) {
234 return &(it_tags->second);
236 return it_tags->second.find_tag(next_path, root, match,
false, visited);
240 const auto it_links = links_.find(name);
241 if(it_links != links_.end()) {
243 return root.
find_tag(it_links->second +
"/" + next_path, root, match,
false);
246 const auto it_fuzzy = std::find_if(tags_.begin(), tags_.end(), [&name](
const tag_map::value_type&
tag) {
247 if(!tag.second.fuzzy_) {
252 if(it_fuzzy != tags_.end()) {
253 if(next_path.empty()) {
254 return &(it_fuzzy->second);
256 return it_tags->second.
find_tag(next_path, root, match,
false, visited);
261 for(
auto& cond : conditions_) {
262 if(cond.matches(match)) {
264 if(
auto tag = cond.find_tag(fullpath, root, match,
false, visited)) {
269 for(
auto& [
_, super_tag] : super_refs_) {
270 if(
const wml_tag* found_tag = super_tag->find_tag(fullpath, root, match,
false, visited)) {
285 for(
auto&
tag : tags_) {
286 tag.second.expand(root);
287 tag.second.expand_all(root);
289 for(
auto& cond : conditions_) {
291 cond.expand_all(root);
295 void wml_tag::remove_keys_by_type(
const std::string&
type)
297 auto i = keys_.begin();
298 while(
i != keys_.end()) {
299 if(
i->second.get_type() ==
type) {
306 for(
auto&
tag : tags_) {
307 tag.second.remove_keys_by_type(
type);
311 void wml_tag::printl(std::ostream& os,
int level,
int step)
314 for(
int j = 0; j <
level; j++) {
319 <<
s <<
" name=\"" << name_ <<
"\"\n"
320 <<
s <<
" min=\"" << min_ <<
"\"\n"
321 <<
s <<
" max=\"" << max_ <<
"\"\n";
323 if(!super_.empty()) {
324 os <<
s <<
" super=\"" << super_ <<
"\"\n";
327 for(
auto&
tag : tags_) {
328 tag.second.printl(os,
level + step, step);
331 for(
auto& link : links_) {
335 <<
" name=\"" << link.second <<
"\"\n"
340 for(
auto& key : keys_) {
341 key.second.print(os,
level + step);
346 os <<
s <<
"[/tag]\n";
352 auto it = tags_.find(
tag.name_);
354 if(it == tags_.end()) {
355 tags_.emplace(
tag.name_,
tag);
357 it->second.set_min(
tag.min_);
358 it->second.set_max(
tag.max_);
359 it->second.add_tags(
tag.tags_);
360 it->second.add_keys(
tag.keys_);
361 it->second.add_links(
tag.links_);
365 links_.erase(
tag.get_name());
369 std::string::size_type pos =
path.find(
'/');
370 std::string name =
path.substr(0, pos);
371 std::string next_path =
path.substr(pos + 1,
path.length());
373 auto it_links = links_.find(name);
374 if(it_links != links_.end()) {
375 root.
add_tag(it_links->second +
"/" + next_path,
tag, root);
378 auto it_tags = tags_.find(name);
379 if(it_tags == tags_.end()) {
383 tags_.emplace(name, subtag);
387 it_tags->second.add_tag(next_path,
tag, root);
392 conditions_.insert(conditions_.end(), list.begin(), list.end());
400 if(super_tag !=
this) {
401 super_refs_.emplace(super, super_tag);
407 void wml_tag::add_switch(
const config& switch_cfg)
410 const std::string key = switch_cfg[
"key"];
411 bool allow_missing =
false;
412 for(
const auto& case_cfg : switch_cfg.
child_range(
"case")) {
413 if(case_cfg.has_attribute(
"value")) {
416 for(
const auto& value :
values) {
422 filter.add_child(
"or")[key] = value;
424 default_cfg.
add_child(
"not")[key] = value;
426 if(!allow_missing && case_cfg[
"trigger_if_missing"].to_bool()) {
427 config& missing_filter =
filter.add_child(
"or").add_child(
"not");
428 missing_filter[
"glob_on_" + key] =
"*";
429 allow_missing =
true;
431 conditions_.emplace_back(case_cfg,
filter);
434 conditions_.emplace_back(case_cfg,
config());
436 const std::string name =
formatter() << get_name() <<
'[' << key <<
'=' << case_cfg[
"value"] <<
']';
437 conditions_.back().set_name(name);
444 default_cfg.
add_child(
"and")[
"glob_on_" + key] =
"*";
446 conditions_.emplace_back(switch_cfg.
mandatory_child(
"else"), default_cfg);
447 const std::string name =
formatter() << get_name() <<
"[else]";
448 conditions_.back().set_name(name);
452 void wml_tag::add_filter(
const config& cond_cfg)
455 filter.clear_children(
"then",
"else",
"elseif");
458 else_filter.add_child(
"not",
filter);
461 const std::string name =
formatter() << get_name() <<
"[then]";
462 conditions_.back().set_name(name);
465 for(
auto elseif_cfg : cond_cfg.
child_range(
"elseif")) {
466 config elseif_filter = elseif_cfg, old_else_filter = else_filter;
468 else_filter.add_child(
"not", elseif_filter);
471 conditions_.emplace_back(elseif_cfg.child_or_empty(
"then"), elseif_filter);
472 const std::string name =
formatter() << get_name() <<
"[elseif " <<
i++ <<
"]";
473 conditions_.back().set_name(name);
476 conditions_.emplace_back(cond_cfg.
mandatory_child(
"else"), else_filter);
477 const std::string name =
formatter() << get_name() <<
"[else]";
478 conditions_.back().set_name(name);
497 current = base_tag.
tags_.begin();
498 condition_queue.push(&base_tag);
502 void wml_tag::tag_iterator::ensure_valid_or_end() {
503 while(current == condition_queue.front()->tags_.end()) {
504 condition_queue.pop();
505 if(condition_queue.empty()) {
508 const wml_tag& new_base = *condition_queue.front();
509 current= new_base.
tags_.begin();
510 push_new_tag_conditions(new_base);
517 current = base_tag.
keys_.begin();
518 condition_queue.push(&base_tag);
522 void wml_tag::key_iterator::ensure_valid_or_end() {
523 while(current == condition_queue.front()->keys_.end()) {
524 condition_queue.pop();
525 if(condition_queue.empty()) {
528 const wml_tag& new_base = *condition_queue.front();
529 current = new_base.keys_.begin();
530 push_new_tag_conditions(new_base);
537 current = base_tag.super_refs_.begin();
538 condition_queue.push(&base_tag);
542 void wml_tag::super_iterator::ensure_valid_or_end()
544 while(current == condition_queue.front()->super_refs_.end()) {
545 condition_queue.pop();
546 if(condition_queue.empty()) {
549 const wml_tag& new_base = *condition_queue.front();
550 current = new_base.super_refs_.begin();
551 push_new_tag_conditions(new_base);
555 void wml_tag::push_new_tag_conditions(std::queue<const wml_tag*>& q,
const config& match,
const wml_tag&
tag)
557 for(
const auto& condition :
tag.conditions_) {
558 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)
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.
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)
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified tag.
wml_tag any_tag("", 0, -1, "", true)
struct utils::detail::formula_initer init
@ STRIP_SPACES
REMOVE_EMPTY: remove empty elements.
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
bool wildcard_string_match(std::string_view str, std::string_view pat) noexcept
Performs pattern matching with wildcards.
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