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);
114 if(cond.matches(match)) {
115 if(
auto key = cond.find_key(name, match,
true)) {
121 const auto it_keys =
keys_.find(name);
122 if(it_keys !=
keys_.end()) {
123 return &(it_keys->second);
126 key_map::const_iterator it_fuzzy = std::find_if(
keys_.begin(),
keys_.end(), [&name](
const key_map::value_type& key){
127 if(!key.second.is_fuzzy()) {
132 if(it_fuzzy != keys_.end()) {
133 return &(it_fuzzy->second);
137 for(
auto& cond : conditions_) {
138 if(cond.matches(match)) {
140 if(
auto key = cond.find_key(name, match,
false)) {
145 for(
auto& super_tag : super_refs_) {
146 if(
const wml_key* found_key = super_tag->find_key(name, match)) {
155 const std::string* wml_tag::find_link(
const std::string& name)
const
157 const auto it_links = links_.find(name);
158 if(it_links != links_.end()) {
159 return &(it_links->second);
165 const wml_tag* wml_tag::find_tag(
const std::string& fullpath,
const wml_tag& root,
const config& match,
bool ignore_super)
const
167 if(fullpath.empty()) {
171 std::string::size_type pos = fullpath.find(
'/');
173 std::string next_path;
175 if(pos != std::string::npos) {
176 name = fullpath.substr(0, pos);
177 next_path = fullpath.substr(pos + 1, fullpath.length());
184 for(
auto& cond : conditions_) {
185 if(cond.matches(match)) {
186 if(
auto tag = cond.find_tag(fullpath, root, match,
true)) {
192 const auto it_tags = tags_.find(name);
193 if(it_tags != tags_.end()) {
194 if(next_path.empty()) {
195 return &(it_tags->second);
197 return it_tags->second.
find_tag(next_path, root, match);
201 const auto it_links = links_.find(name);
202 if(it_links != links_.end()) {
203 return root.
find_tag(it_links->second +
"/" + next_path, root, match);
206 const auto it_fuzzy = std::find_if(tags_.begin(), tags_.end(), [&name](
const tag_map::value_type& tag){
207 if(!tag.second.fuzzy_) {
212 if(it_fuzzy != tags_.end()) {
213 if(next_path.empty()) {
214 return &(it_fuzzy->second);
216 return it_tags->second.
find_tag(next_path, root, match);
221 for(
auto& cond : conditions_) {
222 if(cond.matches(match)) {
224 if(
auto tag = cond.find_tag(fullpath, root, match,
false)) {
229 for(
auto& super_tag : super_refs_) {
230 if(
const wml_tag* found_tag = super_tag->find_tag(fullpath, root, match)) {
245 for(
auto& tag : tags_) {
247 tag.second.expand_all(root);
249 for(
auto& cond : conditions_) {
251 cond.expand_all(root);
255 void wml_tag::remove_keys_by_type(
const std::string&
type)
257 auto i = keys_.begin();
258 while(
i != keys_.end()) {
259 if(
i->second.get_type() ==
type) {
266 for(
auto& tag : tags_) {
267 tag.second.remove_keys_by_type(
type);
271 void wml_tag::printl(std::ostream& os,
int level,
int step)
274 for(
int j = 0; j <
level; j++) {
279 <<
s <<
" name=\"" << name_ <<
"\"\n"
280 <<
s <<
" min=\"" << min_ <<
"\"\n"
281 <<
s <<
" max=\"" << max_ <<
"\"\n";
283 if(!super_.empty()) {
284 os <<
s <<
" super=\"" << super_ <<
"\"\n";
287 for(
auto& tag : tags_) {
288 tag.second.printl(os,
level + step, step);
291 for(
auto& link : links_) {
295 <<
" name=\"" << link.second <<
"\"\n"
300 for(
auto& key : keys_) {
301 key.second.print(os,
level + step);
306 os <<
s <<
"[/tag]\n";
312 auto it = tags_.find(tag.
name_);
314 if(it == tags_.end()) {
315 tags_.emplace(tag.
name_, tag);
317 it->second.set_min(tag.
min_);
318 it->second.set_max(tag.
max_);
319 it->second.add_tags(tag.
tags_);
320 it->second.add_keys(tag.
keys_);
321 it->second.add_links(tag.
links_);
329 std::string::size_type pos =
path.find(
'/');
330 std::string name =
path.substr(0, pos);
331 std::string next_path =
path.substr(pos + 1,
path.length());
333 auto it_links = links_.find(name);
334 if(it_links != links_.end()) {
335 root.
add_tag(it_links->second +
"/" + next_path, tag, root);
338 auto it_tags = tags_.find(name);
339 if(it_tags == tags_.end()) {
342 subtag.
add_tag(next_path, tag, root);
343 tags_.emplace(name, subtag);
347 it_tags->second.add_tag(next_path, tag, root);
352 conditions_.insert(conditions_.end(), list.begin(), list.end());
360 if(super_tag !=
this) {
361 super_refs_.push_back(super_tag);
371 void wml_tag::add_switch(
const config& switch_cfg)
374 const std::string key = switch_cfg[
"key"];
375 bool allow_missing =
false;
376 for(
const auto& case_cfg : switch_cfg.
child_range(
"case")) {
377 if(case_cfg.has_attribute(
"value")) {
380 for(
const auto& value : values) {
388 default_cfg.
add_child(
"not")[key] = value;
390 if(!allow_missing && case_cfg[
"trigger_if_missing"].to_bool()) {
392 missing_filter[
"glob_on_" + key] =
"*";
393 allow_missing =
true;
395 conditions_.emplace_back(case_cfg, filter);
398 conditions_.emplace_back(case_cfg,
config());
400 const std::string name =
formatter() << get_name() <<
'[' << key <<
'=' << case_cfg[
"value"] <<
']';
401 conditions_.back().set_name(name);
408 default_cfg.
add_child(
"and")[
"glob_on_" + key] =
"*";
410 conditions_.emplace_back(switch_cfg.
mandatory_child(
"else"), default_cfg);
411 const std::string name =
formatter() << get_name() <<
"[else]";
412 conditions_.back().set_name(name);
416 void wml_tag::add_filter(
const config& cond_cfg)
418 config filter = cond_cfg, else_filter;
422 else_filter.add_child(
"not", filter);
425 const std::string name =
formatter() << get_name() <<
"[then]";
426 conditions_.back().set_name(name);
429 for(
auto elseif_cfg : cond_cfg.
child_range(
"elseif")) {
430 config elseif_filter = elseif_cfg, old_else_filter = else_filter;
432 else_filter.add_child(
"not", elseif_filter);
435 conditions_.emplace_back(elseif_cfg.child_or_empty(
"then"), elseif_filter);
436 const std::string name =
formatter() << get_name() <<
"[elseif " <<
i++ <<
"]";
437 conditions_.back().set_name(name);
440 conditions_.emplace_back(cond_cfg.
mandatory_child(
"else"), else_filter);
441 const std::string name =
formatter() << get_name() <<
"[else]";
442 conditions_.back().set_name(name);
446 bool wml_condition::matches(
const config& cfg)
const
461 current = base_tag.
tags_.begin();
462 condition_queue.push(&base_tag);
466 void wml_tag::tag_iterator::ensure_valid_or_end() {
467 while(current == condition_queue.front()->tags_.end()) {
468 condition_queue.pop();
469 if(condition_queue.empty()) {
472 const wml_tag& new_base = *condition_queue.front();
473 current= new_base.
tags_.begin();
474 push_new_tag_conditions(new_base);
481 current = base_tag.
keys_.begin();
482 condition_queue.push(&base_tag);
486 void wml_tag::key_iterator::ensure_valid_or_end() {
487 while(current == condition_queue.front()->keys_.end()) {
488 condition_queue.pop();
489 if(condition_queue.empty()) {
492 const wml_tag& new_base = *condition_queue.front();
493 current = new_base.keys_.begin();
494 push_new_tag_conditions(new_base);
498 void wml_tag::push_new_tag_conditions(std::queue<const wml_tag*>& q,
const config& match,
const wml_tag& tag) {
500 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.
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